| ... | ... | @@ -2,10 +2,10 @@ const std = @import("std"); |
| 2 | 2 | const ansi = @import("ansi"); |
| 3 | 3 | const range = @import("range").range; |
| 4 | 4 | |
| 5 | | pub fn answer(comptime prompt: []const u8, comptime T: type, comptime valfmt: []const u8, value: T) T { |
| 6 | | std.debug.print(comptime ansi.color.Fg(.Green, "? "), .{}); |
| 7 | | std.debug.print(comptime ansi.color.Bold(prompt ++ " "), .{}); |
| 8 | | std.debug.print(comptime ansi.color.Fg(.Cyan, valfmt ++ "\n"), .{value}); |
| 5 | pub fn answer(out: anytype, comptime prompt: []const u8, comptime T: type, comptime valfmt: []const u8, value: T) !T { |
| 6 | try out.print(comptime ansi.color.Fg(.Green, "? "), .{}); |
| 7 | try out.print(comptime ansi.color.Bold(prompt ++ " "), .{}); |
| 8 | try out.print(comptime ansi.color.Fg(.Cyan, valfmt ++ "\n"), .{value}); |
| 9 | 9 | return value; |
| 10 | 10 | } |
| 11 | 11 | |
| ... | ... | @@ -14,11 +14,11 @@ const PromptRet = struct { |
| 14 | 14 | value: []const u8, |
| 15 | 15 | }; |
| 16 | 16 | |
| 17 | | fn doprompt(in: std.fs.File.Reader, alloc: *std.mem.Allocator, default: ?[]const u8) !PromptRet { |
| 17 | fn doprompt(out: anytype, in: anytype, alloc: *std.mem.Allocator, default: ?[]const u8) !PromptRet { |
| 18 | 18 | var n: usize = 1; |
| 19 | 19 | var value: []const u8 = undefined; |
| 20 | 20 | while (true) : (n += 1) { |
| 21 | | std.debug.print(comptime ansi.color.Faint("> "), .{}); |
| 21 | try out.print(comptime ansi.color.Faint("> "), .{}); |
| 22 | 22 | const input = try in.readUntilDelimiterAlloc(alloc, '\n', 100); |
| 23 | 23 | |
| 24 | 24 | if (input.len == 0) if (default) |d| { |
| ... | ... | @@ -33,32 +33,31 @@ fn doprompt(in: std.fs.File.Reader, alloc: *std.mem.Allocator, default: ?[]const |
| 33 | 33 | return PromptRet{ .n = n, .value = value }; |
| 34 | 34 | } |
| 35 | 35 | |
| 36 | | fn clean(n: usize) void { |
| 36 | fn clean(out: anytype, n: usize) !void { |
| 37 | 37 | for (range(n)) |_| { |
| 38 | | std.debug.print(comptime ansi.csi.CursorUp(1), .{}); |
| 39 | | std.debug.print(comptime ansi.csi.EraseInLine(0), .{}); |
| 38 | try out.print(comptime ansi.csi.CursorUp(1), .{}); |
| 39 | try out.print(comptime ansi.csi.EraseInLine(0), .{}); |
| 40 | 40 | } |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | | pub fn forEnum(comptime prompt: []const u8, alloc: *std.mem.Allocator, comptime options: type, default: ?options) !options { |
| 43 | pub fn forEnum(out: anytype, in: anytype, comptime prompt: []const u8, alloc: *std.mem.Allocator, comptime options: type, default: ?options) !options { |
| 44 | 44 | comptime std.debug.assert(@typeInfo(options) == .Enum); |
| 45 | 45 | |
| 46 | | std.debug.print(comptime ansi.color.Fg(.Green, "? "), .{}); |
| 47 | | std.debug.print(comptime ansi.color.Bold(prompt ++ " "), .{}); |
| 46 | try out.print(comptime ansi.color.Fg(.Green, "? "), .{}); |
| 47 | try out.print(comptime ansi.color.Bold(prompt ++ " "), .{}); |
| 48 | 48 | |
| 49 | | std.debug.print(ansi.style.Faint ++ "(", .{}); |
| 49 | try out.print(ansi.style.Faint ++ "(", .{}); |
| 50 | 50 | inline for (std.meta.fields(options)) |f, i| { |
| 51 | | if (i != 0) std.debug.print("/", .{}); |
| 52 | | std.debug.print(f.name, .{}); |
| 51 | if (i != 0) try out.print("/", .{}); |
| 52 | try out.print(f.name, .{}); |
| 53 | 53 | } |
| 54 | | std.debug.print(")" ++ ansi.style.ResetIntensity ++ " ", .{}); |
| 54 | try out.print(")" ++ ansi.style.ResetIntensity ++ " ", .{}); |
| 55 | 55 | |
| 56 | | const stdin = std.io.getStdIn().reader(); |
| 57 | 56 | var value: options = undefined; |
| 58 | 57 | var i: usize = 0; |
| 59 | 58 | while (true) { |
| 60 | 59 | const def: ?[]const u8 = if (default) |d| @tagName(d) else null; |
| 61 | | const p = try doprompt(stdin, alloc, def); |
| 60 | const p = try doprompt(out, in, alloc, def); |
| 62 | 61 | defer if (!std.mem.eql(u8, p.value, def orelse "")) alloc.free(p.value); |
| 63 | 62 | |
| 64 | 63 | i += p.n; |
| ... | ... | @@ -67,43 +66,41 @@ pub fn forEnum(comptime prompt: []const u8, alloc: *std.mem.Allocator, comptime |
| 67 | 66 | break; |
| 68 | 67 | } |
| 69 | 68 | } |
| 70 | | clean(i); |
| 71 | | _ = answer(prompt, []const u8, "{s}", @tagName(value)); |
| 69 | try clean(out, i); |
| 70 | _ = try answer(out, prompt, []const u8, "{s}", @tagName(value)); |
| 72 | 71 | |
| 73 | 72 | return value; |
| 74 | 73 | } |
| 75 | 74 | |
| 76 | | pub fn forString(comptime prompt: []const u8, alloc: *std.mem.Allocator, default: ?[]const u8) ![]const u8 { |
| 77 | | std.debug.print(comptime ansi.color.Fg(.Green, "? "), .{}); |
| 78 | | std.debug.print(comptime ansi.color.Bold(prompt ++ " "), .{}); |
| 75 | pub fn forString(out: anytype, in: anytype, comptime prompt: []const u8, alloc: *std.mem.Allocator, default: ?[]const u8) ![]const u8 { |
| 76 | try out.print(comptime ansi.color.Fg(.Green, "? "), .{}); |
| 77 | try out.print(comptime ansi.color.Bold(prompt ++ " "), .{}); |
| 79 | 78 | |
| 80 | 79 | if (default != null) { |
| 81 | | std.debug.print(ansi.style.Faint ++ "(", .{}); |
| 82 | | std.debug.print("{s}", .{default.?}); |
| 83 | | std.debug.print(")" ++ ansi.style.ResetIntensity ++ " ", .{}); |
| 80 | try out.print(ansi.style.Faint ++ "(", .{}); |
| 81 | try out.print("{s}", .{default.?}); |
| 82 | try out.print(")" ++ ansi.style.ResetIntensity ++ " ", .{}); |
| 84 | 83 | } |
| 85 | 84 | |
| 86 | | const stdin = std.io.getStdIn().reader(); |
| 87 | | const p = try doprompt(stdin, alloc, default); |
| 88 | | clean(p.n); |
| 89 | | return answer(prompt, []const u8, "{s}", p.value); |
| 85 | const p = try doprompt(out, in, alloc, default); |
| 86 | try clean(out, p.n); |
| 87 | return try answer(out, prompt, []const u8, "{s}", p.value); |
| 90 | 88 | } |
| 91 | 89 | |
| 92 | | pub fn forConfirm(comptime prompt: []const u8, alloc: *std.mem.Allocator) !bool { |
| 93 | | return (try forEnum(prompt, alloc, enum { y, n }, .y)) == .y; |
| 90 | pub fn forConfirm(out: anytype, in: anytype, comptime prompt: []const u8, alloc: *std.mem.Allocator) !bool { |
| 91 | return (try forEnum(out, in, prompt, alloc, enum { y, n }, .y)) == .y; |
| 94 | 92 | } |
| 95 | 93 | |
| 96 | 94 | // pub fn forNumber(comptime prompt: []const u8, alloc: *std.mem.Allocator, comptime T: type, default: ?T) !T { |
| 97 | | // std.debug.print(comptime ansi.color.Fg(.Green, "? "), .{}); |
| 98 | | // std.debug.print(comptime ansi.color.Bold(prompt ++ " "), .{}); |
| 95 | // try out.print(comptime ansi.color.Fg(.Green, "? "), .{}); |
| 96 | // try out.print(comptime ansi.color.Bold(prompt ++ " "), .{}); |
| 99 | 97 | |
| 100 | 98 | // if (default != null) { |
| 101 | | // std.debug.print(ansi.style.Faint ++ "(", .{}); |
| 102 | | // std.debug.print("{d}", .{default.?}); |
| 103 | | // std.debug.print(")" ++ ansi.style.ResetIntensity ++ " ", .{}); |
| 99 | // try out.print(ansi.style.Faint ++ "(", .{}); |
| 100 | // try out.print("{d}", .{default.?}); |
| 101 | // try out.print(")" ++ ansi.style.ResetIntensity ++ " ", .{}); |
| 104 | 102 | // } |
| 105 | 103 | |
| 106 | | // const stdin = std.io.getStdIn().reader(); |
| 107 | 104 | // var value: T = undefined; |
| 108 | 105 | // var i: usize = 0; |
| 109 | 106 | // while (true) { |