mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-07-14 15:56:13 +03:00
themes: allow loading from absolute paths and from user config dir
This commit is contained in:

committed by
Mitchell Hashimoto

parent
f7f55d716d
commit
7a11b22c5f
@ -5,6 +5,7 @@ const Action = @import("action.zig").Action;
|
||||
const Arena = std.heap.ArenaAllocator;
|
||||
const Allocator = std.mem.Allocator;
|
||||
const Config = @import("../config/Config.zig");
|
||||
const internal_os = @import("../os/main.zig");
|
||||
const global_state = &@import("../global.zig").state;
|
||||
|
||||
pub const Options = struct {
|
||||
@ -41,40 +42,76 @@ pub fn run(alloc: Allocator) !u8 {
|
||||
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 paths: []const struct {
|
||||
type: Config.ThemeDirType,
|
||||
dir: ?[]const u8,
|
||||
} = &.{
|
||||
.{
|
||||
.type = .user,
|
||||
.dir = Config.themeDir(alloc, .user),
|
||||
},
|
||||
.{
|
||||
.type = .system,
|
||||
.dir = Config.themeDir(alloc, .system),
|
||||
},
|
||||
};
|
||||
|
||||
const path = try std.fs.path.join(alloc, &.{ resources_dir, "themes" });
|
||||
defer alloc.free(path);
|
||||
const ThemeListElement = struct {
|
||||
type: Config.ThemeDirType,
|
||||
path: []const u8,
|
||||
theme: []const u8,
|
||||
fn deinit(self: *const @This(), alloc_: std.mem.Allocator) void {
|
||||
alloc_.free(self.path);
|
||||
alloc_.free(self.theme);
|
||||
}
|
||||
fn lessThan(_: void, lhs: @This(), rhs: @This()) bool {
|
||||
return std.ascii.orderIgnoreCase(lhs.theme, rhs.theme) == .lt;
|
||||
}
|
||||
};
|
||||
|
||||
var dir = try std.fs.cwd().openDir(path, .{ .iterate = true });
|
||||
defer dir.close();
|
||||
var count: usize = 0;
|
||||
|
||||
var walker = try dir.walk(alloc);
|
||||
defer walker.deinit();
|
||||
|
||||
var themes = std.ArrayList([]const u8).init(alloc);
|
||||
var themes = std.ArrayList(ThemeListElement).init(alloc);
|
||||
defer {
|
||||
for (themes.items) |v| alloc.free(v);
|
||||
for (themes.items) |v| v.deinit(alloc);
|
||||
themes.deinit();
|
||||
}
|
||||
|
||||
while (try walker.next()) |entry| {
|
||||
if (entry.kind != .file) continue;
|
||||
try themes.append(try alloc.dupe(u8, entry.basename));
|
||||
for (paths) |path| {
|
||||
if (path.dir) |p| {
|
||||
defer alloc.free(p);
|
||||
|
||||
var dir = try std.fs.cwd().openDir(p, .{ .iterate = true });
|
||||
defer dir.close();
|
||||
|
||||
var walker = try dir.walk(alloc);
|
||||
defer walker.deinit();
|
||||
|
||||
while (try walker.next()) |entry| {
|
||||
if (entry.kind != .file) continue;
|
||||
count += 1;
|
||||
try themes.append(.{
|
||||
.type = path.type,
|
||||
.path = try std.fs.path.join(alloc, &.{ p, entry.basename }),
|
||||
.theme = 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);
|
||||
std.mem.sortUnstable(ThemeListElement, themes.items, {}, ThemeListElement.lessThan);
|
||||
|
||||
for (themes.items) |theme| {
|
||||
try stdout.print("{s}\n", .{theme});
|
||||
try stdout.print("{s} ({s})\n", .{ theme.theme, @tagName(theme.type) });
|
||||
}
|
||||
|
||||
if (count == 0) {
|
||||
try stderr.print("No themes found, check to make sure that the themes were installed correctly.", .{});
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
@ -240,19 +240,29 @@ 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.
|
||||
///
|
||||
/// 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.
|
||||
@ -2174,40 +2184,107 @@ fn expandPaths(self: *Config, base: []const u8) !void {
|
||||
}
|
||||
}
|
||||
|
||||
pub const ThemeDirType = enum {
|
||||
user,
|
||||
system,
|
||||
};
|
||||
|
||||
pub fn themeDir(alloc: std.mem.Allocator, type_: ThemeDirType) ?[]const u8 {
|
||||
return switch (type_) {
|
||||
.user => internal_os.xdg.config(alloc, .{ .subdir = "ghostty/themes" }) catch null,
|
||||
.system => result: {
|
||||
const resources_dir = global_state.resources_dir orelse break :result null;
|
||||
break :result std.fs.path.join(alloc, &.{
|
||||
resources_dir,
|
||||
"themes",
|
||||
}) catch null;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
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",
|
||||
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 },
|
||||
),
|
||||
}),
|
||||
const file = file: {
|
||||
if (std.fs.path.isAbsolute(theme)) {
|
||||
// Theme is an absolute path, open that file or fail
|
||||
break :file std.fs.openFileAbsolute(theme, .{}) catch |err| switch (err) {
|
||||
error.FileNotFound => {
|
||||
try self._errors.add(alloc, .{
|
||||
.message = try std.fmt.allocPrintZ(
|
||||
alloc,
|
||||
"failed to load theme from the path \"{s}\"",
|
||||
.{theme},
|
||||
),
|
||||
});
|
||||
return;
|
||||
},
|
||||
else => {
|
||||
try self._errors.add(alloc, .{
|
||||
.message = try std.fmt.allocPrintZ(
|
||||
alloc,
|
||||
"failed to load theme from the path \"{s}\": {}",
|
||||
.{ theme, err },
|
||||
),
|
||||
});
|
||||
return;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// The theme is not an absolute path, search the user and system theme
|
||||
// directories
|
||||
|
||||
const dirs: []const struct {
|
||||
type: ThemeDirType,
|
||||
dir: ?[]const u8,
|
||||
} = &.{
|
||||
.{ .type = .user, .dir = themeDir(alloc, .user) },
|
||||
.{ .type = .system, .dir = themeDir(alloc, .system) },
|
||||
};
|
||||
|
||||
const cwd = std.fs.cwd();
|
||||
for (dirs) |dir| {
|
||||
if (dir.dir) |d| {
|
||||
const path = try std.fs.path.join(alloc, &.{
|
||||
d,
|
||||
theme,
|
||||
});
|
||||
if (cwd.openFile(path, .{})) |file| {
|
||||
break :file file;
|
||||
} else |err| switch (err) {
|
||||
error.FileNotFound => {},
|
||||
else => {
|
||||
try self._errors.add(alloc, .{
|
||||
.message = try std.fmt.allocPrintZ(
|
||||
alloc,
|
||||
"failed to load theme \"{s}\" from the file \"{s}\": {}",
|
||||
.{ theme, path, err },
|
||||
),
|
||||
});
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If we get here, no file was found with the theme. Log some errors
|
||||
// and bail.
|
||||
for (dirs) |dir| {
|
||||
if (dir.dir) |d| {
|
||||
try self._errors.add(alloc, .{
|
||||
.message = try std.fmt.allocPrintZ(
|
||||
alloc,
|
||||
"theme \"{s}\" not found, tried {s} path \"{s}\"",
|
||||
.{
|
||||
theme,
|
||||
@tagName(dir.type),
|
||||
d,
|
||||
},
|
||||
),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
};
|
||||
defer file.close();
|
||||
|
Reference in New Issue
Block a user