authorgravatar for cj@vdbonline.comCJ van den Berg <cj@vdbonline.com> 2021-08-03 17:31:39 +02:00
committergravatar for cj@vdbonline.comCJ van den Berg <cj@vdbonline.com> 2021-08-03 22:59:11 +02:00
logf5ed8770acb3d1e2c382d92050d73090fa06ec96
treea236bd166a6fbe03f26e2879e8b78acbaab952b2
parent00f5c71e9dda6c35d478aead9da569c02f3f8db0

Highlight default option in prompts


1 files changed, 6 insertions(+), 2 deletions(-)

src/lib.zig+6-2
......@@ -43,6 +43,7 @@ fn clean(out: anytype, n: usize) !void {
4343
4444pub fn forEnum(out: anytype, in: anytype, comptime prompt: []const u8, alloc: *std.mem.Allocator, comptime options: type, default: ?options) !options {
4545 comptime std.debug.assert(@typeInfo(options) == .Enum);
46 const def: ?[]const u8 = if (default) |d| @tagName(d) else null;
4647
4748 try out.print(comptime ansi.color.Fg(.Green, "? "), .{});
4849 try out.print(comptime ansi.color.Bold(prompt ++ " "), .{});
......@@ -50,14 +51,17 @@ pub fn forEnum(out: anytype, in: anytype, comptime prompt: []const u8, alloc: *s
5051 try out.print(ansi.style.Faint ++ "(", .{});
5152 inline for (std.meta.fields(options)) |f, i| {
5253 if (i != 0) try out.print("/", .{});
53 try out.print(f.name, .{});
54 if (std.mem.eql(u8, f.name, def orelse "")) {
55 try out.print(ansi.style.ResetIntensity, .{});
56 try out.print(comptime ansi.color.Fg(.Cyan, f.name), .{});
57 try out.print(ansi.style.Faint, .{});
58 } else try out.print(f.name, .{});
5459 }
5560 try out.print(")" ++ ansi.style.ResetIntensity ++ " ", .{});
5661
5762 var value: options = undefined;
5863 var i: usize = 0;
5964 while (true) {
60 const def: ?[]const u8 = if (default) |d| @tagName(d) else null;
6165 const p = try doprompt(out, in, alloc, def);
6266 defer if (!std.mem.eql(u8, p.value, def orelse "")) alloc.free(p.value);
6367