From f25a136ea5e576f414f65d03959dfe9cf0f4fbe3 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 22 Nov 2023 21:50:16 -0800 Subject: [PATCH] cli: +list-themes --- src/cli/action.zig | 5 +++ src/cli/list_themes.zig | 74 +++++++++++++++++++++++++++++++++++++++++ src/config/Config.zig | 2 ++ 3 files changed, 81 insertions(+) create mode 100644 src/cli/list_themes.zig diff --git a/src/cli/action.zig b/src/cli/action.zig index 38cc1fbd7..1297d640b 100644 --- a/src/cli/action.zig +++ b/src/cli/action.zig @@ -4,6 +4,7 @@ const Allocator = std.mem.Allocator; const list_fonts = @import("list_fonts.zig"); const version = @import("version.zig"); const list_keybinds = @import("list_keybinds.zig"); +const list_themes = @import("list_themes.zig"); /// Special commands that can be invoked via CLI flags. These are all /// invoked by using `+` as a CLI flag. The only exception is @@ -18,6 +19,9 @@ pub const Action = enum { /// List available keybinds @"list-keybinds", + /// List available themes + @"list-themes", + pub const Error = error{ /// Multiple actions were detected. You can specify at most one /// action on the CLI otherwise the behavior desired is ambiguous. @@ -57,6 +61,7 @@ pub const Action = enum { .version => try version.run(), .@"list-fonts" => try list_fonts.run(alloc), .@"list-keybinds" => try list_keybinds.run(alloc), + .@"list-themes" => try list_themes.run(alloc), }; } }; diff --git a/src/cli/list_themes.zig b/src/cli/list_themes.zig new file mode 100644 index 000000000..bf763c5bf --- /dev/null +++ b/src/cli/list_themes.zig @@ -0,0 +1,74 @@ +const std = @import("std"); +const inputpkg = @import("../input.zig"); +const args = @import("args.zig"); +const Arena = std.heap.ArenaAllocator; +const Allocator = std.mem.Allocator; +const Config = @import("../config/Config.zig"); +const global_state = &@import("../main.zig").state; + +pub const Options = struct { + pub fn deinit(self: Options) void { + _ = self; + } +}; + +/// The "list-themes" command is used to list all the available themes +/// for Ghostty. +/// +/// Themes require that Ghostty have access to the resources directory. +/// On macOS this is embedded in the app bundle. On Linux, this is usually +/// in `/usr/share`. If you're compiling from source, this is the +/// `zig-out/share` directory. You can also set the `GHOSTTY_RESOURCES_DIR` +/// environment variable to point to the resources directory. Themes +/// live in the `themes` subdirectory of the resources directory. +pub fn run(alloc: Allocator) !u8 { + var opts: Options = .{}; + defer opts.deinit(); + + { + var iter = try std.process.argsWithAllocator(alloc); + defer iter.deinit(); + try args.parse(Options, alloc, &opts, &iter); + } + + const stderr = std.io.getStdErr().writer(); + const stdout = std.io.getStdOut().writer(); + + const resources_dir = global_state.resources_dir orelse { + try stderr.print("Could not find the Ghostty resources directory. Please ensure " ++ + "that Ghostty is installed correctly.\n", .{}); + return 1; + }; + + const path = try std.fs.path.join(alloc, &.{ resources_dir, "themes" }); + defer alloc.free(path); + + var dir = try std.fs.cwd().openIterableDir(path, .{}); + defer dir.close(); + + var walker = try dir.walk(alloc); + defer walker.deinit(); + + var themes = std.ArrayList([]const u8).init(alloc); + defer { + for (themes.items) |v| alloc.free(v); + themes.deinit(); + } + + while (try walker.next()) |entry| { + if (entry.kind != .file) continue; + try themes.append(try alloc.dupe(u8, entry.basename)); + } + + std.mem.sortUnstable([]const u8, themes.items, {}, struct { + fn lessThan(_: void, lhs: []const u8, rhs: []const u8) bool { + return std.ascii.orderIgnoreCase(lhs, rhs) == .lt; + } + }.lessThan); + + for (themes.items) |theme| { + try stdout.print("{s}\n", .{theme}); + } + + return 0; +} diff --git a/src/config/Config.zig b/src/config/Config.zig index c6fc0a893..a28bb2d2d 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -160,6 +160,8 @@ const c = @cImport({ /// list is in the `share/ghostty/themes` directory (wherever you installed /// the Ghostty "share" directory. /// +/// To see a list of available themes, run `ghostty +list-themes`. +/// /// Any additional colors specified via background, foreground, palette, /// etc. will override the colors specified in the theme. ///