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,11 +35,14 @@ fn writeBashCompletions(writer: anytype) !void {
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
\\addSpaces() {
\\ _add_spaces() {
\\ for idx in "${!COMPREPLY[@]}"; do
\\ [ -n "${COMPREPLY[idx]}" ] && COMPREPLY[idx]="${COMPREPLY[idx]} ";
\\ done
@ -79,7 +82,8 @@ fn writeBashCompletions(writer: anytype) !void {
\\ fi
\\ }
\\
\\config="--help"
\\ _handle_config() {
\\ local config="--help"
\\ config+=" --version"
\\
);
@ -87,21 +91,20 @@ fn writeBashCompletions(writer: anytype) !void {
for (@typeInfo(Config).Struct.fields) |field| {
if (field.name[0] == '_') continue;
switch (field.type) {
bool, ?bool => try writer.writeAll("config+=\" '--" ++ field.name ++ " '\"\n"),
else => try writer.writeAll("config+=\" --" ++ field.name ++ "=\"\n"),
bool, ?bool => try writer.writeAll(pad2 ++ "config+=\" '--" ++ field.name ++ " '\"\n"),
else => try writer.writeAll(pad2 ++ "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 ++ ") ");
try writer.writeAll(pad3 ++ "--" ++ field.name ++ ") ");
if (std.mem.startsWith(u8, field.name, "font-family"))
try writer.writeAll("_fonts ;;")
@ -113,7 +116,7 @@ fn writeBashCompletions(writer: anytype) !void {
try writer.writeAll("_files ;;")
else {
const compgenPrefix = "mapfile -t COMPREPLY < <( compgen -W \"";
const compgenSuffix = "\" -- \"$cur\" ); addSpaces ;;";
const compgenSuffix = "\" -- \"$cur\" ); _add_spaces ;;";
switch (@typeInfo(field.type)) {
.Bool => try writer.writeAll("return ;;"),
.Enum => |info| {
@ -150,6 +153,7 @@ fn writeBashCompletions(writer: anytype) !void {
\\ return 0
\\ }
\\
\\ _handle_actions() {
\\
);
@ -166,7 +170,7 @@ fn writeBashCompletions(writer: anytype) !void {
@memcpy(bashName, field.name);
std.mem.replaceScalar(u8, bashName, '-', '_');
try writer.writeAll(bashName ++ "=\"");
try writer.writeAll(pad2 ++ "local " ++ bashName ++ "=\"");
{
var count = 0;
@ -185,7 +189,6 @@ fn writeBashCompletions(writer: anytype) !void {
try writer.writeAll(
\\
\\_handleActions() {
\\ case "${COMP_WORDS[1]}" in
\\
);
@ -202,15 +205,15 @@ fn writeBashCompletions(writer: anytype) !void {
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");
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(pad4 ++ "--" ++ opt.name ++ ") ");
try writer.writeAll(pad5 ++ "--" ++ opt.name ++ ") ");
const compgenPrefix = "mapfile -t COMPREPLY < <( compgen -W \"";
const compgenSuffix = "\" -- \"$cur\" ); addSpaces ;;";
const compgenSuffix = "\" -- \"$cur\" ); _add_spaces ;;";
switch (@typeInfo(opt.type)) {
.Bool => try writer.writeAll("return ;;"),
.Enum => |info| {
@ -246,7 +249,7 @@ fn writeBashCompletions(writer: anytype) !void {
}
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(
\\ esac
\\ ;;
@ -261,7 +264,8 @@ fn writeBashCompletions(writer: anytype) !void {
\\ return 0
\\ }
\\
\\topLevel="-e"
\\ # begin main logic
\\ local topLevel="-e"
\\ 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, "version", field.name)) continue;
try writer.writeAll("topLevel+=\" +" ++ field.name ++ "\"\n");
try writer.writeAll(pad1 ++ "topLevel+=\" +" ++ field.name ++ "\"\n");
}
try writer.writeAll(
\\
\\_ghostty() {
\\ cur=""; prev=""; prevWasEq=false; COMPREPLY=()
\\ ghostty="$1"
\\ 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.
@ -303,8 +306,8 @@ fn writeBashCompletions(writer: anytype) !void {
\\ 1)
\\ case "${COMP_WORDS[1]}" in
\\ -e | --help | --version) return 0 ;;
\\ --*) _handleConfig ;;
\\ *) mapfile -t COMPREPLY < <( compgen -W "${topLevel}" -- "$cur" ); addSpaces ;;
\\ --*) _handle_config ;;
\\ *) mapfile -t COMPREPLY < <( compgen -W "${topLevel}" -- "$cur" ); _add_spaces ;;
\\ esac
\\ ;;
\\ *)
@ -318,8 +321,8 @@ fn writeBashCompletions(writer: anytype) !void {
\\ fi
\\
\\ case "${COMP_WORDS[1]}" in
\\ --*) _handleConfig ;;
\\ +*) _handleActions ;;
\\ --*) _handle_config ;;
\\ +*) _handle_actions ;;
\\ esac
\\ ;;
\\ esac