Skip to content

Commit 10039da

Browse files
authored
bash: avoid mapfile for bash 3.2 compatibility (ghostty-org#10800)
We continue to support bash 3.2 for compatibility with /bin/bash on macOS. `mapfile` was introduced in bash 4.0, so this change introduces a `read -r`-based helper function for populating COMPREPLY from a list of lines. See: ghostty-org#3042
2 parents 8435dff + 54f2be8 commit 10039da

1 file changed

Lines changed: 20 additions & 12 deletions

File tree

src/extra/bash.zig

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ fn writeBashCompletions(writer: *std.Io.Writer) !void {
4040
try writer.writeAll(
4141
\\_ghostty() {
4242
\\
43+
\\ # compat: mapfile -t COMPREPLY < <( "$@" )
44+
\\ _compreply() {
45+
\\ COMPREPLY=()
46+
\\ while IFS='' read -r line; do COMPREPLY+=("$line"); done < <( "$@" )
47+
\\ }
48+
\\
4349
\\ # -o nospace requires we add back a space when a completion is finished
4450
\\ # and not part of a --key= completion
4551
\\ _add_spaces() {
@@ -50,16 +56,18 @@ fn writeBashCompletions(writer: *std.Io.Writer) !void {
5056
\\
5157
\\ _fonts() {
5258
\\ local IFS=$'\n'
53-
\\ mapfile -t COMPREPLY < <( compgen -P '"' -S '"' -W "$($ghostty +list-fonts | grep '^[A-Z]' )" -- "$cur")
59+
\\ COMPREPLY=()
60+
\\ while read -r line; do COMPREPLY+=("$line"); done < <( compgen -P '"' -S '"' -W "$($ghostty +list-fonts | grep '^[A-Z]' )" -- "$cur")
5461
\\ }
5562
\\
5663
\\ _themes() {
5764
\\ local IFS=$'\n'
58-
\\ mapfile -t COMPREPLY < <( compgen -P '"' -S '"' -W "$($ghostty +list-themes | sed -E 's/^(.*) \(.*$/\1/')" -- "$cur")
65+
\\ COMPREPLY=()
66+
\\ while read -r line; do COMPREPLY+=("$line"); done < <( compgen -P '"' -S '"' -W "$($ghostty +list-themes | sed -E 's/^(.*) \(.*$/\1/')" -- "$cur")
5967
\\ }
6068
\\
6169
\\ _files() {
62-
\\ mapfile -t COMPREPLY < <( compgen -o filenames -f -- "$cur" )
70+
\\ _compreply compgen -o filenames -f -- "$cur"
6371
\\ for i in "${!COMPREPLY[@]}"; do
6472
\\ if [[ -d "${COMPREPLY[i]}" ]]; then
6573
\\ COMPREPLY[i]="${COMPREPLY[i]}/";
@@ -71,7 +79,7 @@ fn writeBashCompletions(writer: *std.Io.Writer) !void {
7179
\\ }
7280
\\
7381
\\ _dirs() {
74-
\\ mapfile -t COMPREPLY < <( compgen -o dirnames -d -- "$cur" )
82+
\\ _compreply compgen -o dirnames -d -- "$cur"
7583
\\ for i in "${!COMPREPLY[@]}"; do
7684
\\ if [[ -d "${COMPREPLY[i]}" ]]; then
7785
\\ COMPREPLY[i]="${COMPREPLY[i]}/";
@@ -115,8 +123,8 @@ fn writeBashCompletions(writer: *std.Io.Writer) !void {
115123
else if (field.type == Config.RepeatablePath)
116124
try writer.writeAll("_files ;;")
117125
else {
118-
const compgenPrefix = "mapfile -t COMPREPLY < <( compgen -W \"";
119-
const compgenSuffix = "\" -- \"$cur\" ); _add_spaces ;;";
126+
const compgenPrefix = "_compreply compgen -W \"";
127+
const compgenSuffix = "\" -- \"$cur\"; _add_spaces ;;";
120128
switch (@typeInfo(field.type)) {
121129
.bool => try writer.writeAll("return ;;"),
122130
.@"enum" => |info| {
@@ -147,7 +155,7 @@ fn writeBashCompletions(writer: *std.Io.Writer) !void {
147155
}
148156

149157
try writer.writeAll(
150-
\\ *) mapfile -t COMPREPLY < <( compgen -W "$config" -- "$cur" ) ;;
158+
\\ *) _compreply compgen -W "$config" -- "$cur" ;;
151159
\\ esac
152160
\\
153161
\\ return 0
@@ -206,8 +214,8 @@ fn writeBashCompletions(writer: *std.Io.Writer) !void {
206214

207215
try writer.writeAll(pad5 ++ "--" ++ opt.name ++ ") ");
208216

209-
const compgenPrefix = "mapfile -t COMPREPLY < <( compgen -W \"";
210-
const compgenSuffix = "\" -- \"$cur\" ); _add_spaces ;;";
217+
const compgenPrefix = "_compreply compgen -W \"";
218+
const compgenSuffix = "\" -- \"$cur\"; _add_spaces ;;";
211219
switch (@typeInfo(opt.type)) {
212220
.bool => try writer.writeAll("return ;;"),
213221
.@"enum" => |info| {
@@ -243,7 +251,7 @@ fn writeBashCompletions(writer: *std.Io.Writer) !void {
243251
}
244252
try writer.writeAll("\n");
245253
}
246-
try writer.writeAll(pad5 ++ "*) mapfile -t COMPREPLY < <( compgen -W \"$" ++ bashName ++ "\" -- \"$cur\" ) ;;\n");
254+
try writer.writeAll(pad5 ++ "*) _compreply compgen -W \"$" ++ bashName ++ "\" -- \"$cur\" ;;\n");
247255
try writer.writeAll(
248256
\\ esac
249257
\\ ;;
@@ -252,7 +260,7 @@ fn writeBashCompletions(writer: *std.Io.Writer) !void {
252260
}
253261

254262
try writer.writeAll(
255-
\\ *) mapfile -t COMPREPLY < <( compgen -W "--help" -- "$cur" ) ;;
263+
\\ *) _compreply compgen -W "--help" -- "$cur" ;;
256264
\\ esac
257265
\\
258266
\\ return 0
@@ -298,7 +306,7 @@ fn writeBashCompletions(writer: *std.Io.Writer) !void {
298306
\\ case "${COMP_WORDS[1]}" in
299307
\\ -e | --help | --version) return 0 ;;
300308
\\ --*) _handle_config ;;
301-
\\ *) mapfile -t COMPREPLY < <( compgen -W "${topLevel}" -- "$cur" ); _add_spaces ;;
309+
\\ *) _compreply compgen -W "${topLevel}" -- "$cur"; _add_spaces ;;
302310
\\ esac
303311
\\ ;;
304312
\\ *)

0 commit comments

Comments
 (0)