Something went wrong. Try again.
Charm-like tui library
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213const std = @import("std");const Arg = @import("Arg.zig");const u = @import("utils.zig");const Config = @import("Config.zig");const matte = @import("matte");
pub fn printGroup( writer: *std.Io.Writer, root_description: ?[]const u8, chain: []const []const u8, group: ?Arg, cmds: []const Arg, comptime config: Config,) !void { if (group) |g| { const color = switch (g.value) { .func => config.styles.func, .group => config.styles.group, }; try renderChain(color, writer, chain, config); try writer.print("\n{s}\n\n", .{g.description}); } else { try renderChain(config.styles.bin, writer, chain, config); if (root_description) |description| try writer.print("\n{s}", .{description}); try writer.writeAll("\n\n"); }
if (cmds.len == 0) return;
var max_key: usize = 0; for (cmds) |cmd| max_key = @max(max_key, cmd.key.len);
for (cmds) |cmd| { const desc = switch (cmd.value) { .func => firstLine(cmd.description), .group => firstLine(cmd.description), }; const base = switch (cmd.value) { .func => config.styles.func, .group => config.styles.group, }; const unique = switch (cmd.value) { .func => config.styles.unique_func, .group => config.styles.unique_group, };
const n = if (cmd.unique_index) |index| index + 1 else minPrefixLen(cmd.key, cmds, "key"); try renderMinPrefix(unique, base, writer, cmd.key, n); try pad(writer, cmd.key.len, max_key + 4); try writer.print("{s}\n", .{desc}); }}
pub fn printCommand( writer: *std.Io.Writer, chain: []const []const u8, description: []const u8, params: []const u.Info, comptime config: Config,) !void { if (description.len > 0) { try renderChain(config.styles.func, writer, chain, config); try renderUsageParams(writer, params, config); try writer.print("\n{s}\n\n", .{description}); }
if (params.len == 0) return;
var max_name: usize = 0; var max_type: usize = 0; for (params) |p| { max_name = @max(max_name, p.name.len); const display = p.type_text; max_type = @max(max_type, display.len); }
const desc_col = 2 + max_name + 4 + max_type + 4; for (params, 0..) |p, idx| { if (idx != 0) try writer.writeByte('\n'); const display = p.type_text; try writer.writeAll(" "); const n = if (p.unique_index) |index| index + 1 else minPrefixLen(p.name, params, "name"); try renderMinPrefix( config.styles.unique_param, config.styles.param, writer, p.name, n, ); try pad(writer, p.name.len, max_name + 4); try config.styles.type.render(writer, display); try pad(writer, display.len, max_type + 4);
var base: []const u8 = p.description; if (p.enum_values != null) { if (std.mem.find(u8, base, "\nOptions:")) |i| base = base[0..i]; }
var lines = std.mem.splitScalar(u8, base, '\n'); const first = lines.next() orelse ""; if (p.optional == .default_value) try config.styles.description.render(writer, "[optional] "); try config.styles.description.render(writer, first); while (lines.next()) |line| { try writer.writeByte('\n'); try pad(writer, 0, desc_col); try config.styles.description.render(writer, line); }
if (p.enum_values) |values| { const default: ?[]const u8 = switch (p.optional) { .default_value => |v| v, .not => null, }; try writer.writeByte('\n'); try pad(writer, 0, desc_col); try config.styles.choice.render(writer, "Choices: "); for (values, 0..) |value, i| { if (i != 0) try config.styles.choice.render(writer, ", "); const is_default = if (default) |d| std.mem.eql(u8, value, d) else false; if (is_default) { try config.styles.unique_choice.render(writer, value); try config.styles.choice.render(writer, " (default)"); } else try config.styles.choice.render(writer, value); } if (p.enum_values == null) { if (default) |d| { try writer.writeByte('\n'); try pad(writer, 0, desc_col); try writer.print("Default: {s}", .{d}); } } } try writer.writeByte('\n'); }}
pub fn printError(writer: *std.Io.Writer, msg: []const u8, comptime config: Config) !void { try config.styles.err.render(writer, "error:"); try writer.writeByte(' '); try writer.writeAll(msg);}
fn firstLine(s: []const u8) []const u8 { return if (std.mem.findScalar(u8, s, '\n')) |n| s[0..n] else s;}
/// Renders usage params for the header in the param style: " <name>" for/// required params and " [<name>]" for optional ones.fn renderUsageParams(writer: *std.Io.Writer, params: []const u.Info, comptime config: Config) !void { for (params) |p| { try writer.writeByte(' '); switch (p.optional) { .not => try config.styles.param.print(writer, "{s}", .{p.name}), .default_value => try config.styles.param.print(writer, "[{s}]", .{p.name}), } }}
/// Renders the full command chain with each/// segment colored by its type: the bin (first element, shown as basename)/// in `style.bin`, intermediate groups in `style.group`, and the final/// segment in `leaf_style`.fn renderChain( leaf_style: matte.Style, writer: *std.Io.Writer, chain: []const []const u8, comptime config: Config,) !void { const last = chain.len - 1; for (chain, 0..) |part, i| { if (i != 0) try writer.writeByte(' '); const segment_style: matte.Style = if (i == 0) config.styles.bin else if (i == last) leaf_style else config.styles.group; const name = if (i == 0) std.fs.path.basename(part) else part; try segment_style.render(writer, name); }}
fn renderMinPrefix( ul_style: matte.Style, base_style: matte.Style, writer: *std.Io.Writer, name: []const u8, n: usize,) !void { try ul_style.render(writer, name[0..n]); if (n < name.len) try base_style.render(writer, name[n..]);}
fn minPrefixLen(name: []const u8, items: anytype, comptime field_name: []const u8) usize { var best: usize = 1; for (items) |item| { const other = @field(item, field_name); if (std.mem.eql(u8, other, name)) continue; const n = @min(name.len, other.len); var lcp: usize = 0; while (lcp < n and name[lcp] == other[lcp]) : (lcp += 1) {} best = @max(best, @min(lcp + 1, name.len)); } return best;}
fn pad(writer: *std.Io.Writer, current: usize, target: usize) !void { if (current >= target) return; try writer.splatByteAll(' ', target - current);}