sort output from +list-colors

This commit is contained in:
Jeffrey C. Ollie
2024-01-11 00:30:48 -06:00
parent 00129b23c5
commit 5258554135

View File

@ -1,5 +1,4 @@
const std = @import("std"); const std = @import("std");
const inputpkg = @import("../input.zig");
const args = @import("args.zig"); const args = @import("args.zig");
const x11_color = @import("../terminal/main.zig").x11_color; const x11_color = @import("../terminal/main.zig").x11_color;
@ -9,6 +8,10 @@ pub const Options = struct {
} }
}; };
fn cmp(_: void, lhs: []const u8, rhs: []const u8) bool {
return std.ascii.lessThanIgnoreCase(lhs, rhs);
}
/// The "list-colors" command is used to list all the named RGB colors in /// The "list-colors" command is used to list all the named RGB colors in
/// Ghostty. /// Ghostty.
pub fn run(alloc: std.mem.Allocator) !u8 { pub fn run(alloc: std.mem.Allocator) !u8 {
@ -23,9 +26,17 @@ pub fn run(alloc: std.mem.Allocator) !u8 {
const stdout = std.io.getStdOut().writer(); const stdout = std.io.getStdOut().writer();
var keys = std.ArrayList([]const u8).init(alloc);
inline for (x11_color.map.kvs) |kv| { inline for (x11_color.map.kvs) |kv| {
const name = kv.key; try keys.append(kv.key);
const rgb = kv.value; }
const sorted = try keys.toOwnedSlice();
std.sort.insertion([]const u8, sorted, {}, cmp);
for (sorted) |name| {
const rgb = x11_color.map.get(name).?;
try stdout.print("{s} = #{x:0>2}{x:0>2}{x:0>2}\n", .{ name, rgb.r, rgb.g, rgb.b }); try stdout.print("{s} = #{x:0>2}{x:0>2}{x:0>2}\n", .{ name, rgb.r, rgb.g, rgb.b });
} }