bash: formatting changes, change to reference binary name via variable

This commit is contained in:
Anund
2024-12-15 20:13:45 +11:00
parent c02789205e
commit 361967f721

View File

@ -37,24 +37,23 @@ fn writeBashCompletions(writer: anytype) !void {
const pad4 = pad3 ++ pad1; const pad4 = pad3 ++ pad1;
try writer.writeAll( try writer.writeAll(
\\#!/usr/bin/env bash
\\ \\
\\# -o nospace requires we add back a space when a completion is finished \\# -o nospace requires we add back a space when a completion is finished
\\# and not part of a --key= completion \\# and not part of a --key= completion
\\appendSpaces () { \\addSpaces() {
\\ for idx in "${!COMPREPLY[@]}"; do \\ for idx in "${!COMPREPLY[@]}"; do
\\ [ -n "${COMPREPLY[idx]}" ] && COMPREPLY[idx]="${COMPREPLY[idx]} "; \\ [ -n "${COMPREPLY[idx]}" ] && COMPREPLY[idx]="${COMPREPLY[idx]} ";
\\ done \\ done
\\} \\}
\\ \\
\\_fonts () { \\_fonts() {
\\ local IFS=$'\n' \\ 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() { \\_themes() {
\\ 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-themes | sed -E 's/^(.*) \(.*$/\1/')" -- "$cur")
\\} \\}
\\ \\
\\config="--help" \\config="--help"
@ -83,12 +82,12 @@ fn writeBashCompletions(writer: anytype) !void {
else if (std.mem.eql(u8, "theme", field.name)) else if (std.mem.eql(u8, "theme", field.name))
try writer.writeAll("_themes ;;") try writer.writeAll("_themes ;;")
else if (std.mem.eql(u8, "working-directory", field.name)) 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) 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 { else {
const compgenPrefix = "mapfile -t COMPREPLY < <( compgen -W \""; const compgenPrefix = "mapfile -t COMPREPLY < <( compgen -W \"";
const compgenSuffix = "\" -- \"$cur\" ); appendSpaces ;;"; const compgenSuffix = "\" -- \"$cur\" ); addSpaces ;;";
switch (@typeInfo(field.type)) { switch (@typeInfo(field.type)) {
.Bool => try writer.writeAll(compgenPrefix ++ "true false" ++ compgenSuffix), .Bool => try writer.writeAll(compgenPrefix ++ "true false" ++ compgenSuffix),
.Enum => |info| { .Enum => |info| {
@ -137,11 +136,11 @@ fn writeBashCompletions(writer: anytype) !void {
if (@typeInfo(options).Struct.fields.len == 0) continue; if (@typeInfo(options).Struct.fields.len == 0) continue;
var buffer: [field.name.len]u8 = undefined; var buffer: [field.name.len]u8 = undefined;
const safeName: []u8 = buffer[0..field.name.len]; const bashName: []u8 = buffer[0..field.name.len];
@memcpy(safeName, field.name); @memcpy(bashName, field.name);
std.mem.replaceScalar(u8, safeName, '-', '_'); std.mem.replaceScalar(u8, bashName, '-', '_');
try writer.writeAll(safeName ++ "=\""); try writer.writeAll(bashName ++ "=\"");
{ {
var count = 0; var count = 0;
@ -157,7 +156,7 @@ fn writeBashCompletions(writer: anytype) !void {
try writer.writeAll( try writer.writeAll(
\\ \\
\\_handleActions () { \\_handleActions() {
\\ case "${COMP_WORDS[1]}" in \\ case "${COMP_WORDS[1]}" in
\\ \\
); );
@ -171,8 +170,8 @@ fn writeBashCompletions(writer: anytype) !void {
// bash doesn't allow variable names containing '-' so replace them // bash doesn't allow variable names containing '-' so replace them
var buffer: [field.name.len]u8 = undefined; var buffer: [field.name.len]u8 = undefined;
const safeName: []u8 = buffer[0..field.name.len]; const bashName: []u8 = buffer[0..field.name.len];
_ = std.mem.replace(u8, field.name, "-", "_", safeName); _ = std.mem.replace(u8, field.name, "-", "_", bashName);
try writer.writeAll(pad2 ++ "+" ++ field.name ++ ")\n"); try writer.writeAll(pad2 ++ "+" ++ field.name ++ ")\n");
try writer.writeAll(pad3 ++ "case $prev in\n"); try writer.writeAll(pad3 ++ "case $prev in\n");
@ -182,7 +181,7 @@ fn writeBashCompletions(writer: anytype) !void {
try writer.writeAll(pad4 ++ "--" ++ opt.name ++ ") "); try writer.writeAll(pad4 ++ "--" ++ opt.name ++ ") ");
const compgenPrefix = "mapfile -t COMPREPLY < <( compgen -W \""; const compgenPrefix = "mapfile -t COMPREPLY < <( compgen -W \"";
const compgenSuffix = "\" -- \"$cur\" ); appendSpaces ;;"; const compgenSuffix = "\" -- \"$cur\" ); addSpaces ;;";
switch (@typeInfo(opt.type)) { switch (@typeInfo(opt.type)) {
.Bool => try writer.writeAll(compgenPrefix ++ "true false" ++ compgenSuffix), .Bool => try writer.writeAll(compgenPrefix ++ "true false" ++ compgenSuffix),
.Enum => |info| { .Enum => |info| {
@ -195,13 +194,13 @@ fn writeBashCompletions(writer: anytype) !void {
}, },
else => { else => {
if (std.mem.eql(u8, "config-file", opt.name)) { 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;;"); } else try writer.writeAll("return;;");
}, },
} }
try writer.writeAll("\n"); 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( try writer.writeAll(
\\ esac \\ esac
\\ ;; \\ ;;
@ -231,8 +230,9 @@ fn writeBashCompletions(writer: anytype) !void {
try writer.writeAll( try writer.writeAll(
\\ \\
\\_ghostty () { \\_ghostty() {
\\ cur=""; prev=""; prevWasEq=false; COMPREPLY=() \\ cur=""; prev=""; prevWasEq=false; COMPREPLY=()
\\ ghostty="$1"
\\ \\
\\ if [ "$2" = "=" ]; then cur="" \\ if [ "$2" = "=" ]; then cur=""
\\ else cur="$2" \\ else cur="$2"
@ -253,7 +253,7 @@ fn writeBashCompletions(writer: anytype) !void {
\\ case "${COMP_WORDS[1]}" in \\ case "${COMP_WORDS[1]}" in
\\ -e | --help | --version) return 0 ;; \\ -e | --help | --version) return 0 ;;
\\ --*) _handleConfig ;; \\ --*) _handleConfig ;;
\\ *) mapfile -t COMPREPLY < <( compgen -W "${topLevel}" -- "$cur" ); appendSpaces ;; \\ *) mapfile -t COMPREPLY < <( compgen -W "${topLevel}" -- "$cur" ); addSpaces ;;
\\ esac \\ esac
\\ ;; \\ ;;
\\ *) \\ *)