build: generate various resources at build run, not build graph

This is stomping towards minimizing our build.zig dependencies so that
it can be cached more often. Right now, touching almost any file in the
project forces the build.zig to rebuild which is destroying my
productivity.
This commit is contained in:
Mitchell Hashimoto
2025-09-19 14:23:33 -07:00
parent 999b605145
commit bf047032b5
10 changed files with 118 additions and 50 deletions

View File

@@ -5,9 +5,6 @@ const builtin = @import("builtin");
const assert = std.debug.assert;
const buildpkg = @import("main.zig");
const Config = @import("Config.zig");
const config_vim = @import("../config/vim.zig");
const config_sublime_syntax = @import("../config/sublime_syntax.zig");
const terminfo = @import("../terminfo/main.zig");
const RunStep = std.Build.Step.Run;
steps: []*std.Build.Step,
@@ -16,6 +13,19 @@ pub fn init(b: *std.Build, cfg: *const Config) !GhosttyResources {
var steps = std.ArrayList(*std.Build.Step).init(b.allocator);
errdefer steps.deinit();
// This is the exe used to generate some build data.
const build_data_exe = b.addExecutable(.{
.name = "ghostty-build-data",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main_build_data.zig"),
.target = b.graph.host,
.strip = false,
.omit_frame_pointer = false,
.unwind_tables = .sync,
}),
});
build_data_exe.linkLibC();
// Terminfo
terminfo: {
const os_tag = cfg.target.result.os.tag;
@@ -25,13 +35,10 @@ pub fn init(b: *std.Build, cfg: *const Config) !GhosttyResources {
"terminfo";
// Encode our terminfo
var str = std.ArrayList(u8).init(b.allocator);
defer str.deinit();
try terminfo.ghostty.encode(str.writer());
// Write it
var wf = b.addWriteFiles();
const source = wf.add("ghostty.terminfo", str.items);
const run = b.addRunArtifact(build_data_exe);
run.addArg("+terminfo");
const wf = b.addWriteFiles();
const source = wf.addCopyFile(run.captureStdOut(), "ghostty.terminfo");
if (cfg.emit_terminfo) {
const source_install = b.addInstallFile(
@@ -130,8 +137,10 @@ pub fn init(b: *std.Build, cfg: *const Config) !GhosttyResources {
// Fish shell completions
{
const run = b.addRunArtifact(build_data_exe);
run.addArg("+fish");
const wf = b.addWriteFiles();
_ = wf.add("ghostty.fish", buildpkg.fish_completions);
_ = wf.addCopyFile(run.captureStdOut(), "ghostty.fish");
const install_step = b.addInstallDirectory(.{
.source_dir = wf.getDirectory(),
@@ -143,8 +152,10 @@ pub fn init(b: *std.Build, cfg: *const Config) !GhosttyResources {
// zsh shell completions
{
const run = b.addRunArtifact(build_data_exe);
run.addArg("+zsh");
const wf = b.addWriteFiles();
_ = wf.add("_ghostty", buildpkg.zsh_completions);
_ = wf.addCopyFile(run.captureStdOut(), "_ghostty");
const install_step = b.addInstallDirectory(.{
.source_dir = wf.getDirectory(),
@@ -156,8 +167,10 @@ pub fn init(b: *std.Build, cfg: *const Config) !GhosttyResources {
// bash shell completions
{
const run = b.addRunArtifact(build_data_exe);
run.addArg("+bash");
const wf = b.addWriteFiles();
_ = wf.add("ghostty.bash", buildpkg.bash_completions);
_ = wf.addCopyFile(run.captureStdOut(), "ghostty.bash");
const install_step = b.addInstallDirectory(.{
.source_dir = wf.getDirectory(),
@@ -167,39 +180,44 @@ pub fn init(b: *std.Build, cfg: *const Config) !GhosttyResources {
try steps.append(&install_step.step);
}
// Vim plugin
// Vim and Neovim plugin
{
const wf = b.addWriteFiles();
_ = wf.add("syntax/ghostty.vim", config_vim.syntax);
_ = wf.add("ftdetect/ghostty.vim", config_vim.ftdetect);
_ = wf.add("ftplugin/ghostty.vim", config_vim.ftplugin);
_ = wf.add("compiler/ghostty.vim", config_vim.compiler);
const install_step = b.addInstallDirectory(.{
{
const run = b.addRunArtifact(build_data_exe);
run.addArg("+vim-syntax");
_ = wf.addCopyFile(run.captureStdOut(), "syntax/ghostty.vim");
}
{
const run = b.addRunArtifact(build_data_exe);
run.addArg("+vim-ftdetect");
_ = wf.addCopyFile(run.captureStdOut(), "ftdetect/ghostty.vim");
}
{
const run = b.addRunArtifact(build_data_exe);
run.addArg("+vim-ftplugin");
_ = wf.addCopyFile(run.captureStdOut(), "ftplugin/ghostty.vim");
}
{
const run = b.addRunArtifact(build_data_exe);
run.addArg("+vim-compiler");
_ = wf.addCopyFile(run.captureStdOut(), "compiler/ghostty.vim");
}
const vim_step = b.addInstallDirectory(.{
.source_dir = wf.getDirectory(),
.install_dir = .prefix,
.install_subdir = "share/vim/vimfiles",
});
try steps.append(&install_step.step);
}
try steps.append(&vim_step.step);
// Neovim plugin
// This is just a copy-paste of the Vim plugin, but using a Neovim subdir.
// By default, Neovim doesn't look inside share/vim/vimfiles. Some distros
// configure it to do that however. Fedora, does not as a counterexample.
{
const wf = b.addWriteFiles();
_ = wf.add("syntax/ghostty.vim", config_vim.syntax);
_ = wf.add("ftdetect/ghostty.vim", config_vim.ftdetect);
_ = wf.add("ftplugin/ghostty.vim", config_vim.ftplugin);
_ = wf.add("compiler/ghostty.vim", config_vim.compiler);
const install_step = b.addInstallDirectory(.{
const neovim_step = b.addInstallDirectory(.{
.source_dir = wf.getDirectory(),
.install_dir = .prefix,
.install_subdir = "share/nvim/site",
});
try steps.append(&install_step.step);
try steps.append(&neovim_step.step);
}
// Sublime syntax highlighting for bat cli tool
@@ -209,8 +227,10 @@ pub fn init(b: *std.Build, cfg: *const Config) !GhosttyResources {
// the config file within the '~.config/bat' directory
// (ex: --map-syntax "/Users/user/.config/ghostty/config:Ghostty Config").
{
const run = b.addRunArtifact(build_data_exe);
run.addArg("+sublime");
const wf = b.addWriteFiles();
_ = wf.add("ghostty.sublime-syntax", config_sublime_syntax.syntax);
_ = wf.addCopyFile(run.captureStdOut(), "ghostty.sublime-syntax");
const install_step = b.addInstallDirectory(.{
.source_dir = wf.getDirectory(),

View File

@@ -1,338 +0,0 @@
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.countingWriter(std.io.null_writer);
try writeBashCompletions(&counter.writer());
var buf: [counter.bytes_written]u8 = undefined;
var stream = std.io.fixedBufferStream(&buf);
try writeBashCompletions(stream.writer());
const final = buf;
return final[0..stream.getWritten().len];
}
}
fn writeBashCompletions(writer: anytype) !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
\\
);
}

View File

@@ -1,144 +0,0 @@
const std = @import("std");
const Config = @import("../config/Config.zig");
const Action = @import("../cli.zig").ghostty.Action;
/// A fish completions configuration that contains all the available commands
/// and options.
pub const completions = comptimeGenerateFishCompletions();
fn comptimeGenerateFishCompletions() []const u8 {
comptime {
@setEvalBranchQuota(50000);
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());
const final = buf;
return final[0..stream.getWritten().len];
}
}
fn writeFishCompletions(writer: anytype) !void {
{
try writer.writeAll("set -l commands \"");
var count: usize = 0;
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);
count += 1;
}
try writer.writeAll("\"\n");
}
try writer.writeAll("complete -c ghostty -f\n");
try writer.writeAll("complete -c ghostty -s e -l help -f\n");
try writer.writeAll("complete -c ghostty -n \"not __fish_seen_subcommand_from $commands\" -l version -f\n");
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 | sed -E 's/^(.*) \\(.*\\$/\\1/')\"")
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 => {},
.@"enum" => |info| {
try writer.writeAll(" -a \"");
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 \"");
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;
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);
count += 1;
}
try writer.writeAll("\"\n");
}
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();
for (@typeInfo(options).@"struct".fields) |opt| {
if (opt.name[0] == '_') continue;
try writer.writeAll("complete -c ghostty -n \"__fish_seen_subcommand_from +" ++ field.name ++ "\" -l ");
try writer.writeAll(opt.name);
try writer.writeAll(if (opt.type != bool) " -r" else "");
// special case +validate_config --config-file
if (std.mem.eql(u8, "config-file", opt.name)) {
try writer.writeAll(" -F");
} else try writer.writeAll(" -f");
switch (@typeInfo(opt.type)) {
.bool => {},
.@"enum" => |info| {
try writer.writeAll(" -a \"");
for (info.fields, 0..) |f, i| {
if (i > 0) try writer.writeAll(" ");
try writer.writeAll(f.name);
}
try writer.writeAll("\"");
},
.optional => |optional| {
switch (@typeInfo(optional.child)) {
.@"enum" => |info| {
try writer.writeAll(" -a \"");
for (info.fields, 0..) |f, i| {
if (i > 0) try writer.writeAll(" ");
try writer.writeAll(f.name);
}
try writer.writeAll("\"");
},
else => {},
}
},
else => {},
}
try writer.writeAll("\n");
}
}
}

View File

@@ -28,10 +28,5 @@ pub const LipoStep = @import("LipoStep.zig");
pub const MetallibStep = @import("MetallibStep.zig");
pub const XCFrameworkStep = @import("XCFrameworkStep.zig");
// Shell completions
pub const fish_completions = @import("fish_completions.zig").completions;
pub const zsh_completions = @import("zsh_completions.zig").completions;
pub const bash_completions = @import("bash_completions.zig").completions;
// Helpers
pub const requireZig = @import("zig.zig").requireZig;

View File

@@ -1,237 +0,0 @@
const std = @import("std");
const Config = @import("../config/Config.zig");
const Action = @import("../cli.zig").ghostty.Action;
/// A zsh completions configuration that contains all the available commands
/// and options.
pub const completions = comptimeGenerateZshCompletions();
const equals_required = "=-:::";
fn comptimeGenerateZshCompletions() []const u8 {
comptime {
@setEvalBranchQuota(50000);
var counter = std.io.countingWriter(std.io.null_writer);
try writeZshCompletions(&counter.writer());
var buf: [counter.bytes_written]u8 = undefined;
var stream = std.io.fixedBufferStream(&buf);
try writeZshCompletions(stream.writer());
const final = buf;
return final[0..stream.getWritten().len];
}
}
fn writeZshCompletions(writer: anytype) !void {
try writer.writeAll(
\\#compdef ghostty
\\
\\_fonts () {
\\ local font_list=$(ghostty +list-fonts | grep -Z '^[A-Z]')
\\ local fonts=(${(f)font_list})
\\ _describe -t fonts 'fonts' fonts
\\}
\\
\\_themes() {
\\ local theme_list=$(ghostty +list-themes | sed -E 's/^(.*) \(.*$/\1/')
\\ local themes=(${(f)theme_list})
\\ _describe -t themes 'themes' themes
\\}
\\
);
try writer.writeAll("_config() {\n");
try writer.writeAll(" _arguments \\\n");
try writer.writeAll(" \"--help\" \\\n");
try writer.writeAll(" \"--version\" \\\n");
for (@typeInfo(Config).@"struct".fields) |field| {
if (field.name[0] == '_') continue;
try writer.writeAll(" \"--");
try writer.writeAll(field.name);
if (std.mem.startsWith(u8, field.name, "font-family")) {
try writer.writeAll(equals_required);
try writer.writeAll("_fonts");
} else if (std.mem.eql(u8, "theme", field.name)) {
try writer.writeAll(equals_required);
try writer.writeAll("_themes");
} else if (std.mem.eql(u8, "working-directory", field.name)) {
try writer.writeAll(equals_required);
try writer.writeAll("{_files -/}");
} else if (field.type == Config.RepeatablePath) {
try writer.writeAll(equals_required);
try writer.writeAll("_files"); // todo check if this is needed
} else {
switch (@typeInfo(field.type)) {
.bool => {},
.@"enum" => |info| {
try writer.writeAll(equals_required);
try writer.writeAll("(");
for (info.fields, 0..) |f, i| {
if (i > 0) try writer.writeAll(" ");
try writer.writeAll(f.name);
}
try writer.writeAll(")");
},
.@"struct" => |info| {
try writer.writeAll(equals_required);
if (!@hasDecl(field.type, "parseCLI") and info.layout == .@"packed") {
try writer.writeAll("(");
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 {
//resize-overlay-duration
//keybind
//window-padding-x ...-y
//link
//palette
//background
//foreground
//font-variation*
//font-feature
try writer.writeAll("( )");
}
},
else => {
try writer.writeAll(equals_required);
try writer.writeAll("( )");
},
}
}
try writer.writeAll("\" \\\n");
}
try writer.writeAll("\n}\n\n");
try writer.writeAll(
\\_ghostty() {
\\ typeset -A opt_args
\\ local context state line
\\ local opt=('-e' '--help' '--version')
\\
\\ _arguments -C \
\\ '1:actions:->actions' \
\\ '*:: :->rest' \
\\
\\ if [[ "$line[1]" == "--help" || "$line[1]" == "--version" || "$line[1]" == "-e" ]]; then
\\ return
\\ fi
\\
\\ if [[ "$line[1]" == -* ]]; then
\\ _config
\\ return
\\ fi
\\
\\ case "$state" in
\\ (actions)
\\ local actions; actions=(
\\
);
{
// how to get 'commands'
var count: usize = 0;
const padding = " ";
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(padding ++ "'+");
try writer.writeAll(field.name);
try writer.writeAll("'\n");
count += 1;
}
}
try writer.writeAll(
\\ )
\\ _describe '' opt
\\ _describe -t action 'action' actions
\\ ;;
\\ (rest)
\\ if [[ "$line[2]" == "--help" ]]; then
\\ return
\\ fi
\\
\\ local help=('--help')
\\ _describe '' help
\\
\\ case $line[1] in
\\
);
{
const padding = " ";
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;
try writer.writeAll(padding ++ "(+" ++ field.name ++ ")\n");
try writer.writeAll(padding ++ " _arguments \\\n");
for (@typeInfo(options).@"struct".fields) |opt| {
if (opt.name[0] == '_') continue;
try writer.writeAll(padding ++ " '--");
try writer.writeAll(opt.name);
switch (@typeInfo(opt.type)) {
.bool => {},
.@"enum" => |info| {
try writer.writeAll(equals_required);
try writer.writeAll("(");
for (info.fields, 0..) |f, i| {
if (i > 0) try writer.writeAll(" ");
try writer.writeAll(f.name);
}
try writer.writeAll(")");
},
.optional => |optional| {
try writer.writeAll(equals_required);
switch (@typeInfo(optional.child)) {
.@"enum" => |info| {
try writer.writeAll("(");
for (info.fields, 0..) |f, i| {
if (i > 0) try writer.writeAll(" ");
try writer.writeAll(f.name);
}
try writer.writeAll(")");
},
else => {
if (std.mem.eql(u8, "config-file", opt.name)) {
try writer.writeAll("_files");
} else try writer.writeAll("( )");
},
}
},
else => {
try writer.writeAll(equals_required);
if (std.mem.eql(u8, "config-file", opt.name)) {
try writer.writeAll("_files");
} else try writer.writeAll("( )");
},
}
try writer.writeAll("' \\\n");
}
try writer.writeAll(padding ++ ";;\n");
}
}
try writer.writeAll(
\\ esac
\\ ;;
\\ esac
\\}
\\
\\_ghostty "$@"
\\
);
}