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 {...@@ -43,6 +43,7 @@ fn clean(out: anytype, n: usize) !void {
4343
44pub fn forEnum(out: anytype, in: anytype, comptime prompt: []const u8, alloc: *std.mem.Allocator, comptime options: type, default: ?options) !options {44pub fn forEnum(out: anytype, in: anytype, comptime prompt: []const u8, alloc: *std.mem.Allocator, comptime options: type, default: ?options) !options {
45 comptime std.debug.assert(@typeInfo(options) == .Enum);45 comptime std.debug.assert(@typeInfo(options) == .Enum);
46 const def: ?[]const u8 = if (default) |d| @tagName(d) else null;
4647
47 try out.print(comptime ansi.color.Fg(.Green, "? "), .{});48 try out.print(comptime ansi.color.Fg(.Green, "? "), .{});
48 try out.print(comptime ansi.color.Bold(prompt ++ " "), .{});49 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...@@ -50,14 +51,17 @@ pub fn forEnum(out: anytype, in: anytype, comptime prompt: []const u8, alloc: *s
50 try out.print(ansi.style.Faint ++ "(", .{});51 try out.print(ansi.style.Faint ++ "(", .{});
51 inline for (std.meta.fields(options)) |f, i| {52 inline for (std.meta.fields(options)) |f, i| {
52 if (i != 0) try out.print("/", .{});53 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, .{});
54 }59 }
55 try out.print(")" ++ ansi.style.ResetIntensity ++ " ", .{});60 try out.print(")" ++ ansi.style.ResetIntensity ++ " ", .{});
5661
57 var value: options = undefined;62 var value: options = undefined;
58 var i: usize = 0;63 var i: usize = 0;
59 while (true) {64 while (true) {
60 const def: ?[]const u8 = if (default) |d| @tagName(d) else null;
61 const p = try doprompt(out, in, alloc, def);65 const p = try doprompt(out, in, alloc, def);
62 defer if (!std.mem.eql(u8, p.value, def orelse "")) alloc.free(p.value);66 defer if (!std.mem.eql(u8, p.value, def orelse "")) alloc.free(p.value);
6367