From 405db404869ee1ad420ebb5343a4b99eae60ac11 Mon Sep 17 00:00:00 2001 From: wozrer Date: Fri, 6 Dec 2024 03:53:12 +0300 Subject: [PATCH 01/12] Create misc/bash_completion.sh --- misc/bash_completion.sh | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100755 misc/bash_completion.sh diff --git a/misc/bash_completion.sh b/misc/bash_completion.sh new file mode 100755 index 000000000..1adafff13 --- /dev/null +++ b/misc/bash_completion.sh @@ -0,0 +1,14 @@ +_luanti() { + local cur prev opts + + COMPREPLY=() + cur="${COMP_WORDS[COMP_CWORD]}" + prev="${COMP_WORDS[COMP_CWORD-1]}" + + opts="--address --color --config --console --debugger --gameid --go --help --info --logfile --map-dir --migrate --migrate-auth --migrate-mod-storage --migrate-players --name --password --password-file --port --quiet --random-input --recompress --run-benchmarks --run-unittests --server --terminal --test-module --trace --verbose --version --world --worldlist --worldname" + + + COMPREPLY=($(compgen -W "$opts" -- "$cur")) +} + +complete -F _luanti luanti From a88c3a0cac9e49529afb76cdf60922a2f99329e0 Mon Sep 17 00:00:00 2001 From: wozrer Date: Fri, 6 Dec 2024 03:53:42 +0300 Subject: [PATCH 02/12] Add bash completion to install list in cmake --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index de97d49bf..606aad57d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -268,6 +268,7 @@ if(UNIX AND NOT APPLE) install(FILES "misc/luanti-xorg-icon-128.png" DESTINATION "${ICONDIR}/hicolor/128x128/apps" RENAME "luanti.png") + install(FILES "misc/bash_completion.sh" DESTINATION "share/bash-completion/completions" RENAME "luanti") endif() if(APPLE) From 5fab1a759bcd48268a56cca77f0f5c4011b3a6c9 Mon Sep 17 00:00:00 2001 From: wozrer Date: Fri, 6 Dec 2024 19:07:46 +0300 Subject: [PATCH 03/12] Better --color arg --- misc/bash_completion.sh | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/misc/bash_completion.sh b/misc/bash_completion.sh index 1adafff13..e83d9d05e 100755 --- a/misc/bash_completion.sh +++ b/misc/bash_completion.sh @@ -1,14 +1,19 @@ _luanti() { - local cur prev opts + local cur prev opts color_values COMPREPLY=() cur="${COMP_WORDS[COMP_CWORD]}" prev="${COMP_WORDS[COMP_CWORD-1]}" opts="--address --color --config --console --debugger --gameid --go --help --info --logfile --map-dir --migrate --migrate-auth --migrate-mod-storage --migrate-players --name --password --password-file --port --quiet --random-input --recompress --run-benchmarks --run-unittests --server --terminal --test-module --trace --verbose --version --world --worldlist --worldname" + color_values="always never auto" - COMPREPLY=($(compgen -W "$opts" -- "$cur")) + if [[ "$prev" == "--color" ]]; then + COMPREPLY=($(compgen -W "$color_values" -- "$cur")) + else + COMPREPLY=($(compgen -W "$opts" -- "$cur")) + fi } complete -F _luanti luanti From 16ced2a5175c276b9e5a8b31787d0e6d322cff68 Mon Sep 17 00:00:00 2001 From: wozrer Date: Fri, 6 Dec 2024 19:18:25 +0300 Subject: [PATCH 04/12] Better --logfile arg --- misc/bash_completion.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/misc/bash_completion.sh b/misc/bash_completion.sh index e83d9d05e..be36cb40d 100755 --- a/misc/bash_completion.sh +++ b/misc/bash_completion.sh @@ -11,6 +11,8 @@ _luanti() { if [[ "$prev" == "--color" ]]; then COMPREPLY=($(compgen -W "$color_values" -- "$cur")) + elif [[ "$prev" == "--logfile" ]]; then + COMPREPLY=($(compgen -f "$cur")) else COMPREPLY=($(compgen -W "$opts" -- "$cur")) fi From b90d290272286ff22c23ec6f9d44f02c1f7dd4f3 Mon Sep 17 00:00:00 2001 From: wozrer Date: Fri, 6 Dec 2024 20:12:50 +0300 Subject: [PATCH 05/12] Better completion for commands with file --- misc/bash_completion.sh | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/misc/bash_completion.sh b/misc/bash_completion.sh index be36cb40d..f86b4564a 100755 --- a/misc/bash_completion.sh +++ b/misc/bash_completion.sh @@ -1,18 +1,19 @@ _luanti() { - local cur prev opts color_values + local cur prev opts color_values file_opts COMPREPLY=() cur="${COMP_WORDS[COMP_CWORD]}" prev="${COMP_WORDS[COMP_CWORD-1]}" opts="--address --color --config --console --debugger --gameid --go --help --info --logfile --map-dir --migrate --migrate-auth --migrate-mod-storage --migrate-players --name --password --password-file --port --quiet --random-input --recompress --run-benchmarks --run-unittests --server --terminal --test-module --trace --verbose --version --world --worldlist --worldname" + file_opts="--config --logfile --map-dir --password-file --world" color_values="always never auto" if [[ "$prev" == "--color" ]]; then COMPREPLY=($(compgen -W "$color_values" -- "$cur")) - elif [[ "$prev" == "--logfile" ]]; then - COMPREPLY=($(compgen -f "$cur")) + elif [[ " ${file_opts[*]} " == *" ${prev} "* ]]; then + _comp_compgen_filedir else COMPREPLY=($(compgen -W "$opts" -- "$cur")) fi From 61d9bcbee1f68678a0a1413298e3648e99559f82 Mon Sep 17 00:00:00 2001 From: wozrer Date: Fri, 6 Dec 2024 20:14:48 +0300 Subject: [PATCH 06/12] Alphabetize color_values --- misc/bash_completion.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/misc/bash_completion.sh b/misc/bash_completion.sh index f86b4564a..514d9f171 100755 --- a/misc/bash_completion.sh +++ b/misc/bash_completion.sh @@ -7,7 +7,7 @@ _luanti() { opts="--address --color --config --console --debugger --gameid --go --help --info --logfile --map-dir --migrate --migrate-auth --migrate-mod-storage --migrate-players --name --password --password-file --port --quiet --random-input --recompress --run-benchmarks --run-unittests --server --terminal --test-module --trace --verbose --version --world --worldlist --worldname" file_opts="--config --logfile --map-dir --password-file --world" - color_values="always never auto" + color_values="always auto never" if [[ "$prev" == "--color" ]]; then From 95365c86a46f72408308b15e0a8ef5d618c20a55 Mon Sep 17 00:00:00 2001 From: wozrer Date: Fri, 6 Dec 2024 20:52:56 +0300 Subject: [PATCH 07/12] Better completion for gameid --- misc/bash_completion.sh | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/misc/bash_completion.sh b/misc/bash_completion.sh index 514d9f171..23d450800 100755 --- a/misc/bash_completion.sh +++ b/misc/bash_completion.sh @@ -1,3 +1,10 @@ +__gameid_list() { + # FIXME: Why luanti --gameid list returns list into stderr? + output=($(eval "luanti --gameid list 2>&1")) + output+=("list") + eval "$1=(\"${output[@]}\")" +} + _luanti() { local cur prev opts color_values file_opts @@ -12,6 +19,10 @@ _luanti() { if [[ "$prev" == "--color" ]]; then COMPREPLY=($(compgen -W "$color_values" -- "$cur")) + elif [[ "$prev" == "--gameid" ]]; then + local gameid_values + __gameid_list gameid_values + COMPREPLY=($(compgen -W "$gameid_values" -- "$cur")) elif [[ " ${file_opts[*]} " == *" ${prev} "* ]]; then _comp_compgen_filedir else From b2cdcdc2a75374e7d9ce25673e5fb681404c46ae Mon Sep 17 00:00:00 2001 From: wozrer Date: Fri, 6 Dec 2024 21:01:42 +0300 Subject: [PATCH 08/12] Better completion for worldlist --- misc/bash_completion.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/misc/bash_completion.sh b/misc/bash_completion.sh index 23d450800..e258c7694 100755 --- a/misc/bash_completion.sh +++ b/misc/bash_completion.sh @@ -6,7 +6,7 @@ __gameid_list() { } _luanti() { - local cur prev opts color_values file_opts + local cur prev opts file_opts color_values worldlist_values COMPREPLY=() cur="${COMP_WORDS[COMP_CWORD]}" @@ -15,10 +15,13 @@ _luanti() { opts="--address --color --config --console --debugger --gameid --go --help --info --logfile --map-dir --migrate --migrate-auth --migrate-mod-storage --migrate-players --name --password --password-file --port --quiet --random-input --recompress --run-benchmarks --run-unittests --server --terminal --test-module --trace --verbose --version --world --worldlist --worldname" file_opts="--config --logfile --map-dir --password-file --world" color_values="always auto never" + worldlist_values="both name path" if [[ "$prev" == "--color" ]]; then COMPREPLY=($(compgen -W "$color_values" -- "$cur")) + elif [[ "$prev" == "--worldlist" ]]; then + COMPREPLY=($(compgen -W "$worldlist_values" -- "$cur")) elif [[ "$prev" == "--gameid" ]]; then local gameid_values __gameid_list gameid_values From 190c1de4048c93393fe45e2acb736369374bd9a9 Mon Sep 17 00:00:00 2001 From: wozrer Date: Fri, 6 Dec 2024 21:28:07 +0300 Subject: [PATCH 09/12] Better completion for worldname --- misc/bash_completion.sh | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/misc/bash_completion.sh b/misc/bash_completion.sh index e258c7694..0b6fc4a03 100755 --- a/misc/bash_completion.sh +++ b/misc/bash_completion.sh @@ -5,6 +5,12 @@ __gameid_list() { eval "$1=(\"${output[@]}\")" } +__worldname_list() { + string=$(eval "luanti --worldlist name") + output=$(awk 'NR>1{print $0}' <<< "$string") + eval "$1=(\"${output[@]}\")" +} + _luanti() { local cur prev opts file_opts color_values worldlist_values @@ -26,6 +32,10 @@ _luanti() { local gameid_values __gameid_list gameid_values COMPREPLY=($(compgen -W "$gameid_values" -- "$cur")) + elif [[ "$prev" == "--worldname" ]]; then + local worldname_values + __worldname_list worldname_values + COMPREPLY=($(compgen -W "$worldname_values" -- "$cur")) elif [[ " ${file_opts[*]} " == *" ${prev} "* ]]; then _comp_compgen_filedir else From c06d61235969b8c260d0d165c4473ecb6f4dd6c2 Mon Sep 17 00:00:00 2001 From: wozrer Date: Sat, 7 Dec 2024 17:33:21 +0300 Subject: [PATCH 10/12] Refactor gameid arg --- misc/bash_completion.sh | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/misc/bash_completion.sh b/misc/bash_completion.sh index 0b6fc4a03..425543b21 100755 --- a/misc/bash_completion.sh +++ b/misc/bash_completion.sh @@ -1,10 +1,3 @@ -__gameid_list() { - # FIXME: Why luanti --gameid list returns list into stderr? - output=($(eval "luanti --gameid list 2>&1")) - output+=("list") - eval "$1=(\"${output[@]}\")" -} - __worldname_list() { string=$(eval "luanti --worldlist name") output=$(awk 'NR>1{print $0}' <<< "$string") @@ -29,9 +22,8 @@ _luanti() { elif [[ "$prev" == "--worldlist" ]]; then COMPREPLY=($(compgen -W "$worldlist_values" -- "$cur")) elif [[ "$prev" == "--gameid" ]]; then - local gameid_values - __gameid_list gameid_values - COMPREPLY=($(compgen -W "$gameid_values" -- "$cur")) + # FIXME: Why luanti --gameid list returns list into stderr? + COMPREPLY=($(compgen -W "list $(luanti --gameid list 2>&1)" -- "$cur")) elif [[ "$prev" == "--worldname" ]]; then local worldname_values __worldname_list worldname_values From 59a95221c822481883f7b118f6ca9c0d36084540 Mon Sep 17 00:00:00 2001 From: wozrer Date: Sat, 7 Dec 2024 17:44:34 +0300 Subject: [PATCH 11/12] Remove awk and eval --- misc/bash_completion.sh | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/misc/bash_completion.sh b/misc/bash_completion.sh index 425543b21..29951c728 100755 --- a/misc/bash_completion.sh +++ b/misc/bash_completion.sh @@ -1,7 +1,11 @@ __worldname_list() { - string=$(eval "luanti --worldlist name") - output=$(awk 'NR>1{print $0}' <<< "$string") - eval "$1=(\"${output[@]}\")" + local string output + string=$(luanti --worldlist name) + + IFS=$'\n' output=($string) + output=("${output[@]:1}") + output=("${output[@]#$'\t'}") + printf '%s\n' "${output[@]}" } _luanti() { @@ -25,9 +29,7 @@ _luanti() { # FIXME: Why luanti --gameid list returns list into stderr? COMPREPLY=($(compgen -W "list $(luanti --gameid list 2>&1)" -- "$cur")) elif [[ "$prev" == "--worldname" ]]; then - local worldname_values - __worldname_list worldname_values - COMPREPLY=($(compgen -W "$worldname_values" -- "$cur")) + COMPREPLY=($(compgen -W "$(__worldname_list)" -- "$cur")) elif [[ " ${file_opts[*]} " == *" ${prev} "* ]]; then _comp_compgen_filedir else From 001a047f36eb4fbebea8a7f5a243b5b5d69b2539 Mon Sep 17 00:00:00 2001 From: wozrer Date: Sat, 7 Dec 2024 17:57:16 +0300 Subject: [PATCH 12/12] Dont complete unknown args --- misc/bash_completion.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/misc/bash_completion.sh b/misc/bash_completion.sh index 29951c728..b82fb6021 100755 --- a/misc/bash_completion.sh +++ b/misc/bash_completion.sh @@ -9,7 +9,7 @@ __worldname_list() { } _luanti() { - local cur prev opts file_opts color_values worldlist_values + local cur prev opts file_opts unknown_val_opts color_values worldlist_values COMPREPLY=() cur="${COMP_WORDS[COMP_CWORD]}" @@ -17,6 +17,7 @@ _luanti() { opts="--address --color --config --console --debugger --gameid --go --help --info --logfile --map-dir --migrate --migrate-auth --migrate-mod-storage --migrate-players --name --password --password-file --port --quiet --random-input --recompress --run-benchmarks --run-unittests --server --terminal --test-module --trace --verbose --version --world --worldlist --worldname" file_opts="--config --logfile --map-dir --password-file --world" + unknown_val_opts="--address --migrate --migrate-auth --migrate-mod-storage --migrate-players --name --password --port --test-module" color_values="always auto never" worldlist_values="both name path" @@ -32,6 +33,8 @@ _luanti() { COMPREPLY=($(compgen -W "$(__worldname_list)" -- "$cur")) elif [[ " ${file_opts[*]} " == *" ${prev} "* ]]; then _comp_compgen_filedir + elif [[ " ${unknown_val_opts[*]} " == *" ${prev} "* ]]; then + : else COMPREPLY=($(compgen -W "$opts" -- "$cur")) fi