mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-07-14 15:56:13 +03:00
@ -2,12 +2,18 @@ const std = @import("std");
|
||||
const inputpkg = @import("../input.zig");
|
||||
const args = @import("args.zig");
|
||||
const Action = @import("action.zig").Action;
|
||||
const Arena = std.heap.ArenaAllocator;
|
||||
const Allocator = std.mem.Allocator;
|
||||
const Config = @import("../config/Config.zig");
|
||||
const themepkg = @import("../config/theme.zig");
|
||||
const internal_os = @import("../os/main.zig");
|
||||
const global_state = &@import("../global.zig").state;
|
||||
|
||||
pub const Options = struct {
|
||||
/// If true, print the full path to the theme.
|
||||
path: bool = false,
|
||||
|
||||
/// If true, show a small preview of the theme.
|
||||
preview: bool = false,
|
||||
|
||||
pub fn deinit(self: Options) void {
|
||||
_ = self;
|
||||
}
|
||||
@ -22,59 +28,132 @@ pub const Options = struct {
|
||||
/// 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/ghostty`. If you're compiling from source, this is the `zig-out/share/
|
||||
/// ghostty` 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 {
|
||||
/// Two different directories will be searched for themes.
|
||||
///
|
||||
/// The first directory is the `themes` subdirectory of your Ghostty
|
||||
/// configuration directory. This is `$XDG_CONFIG_DIR/ghostty/themes` or
|
||||
/// `~/.config/ghostty/themes`.
|
||||
///
|
||||
/// 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:
|
||||
///
|
||||
/// * `--path`: Show the full path to the theme.
|
||||
/// * `--preview`: Show a short preview of the theme colors.
|
||||
pub fn run(gpa_alloc: std.mem.Allocator) !u8 {
|
||||
var opts: Options = .{};
|
||||
defer opts.deinit();
|
||||
|
||||
{
|
||||
var iter = try std.process.argsWithAllocator(alloc);
|
||||
var iter = try std.process.argsWithAllocator(gpa_alloc);
|
||||
defer iter.deinit();
|
||||
try args.parse(Options, alloc, &opts, &iter);
|
||||
try args.parse(Options, gpa_alloc, &opts, &iter);
|
||||
}
|
||||
|
||||
var arena = std.heap.ArenaAllocator.init(gpa_alloc);
|
||||
const alloc = arena.allocator();
|
||||
|
||||
const stderr = std.io.getStdErr().writer();
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
|
||||
const resources_dir = global_state.resources_dir orelse {
|
||||
if (global_state.resources_dir == null)
|
||||
try stderr.print("Could not find the Ghostty resources directory. Please ensure " ++
|
||||
"that Ghostty is installed correctly.\n", .{});
|
||||
return 1;
|
||||
|
||||
const ThemeListElement = struct {
|
||||
location: themepkg.Location,
|
||||
path: []const u8,
|
||||
theme: []const u8,
|
||||
fn lessThan(_: void, lhs: @This(), rhs: @This()) bool {
|
||||
// TODO: use Unicode-aware comparison
|
||||
return std.ascii.orderIgnoreCase(lhs.theme, rhs.theme) == .lt;
|
||||
}
|
||||
};
|
||||
|
||||
const path = try std.fs.path.join(alloc, &.{ resources_dir, "themes" });
|
||||
defer alloc.free(path);
|
||||
var count: usize = 0;
|
||||
|
||||
var dir = try std.fs.cwd().openDir(path, .{ .iterate = true });
|
||||
var themes = std.ArrayList(ThemeListElement).init(alloc);
|
||||
|
||||
var it = themepkg.LocationIterator{ .arena_alloc = arena.allocator() };
|
||||
|
||||
while (try it.next()) |loc| {
|
||||
var dir = std.fs.cwd().openDir(loc.dir, .{ .iterate = true }) catch |err| switch (err) {
|
||||
error.FileNotFound => continue,
|
||||
else => {
|
||||
std.debug.print("error trying to open {s}: {}\n", .{ loc.dir, err });
|
||||
continue;
|
||||
},
|
||||
};
|
||||
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();
|
||||
}
|
||||
var walker = dir.iterate();
|
||||
|
||||
while (try walker.next()) |entry| {
|
||||
if (entry.kind != .file) continue;
|
||||
try themes.append(try alloc.dupe(u8, entry.basename));
|
||||
count += 1;
|
||||
try themes.append(.{
|
||||
.location = loc.location,
|
||||
.path = try std.fs.path.join(alloc, &.{ loc.dir, entry.name }),
|
||||
.theme = try alloc.dupe(u8, entry.name),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
std.mem.sortUnstable(ThemeListElement, themes.items, {}, ThemeListElement.lessThan);
|
||||
|
||||
for (themes.items) |theme| {
|
||||
try stdout.print("{s}\n", .{theme});
|
||||
if (opts.path)
|
||||
try stdout.print("{s} ({s}) {s}\n", .{ theme.theme, @tagName(theme.location), theme.path })
|
||||
else
|
||||
try stdout.print("{s} ({s})\n", .{ theme.theme, @tagName(theme.location) });
|
||||
|
||||
if (opts.preview) {
|
||||
var config = try Config.default(gpa_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) {
|
||||
try stderr.print("No themes found, check to make sure that the themes were installed correctly.", .{});
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
@ -24,6 +24,7 @@ const cli = @import("../cli.zig");
|
||||
const Command = @import("../Command.zig");
|
||||
|
||||
const formatterpkg = @import("formatter.zig");
|
||||
const themepkg = @import("theme.zig");
|
||||
const url = @import("url.zig");
|
||||
const Key = @import("key.zig").Key;
|
||||
const KeyValue = @import("key.zig").Value;
|
||||
@ -240,19 +241,30 @@ const c = @cImport({
|
||||
/// terminals. Only new terminals will use the new configuration.
|
||||
@"grapheme-width-method": GraphemeWidthMethod = .unicode,
|
||||
|
||||
/// A named theme to use. The available themes are currently hardcoded to the
|
||||
/// themes that ship with Ghostty. On macOS, this list is in the `Ghostty.app/
|
||||
/// Contents/Resources/ghostty/themes` directory. On Linux, this list is in the
|
||||
/// `share/ghostty/themes` directory (wherever you installed the Ghostty "share"
|
||||
/// A theme to use. If the theme is an absolute pathname, Ghostty will attempt
|
||||
/// to load that file as a theme. If that file does not exist or is inaccessible,
|
||||
/// an error will be logged and no other directories will be searched.
|
||||
///
|
||||
/// If the theme is not an absolute pathname, two different directories will be
|
||||
/// searched for a file name that matches the theme. This is case sensitive on
|
||||
/// systems with case-sensitive filesystems. It is an error for a theme name to
|
||||
/// include path separators unless it is an absolute pathname.
|
||||
///
|
||||
/// The first directory is the `themes` subdirectory of your Ghostty
|
||||
/// configuration directory. This is `$XDG_CONFIG_DIR/ghostty/themes` or
|
||||
/// `~/.config/ghostty/themes`.
|
||||
///
|
||||
/// 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 list is in the `Ghostty.app/Contents/
|
||||
/// Resources/ghostty/themes` directory. On Linux, this 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.
|
||||
///
|
||||
/// A future update will allow custom themes to be installed in certain
|
||||
/// directories.
|
||||
theme: ?[]const u8 = null,
|
||||
|
||||
/// Background color for the window.
|
||||
@ -2175,41 +2187,12 @@ fn expandPaths(self: *Config, base: []const u8) !void {
|
||||
}
|
||||
|
||||
fn loadTheme(self: *Config, theme: []const u8) !void {
|
||||
const alloc = self._arena.?.allocator();
|
||||
const resources_dir = global_state.resources_dir orelse {
|
||||
try self._errors.add(alloc, .{
|
||||
.message = "no resources directory found, themes will not work",
|
||||
});
|
||||
return;
|
||||
};
|
||||
|
||||
const path = try std.fs.path.join(alloc, &.{
|
||||
resources_dir,
|
||||
"themes",
|
||||
// Find our theme file and open it. See the open function for details.
|
||||
const file: std.fs.File = (try themepkg.open(
|
||||
self._arena.?.allocator(),
|
||||
theme,
|
||||
});
|
||||
|
||||
const cwd = std.fs.cwd();
|
||||
var file = cwd.openFile(path, .{}) catch |err| {
|
||||
switch (err) {
|
||||
error.FileNotFound => try self._errors.add(alloc, .{
|
||||
.message = try std.fmt.allocPrintZ(
|
||||
alloc,
|
||||
"theme \"{s}\" not found, path={s}",
|
||||
.{ theme, path },
|
||||
),
|
||||
}),
|
||||
|
||||
else => try self._errors.add(alloc, .{
|
||||
.message = try std.fmt.allocPrintZ(
|
||||
alloc,
|
||||
"failed to load theme \"{s}\": {}",
|
||||
.{ theme, err },
|
||||
),
|
||||
}),
|
||||
}
|
||||
return;
|
||||
};
|
||||
&self._errors,
|
||||
)) orelse return;
|
||||
defer file.close();
|
||||
|
||||
// From this point onwards, we load the theme and do a bit of a dance
|
||||
|
211
src/config/theme.zig
Normal file
211
src/config/theme.zig
Normal file
@ -0,0 +1,211 @@
|
||||
const std = @import("std");
|
||||
const builtin = @import("builtin");
|
||||
const assert = std.debug.assert;
|
||||
const Allocator = std.mem.Allocator;
|
||||
const global_state = &@import("../global.zig").state;
|
||||
const internal_os = @import("../os/main.zig");
|
||||
const ErrorList = @import("ErrorList.zig");
|
||||
|
||||
/// Location of possible themes. The order of this enum matters because it
|
||||
/// defines the priority of theme search (from top to bottom).
|
||||
pub const Location = enum {
|
||||
user, // XDG config dir
|
||||
resources, // Ghostty resources dir
|
||||
|
||||
/// Returns the directory for the given theme based on this location type.
|
||||
///
|
||||
/// This will return null with no error if the directory type doesn't exist
|
||||
/// or is invalid for any reason. For example, it is perfectly valid to
|
||||
/// install and run Ghostty without the resources directory.
|
||||
///
|
||||
/// Due to the way allocations are handled, an Arena allocator (or another
|
||||
/// similar allocator implementation) should be used. It may not be safe to
|
||||
/// free the returned allocations.
|
||||
pub fn dir(
|
||||
self: Location,
|
||||
arena_alloc: Allocator,
|
||||
) error{OutOfMemory}!?[]const u8 {
|
||||
return switch (self) {
|
||||
.user => user: {
|
||||
const subdir = std.fs.path.join(arena_alloc, &.{
|
||||
"ghostty", "themes",
|
||||
}) catch return error.OutOfMemory;
|
||||
|
||||
break :user internal_os.xdg.config(
|
||||
arena_alloc,
|
||||
.{ .subdir = subdir },
|
||||
) catch |err| {
|
||||
// We need to do some comptime tricks to get the right
|
||||
// error set since some platforms don't support some
|
||||
// error types.
|
||||
const Error = @TypeOf(err) || switch (builtin.os.tag) {
|
||||
.ios => error{BufferTooSmall},
|
||||
else => error{},
|
||||
};
|
||||
|
||||
switch (@as(Error, err)) {
|
||||
error.OutOfMemory => return error.OutOfMemory,
|
||||
error.BufferTooSmall => return error.OutOfMemory,
|
||||
|
||||
// Any other error we treat as the XDG directory not
|
||||
// existing. Windows in particularly can return a LOT
|
||||
// of errors here.
|
||||
else => return null,
|
||||
}
|
||||
};
|
||||
},
|
||||
|
||||
.resources => try std.fs.path.join(arena_alloc, &.{
|
||||
global_state.resources_dir orelse return null,
|
||||
"themes",
|
||||
}),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
/// An iterator that returns all possible directories for finding themes in
|
||||
/// order of priority.
|
||||
pub const LocationIterator = struct {
|
||||
/// Due to the way allocations are handled, an Arena allocator (or another
|
||||
/// similar allocator implementation) should be used. It may not be safe to
|
||||
/// free the returned allocations.
|
||||
arena_alloc: Allocator,
|
||||
i: usize = 0,
|
||||
|
||||
pub fn next(self: *LocationIterator) !?struct {
|
||||
location: Location,
|
||||
dir: []const u8,
|
||||
} {
|
||||
const max = @typeInfo(Location).Enum.fields.len;
|
||||
while (self.i < max) {
|
||||
const location: Location = @enumFromInt(self.i);
|
||||
self.i += 1;
|
||||
if (try location.dir(self.arena_alloc)) |dir|
|
||||
return .{
|
||||
.location = location,
|
||||
.dir = dir,
|
||||
};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
pub fn reset(self: *LocationIterator) void {
|
||||
self.i = 0;
|
||||
}
|
||||
};
|
||||
|
||||
/// Open the given named theme. If there are any errors then messages
|
||||
/// will be appended to the given error list and null is returned. If
|
||||
/// a non-null return value is returned, there are never any errors added.
|
||||
///
|
||||
/// One error that is not recoverable and may be returned is OOM. This is
|
||||
/// always a critical error for configuration loading so it is returned.
|
||||
///
|
||||
/// Due to the way allocations are handled, an Arena allocator (or another
|
||||
/// similar allocator implementation) should be used. It may not be safe to
|
||||
/// free the returned allocations.
|
||||
pub fn open(
|
||||
arena_alloc: Allocator,
|
||||
theme: []const u8,
|
||||
errors: *ErrorList,
|
||||
) error{OutOfMemory}!?std.fs.File {
|
||||
|
||||
// Absolute themes are loaded a different path.
|
||||
if (std.fs.path.isAbsolute(theme)) return try openAbsolute(
|
||||
arena_alloc,
|
||||
theme,
|
||||
errors,
|
||||
);
|
||||
|
||||
const basename = std.fs.path.basename(theme);
|
||||
if (!std.mem.eql(u8, theme, basename)) {
|
||||
try errors.add(arena_alloc, .{
|
||||
.message = try std.fmt.allocPrintZ(
|
||||
arena_alloc,
|
||||
"theme \"{s}\" cannot include path separators unless it is an absolute path",
|
||||
.{theme},
|
||||
),
|
||||
});
|
||||
return null;
|
||||
}
|
||||
|
||||
// Iterate over the possible locations to try to find the
|
||||
// one that exists.
|
||||
var it: LocationIterator = .{ .arena_alloc = arena_alloc };
|
||||
const cwd = std.fs.cwd();
|
||||
while (try it.next()) |loc| {
|
||||
const path = try std.fs.path.join(arena_alloc, &.{ loc.dir, theme });
|
||||
if (cwd.openFile(path, .{})) |file| {
|
||||
return file;
|
||||
} else |err| switch (err) {
|
||||
// Not an error, just continue to the next location.
|
||||
error.FileNotFound => {},
|
||||
|
||||
// Anything else is an error we log and give up on.
|
||||
else => {
|
||||
try errors.add(arena_alloc, .{
|
||||
.message = try std.fmt.allocPrintZ(
|
||||
arena_alloc,
|
||||
"failed to load theme \"{s}\" from the file \"{s}\": {}",
|
||||
.{ theme, path, err },
|
||||
),
|
||||
});
|
||||
|
||||
return null;
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Unlikely scenario: the theme doesn't exist. In this case, we reset
|
||||
// our iterator, reiterate over in order to build a better error message.
|
||||
// This does double allocate some memory but for errors I think thats
|
||||
// fine.
|
||||
it.reset();
|
||||
while (try it.next()) |loc| {
|
||||
const path = try std.fs.path.join(arena_alloc, &.{ loc.dir, theme });
|
||||
try errors.add(arena_alloc, .{
|
||||
.message = try std.fmt.allocPrintZ(
|
||||
arena_alloc,
|
||||
"theme \"{s}\" not found, tried path \"{s}\"",
|
||||
.{ theme, path },
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Open the given theme from an absolute path. If there are any errors
|
||||
/// then messages will be appended to the given error list and null is
|
||||
/// returned. If a non-null return value is returned, there are never any
|
||||
/// errors added.
|
||||
///
|
||||
/// Due to the way allocations are handled, an Arena allocator (or another
|
||||
/// similar allocator implementation) should be used. It may not be safe to
|
||||
/// free the returned allocations.
|
||||
pub fn openAbsolute(
|
||||
arena_alloc: Allocator,
|
||||
theme: []const u8,
|
||||
errors: *ErrorList,
|
||||
) error{OutOfMemory}!?std.fs.File {
|
||||
return std.fs.openFileAbsolute(theme, .{}) catch |err| {
|
||||
switch (err) {
|
||||
error.FileNotFound => try errors.add(arena_alloc, .{
|
||||
.message = try std.fmt.allocPrintZ(
|
||||
arena_alloc,
|
||||
"failed to load theme from the path \"{s}\"",
|
||||
.{theme},
|
||||
),
|
||||
}),
|
||||
else => try errors.add(arena_alloc, .{
|
||||
.message = try std.fmt.allocPrintZ(
|
||||
arena_alloc,
|
||||
"failed to load theme from the path \"{s}\": {}",
|
||||
.{ theme, err },
|
||||
),
|
||||
}),
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user