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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
const std = @import("std");
const Config = @import("../config/Config.zig");
const Action = @import("../cli.zig").ghostty.Action;
/// A bash completions configuration that contains all the available commands
/// and options.
///
/// Notes: bash completion support for --<key>=<value> depends on setting the completion
/// system to _not_ print a space following each successful completion (see -o nospace).
/// This results leading or tailing spaces being necessary to move onto the next match.
///
/// bash completion will read = as it's own completiong word regardless of whether or not
/// it's part of an on going completion like --<key>=. Working around this requires looking
/// backward in the command line args to pretend the = is an empty string
/// see: https://www.gnu.org/software/gnuastro/manual/html_node/Bash-TAB-completion-tutorial.html
pub const completions = comptimeGenerateBashCompletions();
fn comptimeGenerateBashCompletions() []const u8 {
comptime {
@setEvalBranchQuota(50000);
var counter: std.Io.Writer.Discarding = .init(&.{});
try writeBashCompletions(&counter.writer);
var buf: [counter.count]u8 = undefined;
var writer: std.Io.Writer = .fixed(&buf);
try writeBashCompletions(&writer);
const final = buf;
return final[0..writer.end];
}
}
fn writeBashCompletions(writer: *std.Io.Writer) !void {
const pad1 = " ";
const pad2 = pad1 ++ pad1;
const pad3 = pad2 ++ pad1;
const pad4 = pad3 ++ pad1;
const pad5 = pad4 ++ pad1;
try writer.writeAll(
\\_ghostty() {
\\
\\ # -o nospace requires we add back a space when a completion is finished
\\ # and not part of a --key= completion
\\ _add_spaces() {
\\ for idx in "${!COMPREPLY[@]}"; do
\\ [ -n "${COMPREPLY[idx]}" ] && COMPREPLY[idx]="${COMPREPLY[idx]} ";
\\ done
\\ }
\\
\\ _fonts() {
\\ local IFS=$'\n'
\\ mapfile -t COMPREPLY < <( compgen -P '"' -S '"' -W "$($ghostty +list-fonts | grep '^[A-Z]' )" -- "$cur")
\\ }
\\
\\ _themes() {
\\ local IFS=$'\n'
\\ mapfile -t COMPREPLY < <( compgen -P '"' -S '"' -W "$($ghostty +list-themes | sed -E 's/^(.*) \(.*$/\1/')" -- "$cur")
\\ }
\\
\\ _files() {
\\ mapfile -t COMPREPLY < <( compgen -o filenames -f -- "$cur" )
\\ for i in "${!COMPREPLY[@]}"; do
\\ if [[ -d "${COMPREPLY[i]}" ]]; then
\\ COMPREPLY[i]="${COMPREPLY[i]}/";
\\ fi
\\ if [[ -f "${COMPREPLY[i]}" ]]; then
\\ COMPREPLY[i]="${COMPREPLY[i]} ";
\\ fi
\\ done
\\ }
\\
\\ _dirs() {
\\ mapfile -t COMPREPLY < <( compgen -o dirnames -d -- "$cur" )
\\ for i in "${!COMPREPLY[@]}"; do
\\ if [[ -d "${COMPREPLY[i]}" ]]; then
\\ COMPREPLY[i]="${COMPREPLY[i]}/";
\\ fi
\\ done
\\ if [[ "${#COMPREPLY[@]}" == 0 && -d "$cur" ]]; then
\\ COMPREPLY=( "$cur " )
\\ fi
\\ }
\\
\\ _handle_config() {
\\ local config="--help"
\\ config+=" --version"
\\
);
for (@typeInfo(Config).@"struct".fields) |field| {
if (field.name[0] == '_') continue;
switch (field.type) {
bool, ?bool => try writer.writeAll(pad2 ++ "config+=\" '--" ++ field.name ++ " '\"\n"),
else => try writer.writeAll(pad2 ++ "config+=\" --" ++ field.name ++ "=\"\n"),
}
}
try writer.writeAll(
\\
\\ case "$prev" in
\\
);
for (@typeInfo(Config).@"struct".fields) |field| {
if (field.name[0] == '_') continue;
try writer.writeAll(pad3 ++ "--" ++ field.name ++ ") ");
if (std.mem.startsWith(u8, field.name, "font-family"))
try writer.writeAll("_fonts ;;")
else if (std.mem.eql(u8, "theme", field.name))
try writer.writeAll("_themes ;;")
else if (std.mem.eql(u8, "working-directory", field.name))
try writer.writeAll("_dirs ;;")
else if (field.type == Config.RepeatablePath)
try writer.writeAll("_files ;;")
else {
const compgenPrefix = "mapfile -t COMPREPLY < <( compgen -W \"";
const compgenSuffix = "\" -- \"$cur\" ); _add_spaces ;;";
switch (@typeInfo(field.type)) {
.bool => try writer.writeAll("return ;;"),
.@"enum" => |info| {
try writer.writeAll(compgenPrefix);
for (info.fields, 0..) |f, i| {
if (i > 0) try writer.writeAll(" ");
try writer.writeAll(f.name);
}
try writer.writeAll(compgenSuffix);
},
.@"struct" => |info| {
if (!@hasDecl(field.type, "parseCLI") and info.layout == .@"packed") {
try writer.writeAll(compgenPrefix);
for (info.fields, 0..) |f, i| {
if (i > 0) try writer.writeAll(" ");
try writer.writeAll(f.name ++ " no-" ++ f.name);
}
try writer.writeAll(compgenSuffix);
} else {
try writer.writeAll("return ;;");
}
},
else => try writer.writeAll("return ;;"),
}
}
try writer.writeAll("\n");
}
try writer.writeAll(
\\ *) mapfile -t COMPREPLY < <( compgen -W "$config" -- "$cur" ) ;;
\\ esac
\\
\\ return 0
\\ }
\\
\\ _handle_actions() {
\\
);
for (@typeInfo(Action).@"enum".fields) |field| {
if (std.mem.eql(u8, "help", field.name)) continue;
if (std.mem.eql(u8, "version", field.name)) continue;
const options = @field(Action, field.name).options();
// assumes options will never be created with only <_name> members
if (@typeInfo(options).@"struct".fields.len == 0) continue;
var buffer: [field.name.len]u8 = undefined;
const bashName: []u8 = buffer[0..field.name.len];
@memcpy(bashName, field.name);
std.mem.replaceScalar(u8, bashName, '-', '_');
try writer.writeAll(pad2 ++ "local " ++ bashName ++ "=\"");
{
var count = 0;
for (@typeInfo(options).@"struct".fields) |opt| {
if (opt.name[0] == '_') continue;
if (count > 0) try writer.writeAll(" ");
switch (opt.type) {
bool, ?bool => try writer.writeAll("'--" ++ opt.name ++ " '"),
else => try writer.writeAll("--" ++ opt.name ++ "="),
}
count += 1;
}
}
try writer.writeAll(" --help\"\n");
}
try writer.writeAll(
\\
\\ case "${COMP_WORDS[1]}" in
\\
);
for (@typeInfo(Action).@"enum".fields) |field| {
if (std.mem.eql(u8, "help", field.name)) continue;
if (std.mem.eql(u8, "version", field.name)) continue;
const options = @field(Action, field.name).options();
if (@typeInfo(options).@"struct".fields.len == 0) continue;
// bash doesn't allow variable names containing '-' so replace them
var buffer: [field.name.len]u8 = undefined;
const bashName: []u8 = buffer[0..field.name.len];
_ = std.mem.replace(u8, field.name, "-", "_", bashName);
try writer.writeAll(pad3 ++ "+" ++ field.name ++ ")\n");
try writer.writeAll(pad4 ++ "case $prev in\n");
for (@typeInfo(options).@"struct".fields) |opt| {
if (opt.name[0] == '_') continue;
try writer.writeAll(pad5 ++ "--" ++ opt.name ++ ") ");
const compgenPrefix = "mapfile -t COMPREPLY < <( compgen -W \"";
const compgenSuffix = "\" -- \"$cur\" ); _add_spaces ;;";
switch (@typeInfo(opt.type)) {
.bool => try writer.writeAll("return ;;"),
.@"enum" => |info| {
try writer.writeAll(compgenPrefix);
for (info.fields, 0..) |f, i| {
if (i > 0) try writer.writeAll(" ");
try writer.writeAll(f.name);
}
try writer.writeAll(compgenSuffix);
},
.optional => |optional| {
switch (@typeInfo(optional.child)) {
.@"enum" => |info| {
try writer.writeAll(compgenPrefix);
for (info.fields, 0..) |f, i| {
if (i > 0) try writer.writeAll(" ");
try writer.writeAll(f.name);
}
try writer.writeAll(compgenSuffix);
},
else => {
if (std.mem.eql(u8, "config-file", opt.name)) {
try writer.writeAll("return ;;");
} else try writer.writeAll("return;;");
},
}
},
else => {
if (std.mem.eql(u8, "config-file", opt.name)) {
try writer.writeAll("_files ;;");
} else try writer.writeAll("return;;");
},
}
try writer.writeAll("\n");
}
try writer.writeAll(pad5 ++ "*) mapfile -t COMPREPLY < <( compgen -W \"$" ++ bashName ++ "\" -- \"$cur\" ) ;;\n");
try writer.writeAll(
\\ esac
\\ ;;
\\
);
}
try writer.writeAll(
\\ *) mapfile -t COMPREPLY < <( compgen -W "--help" -- "$cur" ) ;;
\\ esac
\\
\\ return 0
\\ }
\\
\\ # begin main logic
\\ local topLevel="-e"
\\ topLevel+=" --help"
\\ topLevel+=" --version"
\\
);
for (@typeInfo(Action).@"enum".fields) |field| {
if (std.mem.eql(u8, "help", field.name)) continue;
if (std.mem.eql(u8, "version", field.name)) continue;
try writer.writeAll(pad1 ++ "topLevel+=\" +" ++ field.name ++ "\"\n");
}
try writer.writeAll(
\\
\\ local cur=""; local prev=""; local prevWasEq=false; COMPREPLY=()
\\ local ghostty="$1"
\\
\\ # script assumes default COMP_WORDBREAKS of roughly $' \t\n"\'><=;|&(:'
\\ # if = is missing this script will degrade to matching on keys only.
\\ # eg: --key=
\\ # this can be improved if needed see: https://github.com/ghostty-org/ghostty/discussions/2994
\\
\\ if [ "$2" = "=" ]; then cur=""
\\ else cur="$2"
\\ fi
\\
\\ if [ "$3" = "=" ]; then prev="${COMP_WORDS[COMP_CWORD-2]}"; prevWasEq=true;
\\ else prev="${COMP_WORDS[COMP_CWORD-1]}"
\\ fi
\\
\\ # current completion is double quoted add a space so the curor progresses
\\ if [[ "$2" == \"*\" ]]; then
\\ COMPREPLY=( "$cur " );
\\ return;
\\ fi
\\
\\ case "$COMP_CWORD" in
\\ 1)
\\ case "${COMP_WORDS[1]}" in
\\ -e | --help | --version) return 0 ;;
\\ --*) _handle_config ;;
\\ *) mapfile -t COMPREPLY < <( compgen -W "${topLevel}" -- "$cur" ); _add_spaces ;;
\\ esac
\\ ;;
\\ *)
\\ case "$prev" in
\\ -e | --help | --version) return 0 ;;
\\ *)
\\ if [[ "=" != "${COMP_WORDS[COMP_CWORD]}" && $prevWasEq != true ]]; then
\\ # must be completing with a space after the key eg: '--<key> '
\\ # clear out prev so we don't run any of the key specific completions
\\ prev=""
\\ fi
\\
\\ case "${COMP_WORDS[1]}" in
\\ --*) _handle_config ;;
\\ +*) _handle_actions ;;
\\ esac
\\ ;;
\\ esac
\\ ;;
\\ esac
\\
\\ return 0
\\}
\\
\\complete -o nospace -o bashdefault -F _ghostty ghostty
\\
);
}
|