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
|
const std = @import("std");
const Allocator = std.mem.Allocator;
const ArenaAllocator = std.heap.ArenaAllocator;
const Action = @import("ghostty.zig").Action;
const args = @import("args.zig");
const diagnostics = @import("diagnostics.zig");
const font = @import("../font/main.zig");
const configpkg = @import("../config.zig");
const Config = configpkg.Config;
pub const Options = struct {
/// This is set by the CLI parser for deinit.
_arena: ?ArenaAllocator = null,
/// The codepoint to search for.
cp: ?u21 = null,
/// Search for all of the codepoints in the string.
string: ?[]const u8 = null,
/// Font style to search for.
style: font.Style = .regular,
/// If specified, force text or emoji presentation.
presentation: ?font.Presentation = null,
// Enable arg parsing diagnostics so that we don't get an error if
// there is a "normal" config setting on the cli.
_diagnostics: diagnostics.DiagnosticList = .{},
pub fn deinit(self: *Options) void {
if (self._arena) |arena| arena.deinit();
self.* = undefined;
}
/// Enables "-h" and "--help" to work.
pub fn help(self: Options) !void {
_ = self;
return Action.help_error;
}
};
/// The `show-face` command shows what font face Ghostty will use to render a
/// specific codepoint. Note that this command does not take into consideration
/// grapheme clustering or any other Unicode features that might modify the
/// presentation of a codepoint, so this may show a different font face than
/// Ghostty uses to render a codepoint in a terminal session.
///
/// Flags:
///
/// * `--cp`: Find the face for a single codepoint. The codepoint may be specified
/// in decimal (`--cp=65`), hexadecimal (`--cp=0x41`), octal (`--cp=0o101`), or
/// binary (`--cp=0b1000001`).
///
/// * `--string`: Find the face for all of the codepoints in a string. The
/// string must be a valid UTF-8 sequence.
///
/// * `--style`: Search for a specific style. Valid options are `regular`, `bold`,
/// `italic`, and `bold_italic`.
///
/// * `--presentation`: If set, force searching for a specific presentation
/// style. Valid options are `text` and `emoji`. If unset, the presentation
/// style of a codepoint will be inferred from the Unicode standard.
pub fn run(alloc: Allocator) !u8 {
var iter = try args.argsIterator(alloc);
defer iter.deinit();
var stdout_buffer: [1024]u8 = undefined;
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
const stdout = &stdout_writer.interface;
var stderr_buffer: [1024]u8 = undefined;
var stderr_writer = std.fs.File.stdout().writer(&stderr_buffer);
const stderr = &stderr_writer.interface;
const result = runArgs(
alloc,
&iter,
stdout,
stderr,
);
stdout.flush() catch {};
stderr.flush() catch {};
return result;
}
fn runArgs(
alloc_gpa: Allocator,
argsIter: anytype,
stdout: *std.Io.Writer,
stderr: *std.Io.Writer,
) !u8 {
// Its possible to build Ghostty without font discovery!
if (comptime font.Discover == void) {
try stderr.print(
\\Ghostty was built without a font discovery mechanism. This is a compile-time
\\option. Please review how Ghostty was built from source, contact the
\\maintainer to enable a font discovery mechanism, and try again.
,
.{},
);
return 1;
}
var opts: Options = .{};
defer opts.deinit();
args.parse(Options, alloc_gpa, &opts, argsIter) catch |err| switch (err) {
error.ActionHelpRequested => return err,
else => {
try stderr.print("Error parsing args: {}\n", .{err});
return 1;
},
};
// Print out any diagnostics, unless it's likely that the diagnostic was
// generated trying to parse a "normal" configuration setting. Exit with an
// error code if any diagnostics were printed.
if (!opts._diagnostics.empty()) {
var exit: bool = false;
outer: for (opts._diagnostics.items()) |diagnostic| {
if (diagnostic.location != .cli) continue :outer;
inner: inline for (@typeInfo(Options).@"struct".fields) |field| {
if (field.name[0] == '_') continue :inner;
if (std.mem.eql(u8, field.name, diagnostic.key)) {
try stderr.print("config error: {f}\n", .{diagnostic});
exit = true;
}
}
}
if (exit) return 1;
}
var arena = ArenaAllocator.init(alloc_gpa);
defer arena.deinit();
const alloc = arena.allocator();
if (opts.cp == null and opts.string == null) {
try stderr.print("You must specify a codepoint with --cp or a string with --string\n", .{});
return 1;
}
var config = Config.load(alloc) catch |err| {
try stderr.print("Unable to load config: {}", .{err});
return 1;
};
defer config.deinit();
// Print out any diagnostics generated from parsing the config, unless
// the diagnostic might have been generated because it's actually an
// action-specific argument.
if (!config._diagnostics.empty()) {
outer: for (config._diagnostics.items()) |diagnostic| {
inner: inline for (@typeInfo(Options).@"struct".fields) |field| {
if (field.name[0] == '_') continue :inner;
if (std.mem.eql(u8, field.name, diagnostic.key) and (diagnostic.location == .none or diagnostic.location == .cli)) continue :outer;
}
try stderr.print("config error: {f}\n", .{diagnostic});
}
}
var font_grid_set = font.SharedGridSet.init(alloc) catch |err| {
try stderr.print("Unable to initialize font grid set: {}", .{err});
return 1;
};
errdefer font_grid_set.deinit();
const font_size: font.face.DesiredSize = .{
.points = config.@"font-size",
.xdpi = 96,
.ydpi = 96,
};
var font_config = font.SharedGridSet.DerivedConfig.init(alloc, config) catch |err| {
try stderr.print("Unable to initialize font config: {}", .{err});
return 1;
};
const font_grid_key, const font_grid = font_grid_set.ref(
&font_config,
font_size,
) catch |err| {
try stderr.print("Unable to get font grid: {}", .{err});
return 1;
};
defer font_grid_set.deref(font_grid_key);
if (opts.cp) |cp| {
if (try lookup(alloc, stdout, stderr, font_grid, opts.style, opts.presentation, cp)) |rc| return rc;
}
if (opts.string) |string| {
const view = std.unicode.Utf8View.init(string) catch |err| {
try stderr.print("Unable to parse string as unicode: {}", .{err});
return 1;
};
var it = view.iterator();
while (it.nextCodepoint()) |cp| {
if (try lookup(alloc, stdout, stderr, font_grid, opts.style, opts.presentation, cp)) |rc| return rc;
}
}
return 0;
}
fn lookup(
alloc: std.mem.Allocator,
stdout: *std.Io.Writer,
stderr: *std.Io.Writer,
font_grid: *font.SharedGrid,
style: font.Style,
presentation: ?font.Presentation,
cp: u21,
) !?u8 {
const idx = font_grid.resolver.getIndex(alloc, cp, style, presentation) orelse {
try stdout.print("U+{0X:0>2} « {0u} » not found.\n", .{cp});
return null;
};
const face = font_grid.resolver.collection.getFace(idx) catch |err| switch (err) {
error.SpecialHasNoFace => {
try stdout.print("U+{0X:0>2} « {0u} » is handled by Ghostty's internal sprites.\n", .{cp});
return null;
},
else => {
try stderr.print("Unable to get face: {}", .{err});
return 1;
},
};
var buf: [1024]u8 = undefined;
const name = face.name(&buf) catch |err| {
try stderr.print("Unable to get name of face: {}", .{err});
return 1;
};
try stdout.print("U+{0X:0>2} « {0u} » found in face “{1s}”.\n", .{ cp, name });
return null;
}
|