feat(list-languages): add colored file type icons

This commit is contained in:
CJ van den Berg 2025-02-11 14:40:45 +01:00
parent 240e82a8c2
commit d6ff940fb7
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 14 additions and 1 deletions

View file

@ -509,6 +509,7 @@ pub fn build_exe(
exe.root_module.addImport("renderer", renderer_mod);
exe.root_module.addImport("input", input_mod);
exe.root_module.addImport("syntax", syntax_mod);
exe.root_module.addImport("color", color_mod);
exe.root_module.addImport("version_info", b.createModule(.{ .root_source_file = version_info_file }));
if (target.result.os.tag == .windows) {

View file

@ -1,6 +1,7 @@
const std = @import("std");
const syntax = @import("syntax");
const builtin = @import("builtin");
const RGB = @import("color").RGB;
const bin_path = @import("bin_path.zig");
@ -23,7 +24,7 @@ pub fn list(allocator: std.mem.Allocator, writer: anytype, tty_config: std.io.tt
}
try tty_config.setColor(writer, .yellow);
try write_string(writer, "Language", max_language_len + 1);
try write_string(writer, " Language", max_language_len + 1 + 4);
try write_string(writer, "Extensions", max_extensions_len + 1 + checkmark_width);
try write_string(writer, "Language Server", max_langserver_len + 1 + checkmark_width);
try write_string(writer, "Formatter", null);
@ -31,6 +32,11 @@ pub fn list(allocator: std.mem.Allocator, writer: anytype, tty_config: std.io.tt
try writer.writeAll("\n");
for (syntax.FileType.file_types) |file_type| {
try writer.writeAll(" ");
try setColorRgb(writer, file_type.color);
try writer.writeAll(file_type.icon);
try tty_config.setColor(writer, .reset);
try writer.writeAll(" ");
try write_string(writer, file_type.name, max_language_len + 1);
try write_segmented(writer, file_type.extensions, ",", max_extensions_len + 1, tty_config);
@ -99,3 +105,9 @@ fn can_execute(allocator: std.mem.Allocator, binary_name: []const u8) bool {
defer if (resolved_binary_path) |path| allocator.free(path);
return resolved_binary_path != null;
}
fn setColorRgb(writer: anytype, color: u24) !void {
const fg_rgb_legacy = "\x1b[38;2;{d};{d};{d}m";
const rgb = RGB.from_u24(color);
try writer.print(fg_rgb_legacy, .{ rgb.r, rgb.g, rgb.b });
}