From c55d5c383af01a0366a93a96aae639959c5a2612 Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Mon, 5 Feb 2024 23:10:13 -0600 Subject: [PATCH 1/3] Generate fish command completions for Ghostty. --- build.zig | 14 ++++ src/fish_completions.zig | 149 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 163 insertions(+) create mode 100644 src/fish_completions.zig diff --git a/build.zig b/build.zig index ce295bbdf..db0d727db 100644 --- a/build.zig +++ b/build.zig @@ -404,6 +404,20 @@ pub fn build(b: *std.Build) !void { } } + // Fish shell completions + { + const fish_exe = b.addExecutable(.{ + .name = "fish_completions", + .root_source_file = .{ .path = "src/fish_completions.zig" }, + .target = b.host, + }); + const fish_step = b.addRunArtifact(fish_exe); + b.getInstallStep().dependOn(&b.addInstallFile( + fish_step.captureStdOut(), + "share/fish/vendor_completions.d/ghostty.fish", + ).step); + } + // Vim plugin { const wf = b.addWriteFiles(); diff --git a/src/fish_completions.zig b/src/fish_completions.zig new file mode 100644 index 000000000..4006c987f --- /dev/null +++ b/src/fish_completions.zig @@ -0,0 +1,149 @@ +const std = @import("std"); + +const Config = @import("config/Config.zig"); +const Action = @import("cli/action.zig").Action; +const ListFontsConfig = @import("cli/list_fonts.zig").Config; +const ShowConfigOptions = @import("cli/show_config.zig").Options; +const ListKeybindsOptions = @import("cli/list_keybinds.zig").Options; + +pub fn main() !void { + const writer = std.io.getStdOut().writer(); + + { + try writer.writeAll("set -l commands \""); + var count: usize = 0; + inline for (@typeInfo(Action).Enum.fields) |field| { + if (comptime std.mem.eql(u8, "help", field.name)) continue; + if (comptime std.mem.eql(u8, "version", field.name)) continue; + if (count > 0) try writer.writeAll(" "); + try writer.writeAll("+"); + try writer.writeAll(field.name); + count += 1; + } + try writer.writeAll("\"\n"); + } + + try writer.writeAll("complete -c ghostty -f\n"); + + try writer.writeAll("complete -c ghostty -l help -f\n"); + try writer.writeAll("complete -c ghostty -n \"not __fish_seen_subcommand_from $commands\" -l version -f\n"); + + inline for (@typeInfo(Config).Struct.fields) |field| { + if (field.name[0] == '_') continue; + + try writer.writeAll("complete -c ghostty -n \"not __fish_seen_subcommand_from $commands\" -l "); + try writer.writeAll(field.name); + try writer.writeAll(if (field.type != bool) " -r" else " "); + if (std.mem.startsWith(u8, field.name, "font-family")) + try writer.writeAll(" -f -a \"(ghostty +list-fonts | grep '^[A-Z]')\"") + else if (std.mem.eql(u8, "theme", field.name)) + try writer.writeAll(" -f -a \"(ghostty +list-themes)\"") + else if (std.mem.eql(u8, "working-directory", field.name)) + try writer.writeAll(" -f -k -a \"(__fish_complete_directories)\"") + else { + try writer.writeAll(if (field.type != Config.RepeatablePath) " -f" else " -F"); + switch (@typeInfo(field.type)) { + .Bool => try writer.writeAll(" -a \"true false\""), + .Enum => |info| { + try writer.writeAll(" -a \""); + inline for (info.fields, 0..) |f, i| { + if (i > 0) try writer.writeAll(" "); + try writer.writeAll(f.name); + } + try writer.writeAll("\""); + }, + .Struct => |info| { + if (!@hasDecl(field.type, "parseCLI") and info.layout == .Packed) { + try writer.writeAll(" -a \""); + inline for (info.fields, 0..) |f, i| { + if (i > 0) try writer.writeAll(" "); + try writer.writeAll(f.name); + try writer.writeAll(" no-"); + try writer.writeAll(f.name); + } + try writer.writeAll("\""); + } + }, + else => {}, + } + } + try writer.writeAll("\n"); + } + + { + try writer.writeAll("complete -c ghostty -n \"string match -q -- '+*' (commandline -pt)\" -f -a \""); + var count: usize = 0; + inline for (@typeInfo(Action).Enum.fields) |field| { + if (comptime std.mem.eql(u8, "help", field.name)) continue; + if (comptime std.mem.eql(u8, "version", field.name)) continue; + if (count > 0) try writer.writeAll(" "); + try writer.writeAll("+"); + try writer.writeAll(field.name); + count += 1; + } + try writer.writeAll("\"\n"); + } + + inline for (@typeInfo(ListFontsConfig).Struct.fields) |field| { + if (field.name[0] == '_') continue; + try writer.writeAll("complete -c ghostty -n \"__fish_seen_subcommand_from +list-fonts\" -l "); + try writer.writeAll(field.name); + try writer.writeAll(if (field.type != bool) " -r" else " "); + try writer.writeAll(" -f"); + switch (@typeInfo(field.type)) { + .Bool => try writer.writeAll(" -a \"true false\""), + .Enum => |info| { + try writer.writeAll(" -a \""); + inline for (info.fields, 0..) |f, i| { + if (i > 0) try writer.writeAll(" "); + try writer.writeAll(f.name); + } + try writer.writeAll("\""); + }, + else => {}, + } + try writer.writeAll("\n"); + } + + inline for (@typeInfo(ShowConfigOptions).Struct.fields) |field| { + if (field.name[0] == '_') continue; + try writer.writeAll("complete -c ghostty -n \"__fish_seen_subcommand_from +show-config\" -l "); + try writer.writeAll(field.name); + try writer.writeAll(if (field.type != bool) " -r" else " "); + try writer.writeAll(" -f"); + switch (@typeInfo(field.type)) { + .Bool => try writer.writeAll(" -a \"true false\""), + .Enum => |info| { + try writer.writeAll(" -a \""); + inline for (info.fields, 0..) |f, i| { + if (i > 0) try writer.writeAll(" "); + try writer.writeAll(f.name); + } + try writer.writeAll("\""); + }, + else => {}, + } + try writer.writeAll("\n"); + } + + inline for (@typeInfo(ListKeybindsOptions).Struct.fields) |field| { + if (field.name[0] == '_') continue; + try writer.writeAll("complete -c ghostty -n \"__fish_seen_subcommand_from +list-keybinds\" -l "); + try writer.writeAll(field.name); + try writer.writeAll(if (field.type != bool) " -r" else " "); + try writer.writeAll(" -f"); + switch (@typeInfo(field.type)) { + .Bool => try writer.writeAll(" -a \"true false\""), + .Enum => |info| { + try writer.writeAll(" -a \""); + inline for (info.fields, 0..) |f, i| { + if (i > 0) try writer.writeAll(" "); + try writer.writeAll(f.name); + } + try writer.writeAll("\""); + }, + else => {}, + } + try writer.writeAll("\n"); + } +} From 72b10841623884201181d234ebf4d5246f668f0e Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Tue, 6 Feb 2024 00:04:36 -0600 Subject: [PATCH 2/3] Do the fish completion generation with comptime during the build rather than building a standalone executable. --- build.zig | 17 ++++----- src/{ => build}/fish_completions.zig | 57 +++++++++++++++++----------- 2 files changed, 43 insertions(+), 31 deletions(-) rename src/{ => build}/fish_completions.zig (74%) diff --git a/build.zig b/build.zig index db0d727db..d9e0e2351 100644 --- a/build.zig +++ b/build.zig @@ -9,6 +9,7 @@ const font = @import("src/font/main.zig"); const renderer = @import("src/renderer.zig"); const terminfo = @import("src/terminfo/main.zig"); const config_vim = @import("src/config/vim.zig"); +const fish_completions = @import("src/build/fish_completions.zig"); const build_config = @import("src/build_config.zig"); const BuildConfig = build_config.BuildConfig; const WasmTarget = @import("src/os/wasm/target.zig").Target; @@ -406,16 +407,14 @@ pub fn build(b: *std.Build) !void { // Fish shell completions { - const fish_exe = b.addExecutable(.{ - .name = "fish_completions", - .root_source_file = .{ .path = "src/fish_completions.zig" }, - .target = b.host, + const wf = b.addWriteFiles(); + _ = wf.add("ghostty.fish", fish_completions.fish_completions); + + b.installDirectory(.{ + .source_dir = wf.getDirectory(), + .install_dir = .prefix, + .install_subdir = "share/fish/vendor_completions.d", }); - const fish_step = b.addRunArtifact(fish_exe); - b.getInstallStep().dependOn(&b.addInstallFile( - fish_step.captureStdOut(), - "share/fish/vendor_completions.d/ghostty.fish", - ).step); } // Vim plugin diff --git a/src/fish_completions.zig b/src/build/fish_completions.zig similarity index 74% rename from src/fish_completions.zig rename to src/build/fish_completions.zig index 4006c987f..5580940ac 100644 --- a/src/fish_completions.zig +++ b/src/build/fish_completions.zig @@ -1,20 +1,33 @@ const std = @import("std"); -const Config = @import("config/Config.zig"); -const Action = @import("cli/action.zig").Action; -const ListFontsConfig = @import("cli/list_fonts.zig").Config; -const ShowConfigOptions = @import("cli/show_config.zig").Options; -const ListKeybindsOptions = @import("cli/list_keybinds.zig").Options; +const Config = @import("../config/Config.zig"); +const Action = @import("../cli/action.zig").Action; +const ListFontsConfig = @import("../cli/list_fonts.zig").Config; +const ShowConfigOptions = @import("../cli/show_config.zig").Options; +const ListKeybindsOptions = @import("../cli/list_keybinds.zig").Options; -pub fn main() !void { - const writer = std.io.getStdOut().writer(); +pub const fish_completions = comptimeGenerateFishCompletions(); +pub fn comptimeGenerateFishCompletions() []const u8 { + comptime { + @setEvalBranchQuota(10000); + var counter = std.io.countingWriter(std.io.null_writer); + try writeFishCompletions(&counter.writer()); + + var buf: [counter.bytes_written]u8 = undefined; + var stream = std.io.fixedBufferStream(&buf); + try writeFishCompletions(stream.writer()); + return stream.getWritten(); + } +} + +pub fn writeFishCompletions(writer: anytype) !void { { try writer.writeAll("set -l commands \""); var count: usize = 0; - inline for (@typeInfo(Action).Enum.fields) |field| { - if (comptime std.mem.eql(u8, "help", field.name)) continue; - if (comptime std.mem.eql(u8, "version", field.name)) continue; + for (@typeInfo(Action).Enum.fields) |field| { + if (std.mem.eql(u8, "help", field.name)) continue; + if (std.mem.eql(u8, "version", field.name)) continue; if (count > 0) try writer.writeAll(" "); try writer.writeAll("+"); try writer.writeAll(field.name); @@ -28,7 +41,7 @@ pub fn main() !void { try writer.writeAll("complete -c ghostty -l help -f\n"); try writer.writeAll("complete -c ghostty -n \"not __fish_seen_subcommand_from $commands\" -l version -f\n"); - inline for (@typeInfo(Config).Struct.fields) |field| { + for (@typeInfo(Config).Struct.fields) |field| { if (field.name[0] == '_') continue; try writer.writeAll("complete -c ghostty -n \"not __fish_seen_subcommand_from $commands\" -l "); @@ -46,7 +59,7 @@ pub fn main() !void { .Bool => try writer.writeAll(" -a \"true false\""), .Enum => |info| { try writer.writeAll(" -a \""); - inline for (info.fields, 0..) |f, i| { + for (info.fields, 0..) |f, i| { if (i > 0) try writer.writeAll(" "); try writer.writeAll(f.name); } @@ -55,7 +68,7 @@ pub fn main() !void { .Struct => |info| { if (!@hasDecl(field.type, "parseCLI") and info.layout == .Packed) { try writer.writeAll(" -a \""); - inline for (info.fields, 0..) |f, i| { + for (info.fields, 0..) |f, i| { if (i > 0) try writer.writeAll(" "); try writer.writeAll(f.name); try writer.writeAll(" no-"); @@ -73,9 +86,9 @@ pub fn main() !void { { try writer.writeAll("complete -c ghostty -n \"string match -q -- '+*' (commandline -pt)\" -f -a \""); var count: usize = 0; - inline for (@typeInfo(Action).Enum.fields) |field| { - if (comptime std.mem.eql(u8, "help", field.name)) continue; - if (comptime std.mem.eql(u8, "version", field.name)) continue; + for (@typeInfo(Action).Enum.fields) |field| { + if (std.mem.eql(u8, "help", field.name)) continue; + if (std.mem.eql(u8, "version", field.name)) continue; if (count > 0) try writer.writeAll(" "); try writer.writeAll("+"); try writer.writeAll(field.name); @@ -84,7 +97,7 @@ pub fn main() !void { try writer.writeAll("\"\n"); } - inline for (@typeInfo(ListFontsConfig).Struct.fields) |field| { + for (@typeInfo(ListFontsConfig).Struct.fields) |field| { if (field.name[0] == '_') continue; try writer.writeAll("complete -c ghostty -n \"__fish_seen_subcommand_from +list-fonts\" -l "); try writer.writeAll(field.name); @@ -94,7 +107,7 @@ pub fn main() !void { .Bool => try writer.writeAll(" -a \"true false\""), .Enum => |info| { try writer.writeAll(" -a \""); - inline for (info.fields, 0..) |f, i| { + for (info.fields, 0..) |f, i| { if (i > 0) try writer.writeAll(" "); try writer.writeAll(f.name); } @@ -105,7 +118,7 @@ pub fn main() !void { try writer.writeAll("\n"); } - inline for (@typeInfo(ShowConfigOptions).Struct.fields) |field| { + for (@typeInfo(ShowConfigOptions).Struct.fields) |field| { if (field.name[0] == '_') continue; try writer.writeAll("complete -c ghostty -n \"__fish_seen_subcommand_from +show-config\" -l "); try writer.writeAll(field.name); @@ -115,7 +128,7 @@ pub fn main() !void { .Bool => try writer.writeAll(" -a \"true false\""), .Enum => |info| { try writer.writeAll(" -a \""); - inline for (info.fields, 0..) |f, i| { + for (info.fields, 0..) |f, i| { if (i > 0) try writer.writeAll(" "); try writer.writeAll(f.name); } @@ -126,7 +139,7 @@ pub fn main() !void { try writer.writeAll("\n"); } - inline for (@typeInfo(ListKeybindsOptions).Struct.fields) |field| { + for (@typeInfo(ListKeybindsOptions).Struct.fields) |field| { if (field.name[0] == '_') continue; try writer.writeAll("complete -c ghostty -n \"__fish_seen_subcommand_from +list-keybinds\" -l "); try writer.writeAll(field.name); @@ -136,7 +149,7 @@ pub fn main() !void { .Bool => try writer.writeAll(" -a \"true false\""), .Enum => |info| { try writer.writeAll(" -a \""); - inline for (info.fields, 0..) |f, i| { + for (info.fields, 0..) |f, i| { if (i > 0) try writer.writeAll(" "); try writer.writeAll(f.name); } From 6eb24a1c67c0a6b89bbed95f23be57366dbdc6d5 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 6 Feb 2024 08:55:54 -0800 Subject: [PATCH 3/3] fish: make generators non-pub --- src/build/fish_completions.zig | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/build/fish_completions.zig b/src/build/fish_completions.zig index 5580940ac..120ef4586 100644 --- a/src/build/fish_completions.zig +++ b/src/build/fish_completions.zig @@ -6,9 +6,11 @@ const ListFontsConfig = @import("../cli/list_fonts.zig").Config; const ShowConfigOptions = @import("../cli/show_config.zig").Options; const ListKeybindsOptions = @import("../cli/list_keybinds.zig").Options; +/// A fish completions configuration that contains all the available commands +/// and options. pub const fish_completions = comptimeGenerateFishCompletions(); -pub fn comptimeGenerateFishCompletions() []const u8 { +fn comptimeGenerateFishCompletions() []const u8 { comptime { @setEvalBranchQuota(10000); var counter = std.io.countingWriter(std.io.null_writer); @@ -21,7 +23,7 @@ pub fn comptimeGenerateFishCompletions() []const u8 { } } -pub fn writeFishCompletions(writer: anytype) !void { +fn writeFishCompletions(writer: anytype) !void { { try writer.writeAll("set -l commands \""); var count: usize = 0;