1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
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 cli = @import("../cli.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.app() 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.
///
/// This will never return anything other than a handle to a regular file. If
/// the theme resolves to something other than a regular file a diagnostic entry
/// will be added to the list and null will be returned.
pub fn open(
arena_alloc: Allocator,
theme: []const u8,
diags: *cli.DiagnosticList,
) error{OutOfMemory}!?struct {
path: []const u8,
file: std.fs.File,
} {
// Absolute themes are loaded a different path.
if (std.fs.path.isAbsolute(theme)) {
const file: std.fs.File = try openAbsolute(
arena_alloc,
theme,
diags,
) orelse return null;
const stat = file.stat() catch |err| {
try diags.append(arena_alloc, .{
.message = try std.fmt.allocPrintSentinel(
arena_alloc,
"not reading theme from \"{s}\": {}",
.{ theme, err },
0,
),
});
return null;
};
switch (stat.kind) {
.file => {},
else => {
try diags.append(arena_alloc, .{
.message = try std.fmt.allocPrintSentinel(
arena_alloc,
"not reading theme from \"{s}\": it is a {s}",
.{ theme, @tagName(stat.kind) },
0,
),
});
return null;
},
}
return .{ .path = theme, .file = file };
}
const basename = std.fs.path.basename(theme);
if (!std.mem.eql(u8, theme, basename)) {
try diags.append(arena_alloc, .{
.message = try std.fmt.allocPrintSentinel(
arena_alloc,
"theme \"{s}\" cannot include path separators unless it is an absolute path",
.{theme},
0,
),
});
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| {
const stat = file.stat() catch |err| {
try diags.append(arena_alloc, .{
.message = try std.fmt.allocPrintSentinel(
arena_alloc,
"not reading theme from \"{s}\": {}",
.{ theme, err },
0,
),
});
return null;
};
switch (stat.kind) {
.file => {},
else => {
try diags.append(arena_alloc, .{
.message = try std.fmt.allocPrintSentinel(
arena_alloc,
"not reading theme from \"{s}\": it is a {s}",
.{ theme, @tagName(stat.kind) },
0,
),
});
return null;
},
}
return .{
.path = path,
.file = 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 diags.append(arena_alloc, .{
.message = try std.fmt.allocPrintSentinel(
arena_alloc,
"failed to load theme \"{s}\" from the file \"{s}\": {}",
.{ theme, path, err },
0,
),
});
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 diags.append(arena_alloc, .{
.message = try std.fmt.allocPrintSentinel(
arena_alloc,
"theme \"{s}\" not found, tried path \"{s}\"",
.{ theme, path },
0,
),
});
}
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,
diags: *cli.DiagnosticList,
) error{OutOfMemory}!?std.fs.File {
return std.fs.openFileAbsolute(theme, .{}) catch |err| {
switch (err) {
error.FileNotFound => try diags.append(arena_alloc, .{
.message = try std.fmt.allocPrintSentinel(
arena_alloc,
"failed to load theme from the path \"{s}\"",
.{theme},
0,
),
}),
else => try diags.append(arena_alloc, .{
.message = try std.fmt.allocPrintSentinel(
arena_alloc,
"failed to load theme from the path \"{s}\": {}",
.{ theme, err },
0,
),
}),
}
return null;
};
}
|