Skip to content

Commit 2ee1f31

Browse files
author
Denys Zhak
committed
feat: add descriptions to fish shell completions
1 parent 848ee92 commit 2ee1f31

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub fn build(b: *std.Build) !void {
5555
);
5656

5757
// Ghostty resources like terminfo, shell integration, themes, etc.
58-
const resources = try buildpkg.GhosttyResources.init(b, &config);
58+
const resources = try buildpkg.GhosttyResources.init(b, &config, &deps);
5959
const i18n = if (config.i18n) try buildpkg.GhosttyI18n.init(b, &config) else null;
6060

6161
// Ghostty executable, the actual runnable Ghostty program.

src/build/GhosttyResources.zig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ const assert = std.debug.assert;
66
const buildpkg = @import("main.zig");
77
const Config = @import("Config.zig");
88
const RunStep = std.Build.Step.Run;
9+
const SharedDeps = @import("SharedDeps.zig");
910

1011
steps: []*std.Build.Step,
1112

12-
pub fn init(b: *std.Build, cfg: *const Config) !GhosttyResources {
13+
pub fn init(b: *std.Build, cfg: *const Config, deps: *const SharedDeps) !GhosttyResources {
1314
var steps: std.ArrayList(*std.Build.Step) = .empty;
1415
errdefer steps.deinit(b.allocator);
1516

@@ -26,6 +27,8 @@ pub fn init(b: *std.Build, cfg: *const Config) !GhosttyResources {
2627
});
2728
build_data_exe.linkLibC();
2829

30+
deps.help_strings.addImport(build_data_exe);
31+
2932
// Terminfo
3033
terminfo: {
3134
const os_tag = cfg.target.result.os.tag;

src/extra/fish.zig

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const std = @import("std");
33

44
const Config = @import("../config/Config.zig");
55
const Action = @import("../cli.zig").ghostty.Action;
6+
const help_strings = @import("help_strings");
67

78
/// A fish completions configuration that contains all the available commands
89
/// and options.
@@ -81,6 +82,15 @@ fn writeCompletions(writer: *std.Io.Writer) !void {
8182
else => {},
8283
}
8384
}
85+
86+
if (@hasDecl(help_strings.Config, field.name)) {
87+
const help = @field(help_strings.Config, field.name);
88+
const desc = getDescription(help);
89+
try writer.writeAll(" -d \"");
90+
try writer.writeAll(desc);
91+
try writer.writeAll("\"");
92+
}
93+
8494
try writer.writeAll("\n");
8595
}
8696

@@ -143,3 +153,42 @@ fn writeCompletions(writer: *std.Io.Writer) !void {
143153
}
144154
}
145155
}
156+
157+
fn getDescription(comptime help: []const u8) []const u8 {
158+
var out: [help.len * 2]u8 = undefined;
159+
var len: usize = 0;
160+
var prev_was_space = false;
161+
162+
for (help, 0..) |c, i| {
163+
switch (c) {
164+
'.' => {
165+
out[len] = '.';
166+
len += 1;
167+
168+
if (i + 1 >= help.len) break;
169+
const next = help[i + 1];
170+
if (next == ' ' or next == '\n') break;
171+
},
172+
'\n' => {
173+
if (!prev_was_space and len > 0) {
174+
out[len] = ' ';
175+
len += 1;
176+
prev_was_space = true;
177+
}
178+
},
179+
'"' => {
180+
out[len] = '\\';
181+
out[len + 1] = '"';
182+
len += 2;
183+
prev_was_space = false;
184+
},
185+
else => {
186+
out[len] = c;
187+
len += 1;
188+
prev_was_space = (c == ' ');
189+
},
190+
}
191+
}
192+
193+
return out[0..len];
194+
}

0 commit comments

Comments
 (0)