| ... | @@ -0,0 +1,135 @@ |
| 1 | const std = @import("std"); |
| 2 | const ansi = @import("ansi"); |
| 3 | const range = @import("range").range; |
| 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}); |
| 9 | return value; |
| 10 | } |
| 11 | |
| 12 | const PromptRet = struct { |
| 13 | n: usize, |
| 14 | value: []const u8, |
| 15 | }; |
| 16 | |
| 17 | fn doprompt(in: std.fs.File.Reader, alloc: *std.mem.Allocator, default: ?[]const u8) !PromptRet { |
| 18 | var n: usize = 1; |
| 19 | var value: []const u8 = undefined; |
| 20 | while (true) : (n += 1) { |
| 21 | std.debug.print(comptime ansi.color.Faint("> "), .{}); |
| 22 | const input = try in.readUntilDelimiterAlloc(alloc, '\n', 100); |
| 23 | |
| 24 | if (input.len == 0) if (default) |d| { |
| 25 | value = d; |
| 26 | break; |
| 27 | }; |
| 28 | if (input.len > 0) { |
| 29 | value = input; |
| 30 | break; |
| 31 | } |
| 32 | } |
| 33 | return PromptRet{ .n = n, .value = value }; |
| 34 | } |
| 35 | |
| 36 | fn clean(n: usize) void { |
| 37 | for (range(n)) |_| { |
| 38 | std.debug.print(comptime ansi.csi.CursorUp(1), .{}); |
| 39 | std.debug.print(comptime ansi.csi.EraseInLine(0), .{}); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | pub fn forEnum(comptime prompt: []const u8, alloc: *std.mem.Allocator, comptime options: type, default: ?options) !options { |
| 44 | comptime std.debug.assert(@typeInfo(options) == .Enum); |
| 45 | |
| 46 | std.debug.print(comptime ansi.color.Fg(.Green, "? "), .{}); |
| 47 | std.debug.print(comptime ansi.color.Bold(prompt ++ " "), .{}); |
| 48 | |
| 49 | std.debug.print(ansi.style.Faint ++ "(", .{}); |
| 50 | inline for (std.meta.fields(options)) |f, i| { |
| 51 | if (i != 0) std.debug.print("/", .{}); |
| 52 | std.debug.print(f.name, .{}); |
| 53 | } |
| 54 | std.debug.print(")" ++ ansi.style.ResetIntensity ++ " ", .{}); |
| 55 | |
| 56 | const stdin = std.io.getStdIn().reader(); |
| 57 | var value: options = undefined; |
| 58 | var i: usize = 0; |
| 59 | while (true) { |
| 60 | const def: ?[]const u8 = if (default) |d| @tagName(d) else null; |
| 61 | const p = try doprompt(stdin, alloc, def); |
| 62 | defer if (!std.mem.eql(u8, p.value, def orelse "")) alloc.free(p.value); |
| 63 | |
| 64 | i += p.n; |
| 65 | if (std.meta.stringToEnum(options, p.value)) |cap| { |
| 66 | value = cap; |
| 67 | break; |
| 68 | } |
| 69 | } |
| 70 | clean(i); |
| 71 | _ = answer(prompt, []const u8, "{s}", @tagName(value)); |
| 72 | |
| 73 | return value; |
| 74 | } |
| 75 | |
| 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 ++ " "), .{}); |
| 79 | |
| 80 | if (default != null) { |
| 81 | std.debug.print(ansi.style.Faint ++ "(", .{}); |
| 82 | std.debug.print("{s}", .{default.?}); |
| 83 | std.debug.print(")" ++ ansi.style.ResetIntensity ++ " ", .{}); |
| 84 | } |
| 85 | |
| 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); |
| 90 | } |
| 91 | |
| 92 | pub fn forConfirm(comptime prompt: []const u8, alloc: *std.mem.Allocator) !bool { |
| 93 | return (try forEnum(prompt, alloc, enum { y, n }, .y)) == .y; |
| 94 | } |
| 95 | |
| 96 | // 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 ++ " "), .{}); |
| 99 | |
| 100 | // if (default != null) { |
| 101 | // std.debug.print(ansi.style.Faint ++ "(", .{}); |
| 102 | // std.debug.print("{d}", .{default.?}); |
| 103 | // std.debug.print(")" ++ ansi.style.ResetIntensity ++ " ", .{}); |
| 104 | // } |
| 105 | |
| 106 | // const stdin = std.io.getStdIn().reader(); |
| 107 | // var value: T = undefined; |
| 108 | // var i: usize = 0; |
| 109 | // while (true) { |
| 110 | // const n = if (default != null) try std.fmt.allocPrint(alloc, "{d}", .{default}) else null; |
| 111 | // defer if (n != null) alloc.free(n.?); |
| 112 | // const p = try doprompt(stdin, alloc, if (default != null) n else null); |
| 113 | // defer alloc.free(p.value); |
| 114 | // i += p.n; |
| 115 | // switch (@typeInfo(T)) { |
| 116 | // .Int => { |
| 117 | // if (std.fmt.parseInt(T, p.value, 10) catch null) |cap| { |
| 118 | // value = cap; |
| 119 | // break; |
| 120 | // } |
| 121 | // }, |
| 122 | // .Float => { |
| 123 | // if (std.fmt.parseFloat(T, p.value) catch null) |cap| { |
| 124 | // value = cap; |
| 125 | // break; |
| 126 | // } |
| 127 | // }, |
| 128 | // else => @compileError("expected number type instead got: " ++ @typeName(T)), |
| 129 | // } |
| 130 | // } |
| 131 | // clean(i); |
| 132 | // _ = answer(prompt, T, "{d}", value); |
| 133 | |
| 134 | // return value; |
| 135 | // } |