bash: move functions and variables out of global scope (#3060)

Leave only `_ghostty` as a bash global.

I agree to re-license my commits to MIT
This commit is contained in:
Mitchell Hashimoto
2024-12-21 17:11:10 -08:00
committed by GitHub

View File

@ -35,73 +35,76 @@ fn writeBashCompletions(writer: anytype) !void {
const pad2 = pad1 ++ pad1; const pad2 = pad1 ++ pad1;
const pad3 = pad2 ++ pad1; const pad3 = pad2 ++ pad1;
const pad4 = pad3 ++ pad1; const pad4 = pad3 ++ pad1;
const pad5 = pad4 ++ pad1;
try writer.writeAll( try writer.writeAll(
\\# -o nospace requires we add back a space when a completion is finished \\_ghostty() {
\\# and not part of a --key= completion
\\addSpaces() {
\\ for idx in "${!COMPREPLY[@]}"; do
\\ [ -n "${COMPREPLY[idx]}" ] && COMPREPLY[idx]="${COMPREPLY[idx]} ";
\\ done
\\}
\\ \\
\\_fonts() { \\ # -o nospace requires we add back a space when a completion is finished
\\ local IFS=$'\n' \\ # and not part of a --key= completion
\\ mapfile -t COMPREPLY < <( compgen -P '"' -S '"' -W "$($ghostty +list-fonts | grep '^[A-Z]' )" -- "$cur") \\ _add_spaces() {
\\} \\ for idx in "${!COMPREPLY[@]}"; do
\\ [ -n "${COMPREPLY[idx]}" ] && COMPREPLY[idx]="${COMPREPLY[idx]} ";
\\ done
\\ }
\\ \\
\\_themes() { \\ _fonts() {
\\ local IFS=$'\n' \\ 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-fonts | grep '^[A-Z]' )" -- "$cur")
\\} \\ }
\\ \\
\\_files() { \\ _themes() {
\\ mapfile -t COMPREPLY < <( compgen -o filenames -f -- "$cur" ) \\ local IFS=$'\n'
\\ for i in "${!COMPREPLY[@]}"; do \\ mapfile -t COMPREPLY < <( compgen -P '"' -S '"' -W "$($ghostty +list-themes | sed -E 's/^(.*) \(.*$/\1/')" -- "$cur")
\\ if [[ -d "${COMPREPLY[i]}" ]]; then \\ }
\\ COMPREPLY[i]="${COMPREPLY[i]}/"; \\
\\ _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 \\ fi
\\ if [[ -f "${COMPREPLY[i]}" ]]; then \\ }
\\ COMPREPLY[i]="${COMPREPLY[i]} ";
\\ fi
\\ done
\\}
\\ \\
\\_dirs() { \\ _handle_config() {
\\ mapfile -t COMPREPLY < <( compgen -o dirnames -d -- "$cur" ) \\ local config="--help"
\\ for i in "${!COMPREPLY[@]}"; do \\ config+=" --version"
\\ if [[ -d "${COMPREPLY[i]}" ]]; then
\\ COMPREPLY[i]="${COMPREPLY[i]}/";
\\ fi
\\ done
\\ if [[ "${#COMPREPLY[@]}" == 0 && -d "$cur" ]]; then
\\ COMPREPLY=( "$cur " )
\\ fi
\\}
\\
\\config="--help"
\\config+=" --version"
\\ \\
); );
for (@typeInfo(Config).Struct.fields) |field| { for (@typeInfo(Config).Struct.fields) |field| {
if (field.name[0] == '_') continue; if (field.name[0] == '_') continue;
switch (field.type) { switch (field.type) {
bool, ?bool => try writer.writeAll("config+=\" '--" ++ field.name ++ " '\"\n"), bool, ?bool => try writer.writeAll(pad2 ++ "config+=\" '--" ++ field.name ++ " '\"\n"),
else => try writer.writeAll("config+=\" --" ++ field.name ++ "=\"\n"), else => try writer.writeAll(pad2 ++ "config+=\" --" ++ field.name ++ "=\"\n"),
} }
} }
try writer.writeAll( try writer.writeAll(
\\ \\
\\_handleConfig() { \\ case "$prev" in
\\ case "$prev" in
\\ \\
); );
for (@typeInfo(Config).Struct.fields) |field| { for (@typeInfo(Config).Struct.fields) |field| {
if (field.name[0] == '_') continue; if (field.name[0] == '_') continue;
try writer.writeAll(pad2 ++ "--" ++ field.name ++ ") "); try writer.writeAll(pad3 ++ "--" ++ field.name ++ ") ");
if (std.mem.startsWith(u8, field.name, "font-family")) if (std.mem.startsWith(u8, field.name, "font-family"))
try writer.writeAll("_fonts ;;") try writer.writeAll("_fonts ;;")
@ -113,7 +116,7 @@ fn writeBashCompletions(writer: anytype) !void {
try writer.writeAll("_files ;;") try writer.writeAll("_files ;;")
else { else {
const compgenPrefix = "mapfile -t COMPREPLY < <( compgen -W \""; const compgenPrefix = "mapfile -t COMPREPLY < <( compgen -W \"";
const compgenSuffix = "\" -- \"$cur\" ); addSpaces ;;"; const compgenSuffix = "\" -- \"$cur\" ); _add_spaces ;;";
switch (@typeInfo(field.type)) { switch (@typeInfo(field.type)) {
.Bool => try writer.writeAll("return ;;"), .Bool => try writer.writeAll("return ;;"),
.Enum => |info| { .Enum => |info| {
@ -144,12 +147,13 @@ fn writeBashCompletions(writer: anytype) !void {
} }
try writer.writeAll( try writer.writeAll(
\\ *) mapfile -t COMPREPLY < <( compgen -W "$config" -- "$cur" ) ;; \\ *) mapfile -t COMPREPLY < <( compgen -W "$config" -- "$cur" ) ;;
\\ esac \\ esac
\\ \\
\\ return 0 \\ return 0
\\} \\ }
\\ \\
\\ _handle_actions() {
\\ \\
); );
@ -166,7 +170,7 @@ fn writeBashCompletions(writer: anytype) !void {
@memcpy(bashName, field.name); @memcpy(bashName, field.name);
std.mem.replaceScalar(u8, bashName, '-', '_'); std.mem.replaceScalar(u8, bashName, '-', '_');
try writer.writeAll(bashName ++ "=\""); try writer.writeAll(pad2 ++ "local " ++ bashName ++ "=\"");
{ {
var count = 0; var count = 0;
@ -185,8 +189,7 @@ fn writeBashCompletions(writer: anytype) !void {
try writer.writeAll( try writer.writeAll(
\\ \\
\\_handleActions() { \\ case "${COMP_WORDS[1]}" in
\\ case "${COMP_WORDS[1]}" in
\\ \\
); );
@ -202,15 +205,15 @@ fn writeBashCompletions(writer: anytype) !void {
const bashName: []u8 = buffer[0..field.name.len]; const bashName: []u8 = buffer[0..field.name.len];
_ = std.mem.replace(u8, field.name, "-", "_", bashName); _ = std.mem.replace(u8, field.name, "-", "_", bashName);
try writer.writeAll(pad2 ++ "+" ++ field.name ++ ")\n"); try writer.writeAll(pad3 ++ "+" ++ field.name ++ ")\n");
try writer.writeAll(pad3 ++ "case $prev in\n"); try writer.writeAll(pad4 ++ "case $prev in\n");
for (@typeInfo(options).Struct.fields) |opt| { for (@typeInfo(options).Struct.fields) |opt| {
if (opt.name[0] == '_') continue; if (opt.name[0] == '_') continue;
try writer.writeAll(pad4 ++ "--" ++ opt.name ++ ") "); try writer.writeAll(pad5 ++ "--" ++ opt.name ++ ") ");
const compgenPrefix = "mapfile -t COMPREPLY < <( compgen -W \""; const compgenPrefix = "mapfile -t COMPREPLY < <( compgen -W \"";
const compgenSuffix = "\" -- \"$cur\" ); addSpaces ;;"; const compgenSuffix = "\" -- \"$cur\" ); _add_spaces ;;";
switch (@typeInfo(opt.type)) { switch (@typeInfo(opt.type)) {
.Bool => try writer.writeAll("return ;;"), .Bool => try writer.writeAll("return ;;"),
.Enum => |info| { .Enum => |info| {
@ -246,24 +249,25 @@ fn writeBashCompletions(writer: anytype) !void {
} }
try writer.writeAll("\n"); try writer.writeAll("\n");
} }
try writer.writeAll(pad4 ++ "*) mapfile -t COMPREPLY < <( compgen -W \"$" ++ bashName ++ "\" -- \"$cur\" ) ;;\n"); try writer.writeAll(pad5 ++ "*) mapfile -t COMPREPLY < <( compgen -W \"$" ++ bashName ++ "\" -- \"$cur\" ) ;;\n");
try writer.writeAll( try writer.writeAll(
\\ esac \\ esac
\\ ;; \\ ;;
\\ \\
); );
} }
try writer.writeAll( try writer.writeAll(
\\ *) mapfile -t COMPREPLY < <( compgen -W "--help" -- "$cur" ) ;; \\ *) mapfile -t COMPREPLY < <( compgen -W "--help" -- "$cur" ) ;;
\\ esac \\ esac
\\ \\
\\ return 0 \\ return 0
\\} \\ }
\\ \\
\\topLevel="-e" \\ # begin main logic
\\topLevel+=" --help" \\ local topLevel="-e"
\\topLevel+=" --version" \\ topLevel+=" --help"
\\ topLevel+=" --version"
\\ \\
); );
@ -271,14 +275,13 @@ fn writeBashCompletions(writer: anytype) !void {
if (std.mem.eql(u8, "help", field.name)) continue; if (std.mem.eql(u8, "help", field.name)) continue;
if (std.mem.eql(u8, "version", field.name)) continue; if (std.mem.eql(u8, "version", field.name)) continue;
try writer.writeAll("topLevel+=\" +" ++ field.name ++ "\"\n"); try writer.writeAll(pad1 ++ "topLevel+=\" +" ++ field.name ++ "\"\n");
} }
try writer.writeAll( try writer.writeAll(
\\ \\
\\_ghostty() { \\ local cur=""; local prev=""; local prevWasEq=false; COMPREPLY=()
\\ cur=""; prev=""; prevWasEq=false; COMPREPLY=() \\ local ghostty="$1"
\\ ghostty="$1"
\\ \\
\\ # script assumes default COMP_WORDBREAKS of roughly $' \t\n"\'><=;|&(:' \\ # script assumes default COMP_WORDBREAKS of roughly $' \t\n"\'><=;|&(:'
\\ # if = is missing this script will degrade to matching on keys only. \\ # if = is missing this script will degrade to matching on keys only.
@ -303,8 +306,8 @@ fn writeBashCompletions(writer: anytype) !void {
\\ 1) \\ 1)
\\ case "${COMP_WORDS[1]}" in \\ case "${COMP_WORDS[1]}" in
\\ -e | --help | --version) return 0 ;; \\ -e | --help | --version) return 0 ;;
\\ --*) _handleConfig ;; \\ --*) _handle_config ;;
\\ *) mapfile -t COMPREPLY < <( compgen -W "${topLevel}" -- "$cur" ); addSpaces ;; \\ *) mapfile -t COMPREPLY < <( compgen -W "${topLevel}" -- "$cur" ); _add_spaces ;;
\\ esac \\ esac
\\ ;; \\ ;;
\\ *) \\ *)
@ -318,8 +321,8 @@ fn writeBashCompletions(writer: anytype) !void {
\\ fi \\ fi
\\ \\
\\ case "${COMP_WORDS[1]}" in \\ case "${COMP_WORDS[1]}" in
\\ --*) _handleConfig ;; \\ --*) _handle_config ;;
\\ +*) _handleActions ;; \\ +*) _handle_actions ;;
\\ esac \\ esac
\\ ;; \\ ;;
\\ esac \\ esac