From fddc8809414f3f12dfac7acb0ed3c9e3ef0b8491 Mon Sep 17 00:00:00 2001 From: Anund Date: Fri, 13 Dec 2024 16:54:12 +1100 Subject: [PATCH 1/6] bash: add bash completion generation closes #2053 --- build.zig | 13 ++ src/build/bash_completions.zig | 278 +++++++++++++++++++++++++++++++++ 2 files changed, 291 insertions(+) create mode 100644 src/build/bash_completions.zig diff --git a/build.zig b/build.zig index f1ff1b1c1..5f144c3d0 100644 --- a/build.zig +++ b/build.zig @@ -13,6 +13,7 @@ const config_vim = @import("src/config/vim.zig"); const config_sublime_syntax = @import("src/config/sublime_syntax.zig"); const fish_completions = @import("src/build/fish_completions.zig"); const zsh_completions = @import("src/build/zsh_completions.zig"); +const bash_completions = @import("src/build/bash_completions.zig"); const build_config = @import("src/build_config.zig"); const BuildConfig = build_config.BuildConfig; const WasmTarget = @import("src/os/wasm/target.zig").Target; @@ -517,6 +518,18 @@ pub fn build(b: *std.Build) !void { }); } + // bash shell completions + { + const wf = b.addWriteFiles(); + _ = wf.add("ghostty.bash", bash_completions.bash_completions); + + b.installDirectory(.{ + .source_dir = wf.getDirectory(), + .install_dir = .prefix, + .install_subdir = "share/bash-completion/completions", + }); + } + // Vim plugin { const wf = b.addWriteFiles(); diff --git a/src/build/bash_completions.zig b/src/build/bash_completions.zig new file mode 100644 index 000000000..fb8d904e8 --- /dev/null +++ b/src/build/bash_completions.zig @@ -0,0 +1,278 @@ +const std = @import("std"); + +const Config = @import("../config/Config.zig"); +const Action = @import("../cli/action.zig").Action; + +/// A bash completions configuration that contains all the available commands +/// and options. +/// +/// Notes: bash completion support for --= 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 --=. 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 bash_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; + + try writer.writeAll( + \\#!/usr/bin/env bash + \\ + \\# -o nospace requires we add back a space when a completion is finished + \\# and not part of a --key= completion + \\appendSpaces () { + \\ 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") + \\} + \\ + \\config="--help" + \\config+=" --version" + \\ + ); + + for (@typeInfo(Config).Struct.fields) |field| { + if (field.name[0] == '_') continue; + try writer.writeAll("config+=\" --" ++ field.name ++ "=\"\n"); + } + + try writer.writeAll( + \\ + \\_handleConfig () { + \\ case "$prev" in + \\ + ); + + for (@typeInfo(Config).Struct.fields) |field| { + if (field.name[0] == '_') continue; + try writer.writeAll(pad2 ++ "--" ++ 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("mapfile -t COMPREPLY < <( compgen -d -- \"$cur\" ); appendSpaces ;;") + else if (field.type == Config.RepeatablePath) + try writer.writeAll("mapfile -t COMPREPLY < <( compgen -f -- \"$cur\" ); appendSpaces ;;") + else { + const compgenPrefix = "mapfile -t COMPREPLY < <( compgen -W \""; + const compgenSuffix = "\" -- \"$cur\" ); appendSpaces ;;"; + switch (@typeInfo(field.type)) { + .Bool => try writer.writeAll(compgenPrefix ++ "true false" ++ compgenSuffix), + .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 + \\} + \\ + \\ + ); + + 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 safeName: []u8 = buffer[0..field.name.len]; + @memcpy(safeName, field.name); + + std.mem.replaceScalar(u8, safeName, '-', '_'); + try writer.writeAll(safeName ++ "=\""); + + { + var count = 0; + for (@typeInfo(options).Struct.fields) |opt| { + if (opt.name[0] == '_') continue; + if (count > 0) try writer.writeAll(" "); + try writer.writeAll("--" ++ opt.name ++ "="); + count += 1; + } + } + try writer.writeAll(" --help\"\n"); + } + + try writer.writeAll( + \\ + \\_handleActions () { + \\ 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 safeName: []u8 = buffer[0..field.name.len]; + _ = std.mem.replace(u8, field.name, "-", "_", safeName); + + try writer.writeAll(pad2 ++ "+" ++ field.name ++ ")\n"); + try writer.writeAll(pad3 ++ "case $prev in\n"); + for (@typeInfo(options).Struct.fields) |opt| { + if (opt.name[0] == '_') continue; + + try writer.writeAll(pad4 ++ "--" ++ opt.name ++ ") "); + + const compgenPrefix = "mapfile -t COMPREPLY < <( compgen -W \""; + const compgenSuffix = "\" -- \"$cur\" ); appendSpaces ;;"; + switch (@typeInfo(opt.type)) { + .Bool => try writer.writeAll(compgenPrefix ++ "true false" ++ compgenSuffix), + .Enum => |info| { + try writer.writeAll(compgenPrefix); + for (info.opts, 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("mapfile -t COMPREPLY < <( compgen -f -- \"$cur\" ); appendSpaces ;;"); + } else try writer.writeAll("return;;"); + }, + } + try writer.writeAll("\n"); + } + try writer.writeAll(pad4 ++ "*) mapfile -t COMPREPLY < <( compgen -W \"$" ++ safeName ++ "\" -- \"$cur\" ) ;;\n"); + try writer.writeAll( + \\ esac + \\ ;; + \\ + ); + } + + try writer.writeAll( + \\ *) mapfile -t COMPREPLY < <( compgen -W "--help" -- "$cur" ) ;; + \\ esac + \\ + \\ return 0 + \\} + \\ + \\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("topLevel+=\" +" ++ field.name ++ "\"\n"); + } + + try writer.writeAll( + \\ + \\_ghostty () { + \\ COMPREPLY=() + \\ if [ "$2" = "=" ]; then cur="" + \\ else cur="$2" + \\ fi + \\ + \\ if [ "$3" = "=" ]; then prev="${COMP_WORDS[COMP_CWORD-2]}" + \\ else prev="${COMP_WORDS[COMP_CWORD-1]}" + \\ fi + \\ + \\ if [[ "$2" == \"*\" ]]; then + \\ COMPREPLY=( "$cur " ); + \\ return; + \\ fi + \\ + \\ cword=$COMP_CWORD + \\ + \\ case "$cword" in + \\ 1) + \\ case "${COMP_WORDS[1]}" in + \\ -e | --help | --version) return 0 ;; + \\ --*) _handleConfig ;; + \\ *) mapfile -t COMPREPLY < <( compgen -W "${topLevel}" -- "$cur" ); appendSpaces ;; + \\ esac + \\ ;; + \\ *) + \\ case "$prev" in + \\ -e | --help | --version) return 0 ;; + \\ *) + \\ case "${COMP_WORDS[1]}" in + \\ --*) _handleConfig ;; + \\ +*) _handleActions ;; + \\ esac + \\ ;; + \\ esac + \\ ;; + \\ esac + \\ + \\ return 0 + \\} + \\ + \\complete -o nospace -o bashdefault -F _ghostty ghostty + \\ + ); +} From c02789205e38773a738664e560797d995fe1680e Mon Sep 17 00:00:00 2001 From: Anund Date: Sun, 15 Dec 2024 18:41:50 +1100 Subject: [PATCH 2/6] bash: fix incorrect completion for '--key ' --- src/build/bash_completions.zig | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/build/bash_completions.zig b/src/build/bash_completions.zig index fb8d904e8..c58c1f233 100644 --- a/src/build/bash_completions.zig +++ b/src/build/bash_completions.zig @@ -69,7 +69,7 @@ fn writeBashCompletions(writer: anytype) !void { try writer.writeAll( \\ - \\_handleConfig () { + \\_handleConfig() { \\ case "$prev" in \\ ); @@ -232,23 +232,23 @@ fn writeBashCompletions(writer: anytype) !void { try writer.writeAll( \\ \\_ghostty () { - \\ COMPREPLY=() + \\ cur=""; prev=""; prevWasEq=false; COMPREPLY=() + \\ \\ if [ "$2" = "=" ]; then cur="" \\ else cur="$2" \\ fi \\ - \\ if [ "$3" = "=" ]; then prev="${COMP_WORDS[COMP_CWORD-2]}" + \\ 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 \\ - \\ cword=$COMP_CWORD - \\ - \\ case "$cword" in + \\ case "$COMP_CWORD" in \\ 1) \\ case "${COMP_WORDS[1]}" in \\ -e | --help | --version) return 0 ;; @@ -260,6 +260,12 @@ fn writeBashCompletions(writer: anytype) !void { \\ 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: '-- ' + \\ # clear out prev so we don't run any of the key specific completions + \\ prev="" + \\ fi + \\ \\ case "${COMP_WORDS[1]}" in \\ --*) _handleConfig ;; \\ +*) _handleActions ;; From 361967f721b521c193f88a05c9a7fb704e5f239b Mon Sep 17 00:00:00 2001 From: Anund Date: Sun, 15 Dec 2024 20:13:45 +1100 Subject: [PATCH 3/6] bash: formatting changes, change to reference binary name via variable --- src/build/bash_completions.zig | 40 +++++++++++++++++----------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/build/bash_completions.zig b/src/build/bash_completions.zig index c58c1f233..ddcb82401 100644 --- a/src/build/bash_completions.zig +++ b/src/build/bash_completions.zig @@ -37,24 +37,23 @@ fn writeBashCompletions(writer: anytype) !void { const pad4 = pad3 ++ pad1; try writer.writeAll( - \\#!/usr/bin/env bash \\ \\# -o nospace requires we add back a space when a completion is finished \\# and not part of a --key= completion - \\appendSpaces () { + \\addSpaces() { \\ for idx in "${!COMPREPLY[@]}"; do \\ [ -n "${COMPREPLY[idx]}" ] && COMPREPLY[idx]="${COMPREPLY[idx]} "; \\ done \\} \\ - \\_fonts () { + \\_fonts() { \\ local IFS=$'\n' - \\ mapfile -t COMPREPLY < <( compgen -P '"' -S '"' -W "$(ghostty +list-fonts | grep '^[A-Z]' )" -- "$cur") + \\ 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") + \\ mapfile -t COMPREPLY < <( compgen -P '"' -S '"' -W "$($ghostty +list-themes | sed -E 's/^(.*) \(.*$/\1/')" -- "$cur") \\} \\ \\config="--help" @@ -83,12 +82,12 @@ fn writeBashCompletions(writer: anytype) !void { 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("mapfile -t COMPREPLY < <( compgen -d -- \"$cur\" ); appendSpaces ;;") + try writer.writeAll("mapfile -t COMPREPLY < <( compgen -d -- \"$cur\" ); addSpaces ;;") else if (field.type == Config.RepeatablePath) - try writer.writeAll("mapfile -t COMPREPLY < <( compgen -f -- \"$cur\" ); appendSpaces ;;") + try writer.writeAll("mapfile -t COMPREPLY < <( compgen -f -- \"$cur\" ); addSpaces ;;") else { const compgenPrefix = "mapfile -t COMPREPLY < <( compgen -W \""; - const compgenSuffix = "\" -- \"$cur\" ); appendSpaces ;;"; + const compgenSuffix = "\" -- \"$cur\" ); addSpaces ;;"; switch (@typeInfo(field.type)) { .Bool => try writer.writeAll(compgenPrefix ++ "true false" ++ compgenSuffix), .Enum => |info| { @@ -137,11 +136,11 @@ fn writeBashCompletions(writer: anytype) !void { if (@typeInfo(options).Struct.fields.len == 0) continue; var buffer: [field.name.len]u8 = undefined; - const safeName: []u8 = buffer[0..field.name.len]; - @memcpy(safeName, field.name); + const bashName: []u8 = buffer[0..field.name.len]; + @memcpy(bashName, field.name); - std.mem.replaceScalar(u8, safeName, '-', '_'); - try writer.writeAll(safeName ++ "=\""); + std.mem.replaceScalar(u8, bashName, '-', '_'); + try writer.writeAll(bashName ++ "=\""); { var count = 0; @@ -157,7 +156,7 @@ fn writeBashCompletions(writer: anytype) !void { try writer.writeAll( \\ - \\_handleActions () { + \\_handleActions() { \\ case "${COMP_WORDS[1]}" in \\ ); @@ -171,8 +170,8 @@ fn writeBashCompletions(writer: anytype) !void { // bash doesn't allow variable names containing '-' so replace them var buffer: [field.name.len]u8 = undefined; - const safeName: []u8 = buffer[0..field.name.len]; - _ = std.mem.replace(u8, field.name, "-", "_", safeName); + const bashName: []u8 = buffer[0..field.name.len]; + _ = std.mem.replace(u8, field.name, "-", "_", bashName); try writer.writeAll(pad2 ++ "+" ++ field.name ++ ")\n"); try writer.writeAll(pad3 ++ "case $prev in\n"); @@ -182,7 +181,7 @@ fn writeBashCompletions(writer: anytype) !void { try writer.writeAll(pad4 ++ "--" ++ opt.name ++ ") "); const compgenPrefix = "mapfile -t COMPREPLY < <( compgen -W \""; - const compgenSuffix = "\" -- \"$cur\" ); appendSpaces ;;"; + const compgenSuffix = "\" -- \"$cur\" ); addSpaces ;;"; switch (@typeInfo(opt.type)) { .Bool => try writer.writeAll(compgenPrefix ++ "true false" ++ compgenSuffix), .Enum => |info| { @@ -195,13 +194,13 @@ fn writeBashCompletions(writer: anytype) !void { }, else => { if (std.mem.eql(u8, "config-file", opt.name)) { - try writer.writeAll("mapfile -t COMPREPLY < <( compgen -f -- \"$cur\" ); appendSpaces ;;"); + try writer.writeAll("mapfile -t COMPREPLY < <( compgen -f -- \"$cur\" ); addSpaces ;;"); } else try writer.writeAll("return;;"); }, } try writer.writeAll("\n"); } - try writer.writeAll(pad4 ++ "*) mapfile -t COMPREPLY < <( compgen -W \"$" ++ safeName ++ "\" -- \"$cur\" ) ;;\n"); + try writer.writeAll(pad4 ++ "*) mapfile -t COMPREPLY < <( compgen -W \"$" ++ bashName ++ "\" -- \"$cur\" ) ;;\n"); try writer.writeAll( \\ esac \\ ;; @@ -231,8 +230,9 @@ fn writeBashCompletions(writer: anytype) !void { try writer.writeAll( \\ - \\_ghostty () { + \\_ghostty() { \\ cur=""; prev=""; prevWasEq=false; COMPREPLY=() + \\ ghostty="$1" \\ \\ if [ "$2" = "=" ]; then cur="" \\ else cur="$2" @@ -253,7 +253,7 @@ fn writeBashCompletions(writer: anytype) !void { \\ case "${COMP_WORDS[1]}" in \\ -e | --help | --version) return 0 ;; \\ --*) _handleConfig ;; - \\ *) mapfile -t COMPREPLY < <( compgen -W "${topLevel}" -- "$cur" ); appendSpaces ;; + \\ *) mapfile -t COMPREPLY < <( compgen -W "${topLevel}" -- "$cur" ); addSpaces ;; \\ esac \\ ;; \\ *) From ea181579dfa615c9f2a3abacfd018262993ff715 Mon Sep 17 00:00:00 2001 From: Anund Date: Sun, 15 Dec 2024 23:20:32 +1100 Subject: [PATCH 4/6] bash: add _files and _dirs to correctly add spaces to only the final result --- src/build/bash_completions.zig | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/build/bash_completions.zig b/src/build/bash_completions.zig index ddcb82401..5d1c4eb06 100644 --- a/src/build/bash_completions.zig +++ b/src/build/bash_completions.zig @@ -56,6 +56,30 @@ fn writeBashCompletions(writer: anytype) !void { \\ 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 + \\} + \\ \\config="--help" \\config+=" --version" \\ @@ -82,9 +106,9 @@ fn writeBashCompletions(writer: anytype) !void { 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("mapfile -t COMPREPLY < <( compgen -d -- \"$cur\" ); addSpaces ;;") + try writer.writeAll("_dirs ;;") else if (field.type == Config.RepeatablePath) - try writer.writeAll("mapfile -t COMPREPLY < <( compgen -f -- \"$cur\" ); addSpaces ;;") + try writer.writeAll("_files ;;") else { const compgenPrefix = "mapfile -t COMPREPLY < <( compgen -W \""; const compgenSuffix = "\" -- \"$cur\" ); addSpaces ;;"; @@ -194,7 +218,7 @@ fn writeBashCompletions(writer: anytype) !void { }, else => { if (std.mem.eql(u8, "config-file", opt.name)) { - try writer.writeAll("mapfile -t COMPREPLY < <( compgen -f -- \"$cur\" ); addSpaces ;;"); + try writer.writeAll("_files ;;"); } else try writer.writeAll("return;;"); }, } From f1728f594a681941b85a8d2fa8a136d625d9b633 Mon Sep 17 00:00:00 2001 From: Anund Date: Mon, 16 Dec 2024 08:22:42 +1100 Subject: [PATCH 5/6] bash: remove quoted completions while sorting possible portability issue --- src/build/bash_completions.zig | 57 +++------------------------------- 1 file changed, 5 insertions(+), 52 deletions(-) diff --git a/src/build/bash_completions.zig b/src/build/bash_completions.zig index 5d1c4eb06..03694bf8d 100644 --- a/src/build/bash_completions.zig +++ b/src/build/bash_completions.zig @@ -37,7 +37,6 @@ fn writeBashCompletions(writer: anytype) !void { const pad4 = pad3 ++ pad1; try writer.writeAll( - \\ \\# -o nospace requires we add back a space when a completion is finished \\# and not part of a --key= completion \\addSpaces() { @@ -46,40 +45,6 @@ fn writeBashCompletions(writer: anytype) !void { \\ 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 - \\} - \\ \\config="--help" \\config+=" --version" \\ @@ -102,13 +67,13 @@ fn writeBashCompletions(writer: anytype) !void { try writer.writeAll(pad2 ++ "--" ++ field.name ++ ") "); if (std.mem.startsWith(u8, field.name, "font-family")) - try writer.writeAll("_fonts ;;") + try writer.writeAll("return ;;") else if (std.mem.eql(u8, "theme", field.name)) - try writer.writeAll("_themes ;;") + try writer.writeAll("return ;;") else if (std.mem.eql(u8, "working-directory", field.name)) - try writer.writeAll("_dirs ;;") + try writer.writeAll("return ;;") else if (field.type == Config.RepeatablePath) - try writer.writeAll("_files ;;") + try writer.writeAll("return ;;") else { const compgenPrefix = "mapfile -t COMPREPLY < <( compgen -W \""; const compgenSuffix = "\" -- \"$cur\" ); addSpaces ;;"; @@ -218,7 +183,7 @@ fn writeBashCompletions(writer: anytype) !void { }, else => { if (std.mem.eql(u8, "config-file", opt.name)) { - try writer.writeAll("_files ;;"); + try writer.writeAll("return ;;"); } else try writer.writeAll("return;;"); }, } @@ -266,12 +231,6 @@ fn writeBashCompletions(writer: anytype) !void { \\ 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 @@ -284,12 +243,6 @@ fn writeBashCompletions(writer: anytype) !void { \\ 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: '-- ' - \\ # clear out prev so we don't run any of the key specific completions - \\ prev="" - \\ fi - \\ \\ case "${COMP_WORDS[1]}" in \\ --*) _handleConfig ;; \\ +*) _handleActions ;; From 79d2f508a9eb767f50ef2c30fbf5282357705e60 Mon Sep 17 00:00:00 2001 From: Anund Date: Mon, 16 Dec 2024 08:23:28 +1100 Subject: [PATCH 6/6] bash: support short form boolean options --- src/build/bash_completions.zig | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/build/bash_completions.zig b/src/build/bash_completions.zig index 03694bf8d..02da91ac4 100644 --- a/src/build/bash_completions.zig +++ b/src/build/bash_completions.zig @@ -52,7 +52,10 @@ fn writeBashCompletions(writer: anytype) !void { for (@typeInfo(Config).Struct.fields) |field| { if (field.name[0] == '_') continue; - try writer.writeAll("config+=\" --" ++ field.name ++ "=\"\n"); + switch (field.type) { + bool, ?bool => try writer.writeAll("config+=\" '--" ++ field.name ++ " '\"\n"), + else => try writer.writeAll("config+=\" --" ++ field.name ++ "=\"\n"), + } } try writer.writeAll( @@ -78,7 +81,7 @@ fn writeBashCompletions(writer: anytype) !void { const compgenPrefix = "mapfile -t COMPREPLY < <( compgen -W \""; const compgenSuffix = "\" -- \"$cur\" ); addSpaces ;;"; switch (@typeInfo(field.type)) { - .Bool => try writer.writeAll(compgenPrefix ++ "true false" ++ compgenSuffix), + .Bool => try writer.writeAll("return ;;"), .Enum => |info| { try writer.writeAll(compgenPrefix); for (info.fields, 0..) |f, i| { @@ -136,7 +139,10 @@ fn writeBashCompletions(writer: anytype) !void { for (@typeInfo(options).Struct.fields) |opt| { if (opt.name[0] == '_') continue; if (count > 0) try writer.writeAll(" "); - try writer.writeAll("--" ++ opt.name ++ "="); + switch (opt.type) { + bool, ?bool => try writer.writeAll("'--" ++ opt.name ++ " '"), + else => try writer.writeAll("--" ++ opt.name ++ "="), + } count += 1; } } @@ -172,7 +178,7 @@ fn writeBashCompletions(writer: anytype) !void { const compgenPrefix = "mapfile -t COMPREPLY < <( compgen -W \""; const compgenSuffix = "\" -- \"$cur\" ); addSpaces ;;"; switch (@typeInfo(opt.type)) { - .Bool => try writer.writeAll(compgenPrefix ++ "true false" ++ compgenSuffix), + .Bool => try writer.writeAll("return ;;"), .Enum => |info| { try writer.writeAll(compgenPrefix); for (info.opts, 0..) |f, i| {