themes: add a switch that shows a small preview of each theme

This commit is contained in:
Jeffrey C. Ollie
2024-08-11 22:06:17 -05:00
committed by Mitchell Hashimoto
parent 7a11b22c5f
commit 87791ed562

View File

@ -9,6 +9,9 @@ const internal_os = @import("../os/main.zig");
const global_state = &@import("../global.zig").state; const global_state = &@import("../global.zig").state;
pub const Options = struct { pub const Options = struct {
/// If true, show a small preview of the theme.
preview: bool = false,
pub fn deinit(self: Options) void { pub fn deinit(self: Options) void {
_ = self; _ = self;
} }
@ -23,12 +26,26 @@ pub const Options = struct {
/// The `list-themes` command is used to list all the available themes for /// The `list-themes` command is used to list all the available themes for
/// Ghostty. /// Ghostty.
/// ///
/// Themes require that Ghostty have access to the resources directory. On macOS /// Two different directories will be searched for themes.
/// this is embedded in the app bundle. On Linux, this is usually in `/usr/ ///
/// share/ghostty`. If you're compiling from source, this is the `zig-out/share/ /// The first directory is the `themes` subdirectory of your Ghostty
/// ghostty` directory. You can also set the `GHOSTTY_RESOURCES_DIR` environment /// configuration directory. This is `$XDG_CONFIG_DIR/ghostty/themes` or
/// variable to point to the resources directory. Themes live in the `themes` /// `~/.config/ghostty/themes`.
/// subdirectory of the resources directory. ///
/// The second directory is the `themes` subdirectory of the Ghostty resources
/// directory. Ghostty ships with a multitude of themes that will be installed
/// into this directory. On macOS, this directory is the `Ghostty.app/Contents/
/// Resources/ghostty/themes`. On Linux, this directory is the `share/ghostty/
/// themes` (wherever you installed the Ghostty "share" directory). If you're
/// running Ghostty from the source, this is the `zig-out/share/ghostty/themes`
/// directory.
///
/// You can also set the `GHOSTTY_RESOURCES_DIR` environment variable to point
/// to the resources directory.
///
/// Flags:
///
/// * `--preview`: Show a short preview of the theme colors.
pub fn run(alloc: Allocator) !u8 { pub fn run(alloc: Allocator) !u8 {
var opts: Options = .{}; var opts: Options = .{};
defer opts.deinit(); defer opts.deinit();
@ -107,6 +124,40 @@ pub fn run(alloc: Allocator) !u8 {
for (themes.items) |theme| { for (themes.items) |theme| {
try stdout.print("{s} ({s})\n", .{ theme.theme, @tagName(theme.type) }); try stdout.print("{s} ({s})\n", .{ theme.theme, @tagName(theme.type) });
if (opts.preview) {
var config = try Config.default(alloc);
defer config.deinit();
if (config.loadFile(config._arena.?.allocator(), theme.path)) |_| {
if (!config._errors.empty()) {
try stderr.print(" Problems were encountered trying to load the theme:\n", .{});
for (config._errors.list.items) |err| {
try stderr.print(" {s}\n", .{err.message});
}
}
try stdout.print("\n ", .{});
for (0..8) |i| {
try stdout.print(" {d:2} \x1b[38;2;{d};{d};{d}m██\x1b[0m", .{
i,
config.palette.value[i].r,
config.palette.value[i].g,
config.palette.value[i].b,
});
}
try stdout.print("\n ", .{});
for (8..16) |i| {
try stdout.print(" {d:2} \x1b[38;2;{d};{d};{d}m██\x1b[0m", .{
i,
config.palette.value[i].r,
config.palette.value[i].g,
config.palette.value[i].b,
});
}
try stdout.print("\n\n", .{});
} else |err| {
try stderr.print("unable to load {s}: {}", .{ theme.path, err });
}
}
} }
if (count == 0) { if (count == 0) {