From b169623b642d44109bb073b7b7fe2f1e88a5da69 Mon Sep 17 00:00:00 2001 From: Simon Schricker Date: Sat, 13 Mar 2021 19:04:33 +0100 Subject: [PATCH] Add: Bash completion for foot and footclient * use toe for terminfo, thanks Craig. * adds optional dependency on bash-completion for positional arguments Co-authored-by: Craig Barnes --- CHANGELOG.md | 3 ++ INSTALL.md | 1 + completions/bash/foot | 83 +++++++++++++++++++++++++++++++++++++ completions/bash/footclient | 74 +++++++++++++++++++++++++++++++++ completions/meson.build | 3 ++ 5 files changed, 164 insertions(+) create mode 100644 completions/bash/foot create mode 100644 completions/bash/footclient diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ead563d..cc5fbe3f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,6 +57,8 @@ (https://codeberg.org/dnkl/foot/issues/391). * `-N,--no-wait` to `footclient` (https://codeberg.org/dnkl/foot/issues/395). +* Completions for Bash shell + (https://codeberg.org/dnkl/foot/issues/10). ### Changed @@ -121,6 +123,7 @@ * [craigbarnes](https://codeberg.org/craigbarnes) * toast * [l3mon4d3](https://codeberg.org/l3mon4d3) +* [Simon Schricker](mailto:s.schricker@sillage.at) ## 1.6.4 diff --git a/INSTALL.md b/INSTALL.md index 504c8854..1cb0d9a7 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -53,6 +53,7 @@ following **optional** dependencies: * libnotify: desktop notifications by default uses `notify-send`. * xdg-utils: URLs are by default launched with `xdg-open`. +* bash-completion: If you want completion for positional arguments. ### Building diff --git a/completions/bash/foot b/completions/bash/foot new file mode 100644 index 00000000..6fbe635f --- /dev/null +++ b/completions/bash/foot @@ -0,0 +1,83 @@ +# Bash completion script for foot +_foot() +{ + COMPREPLY=() + + local cur prev flags word commands match previous_words i offset + flags=( + "--app-id" + "--check-config" + "--config" + "--font" + "--fullscreen" + "--help" + "--hold" + "--log-colorize" + "--log-level" + "--log-no-syslog" + "--login-shell" + "--maximized" + "--print-pid" + "--server" + "--term" + "--title" + "--version" + "--window-size-pixels" + "--window-size-chars" + "--working-directory" + ) + flags="${flags[@]}" + cur=${COMP_WORDS[COMP_CWORD]} + prev=${COMP_WORDS[COMP_CWORD-1]} + + # check if positional argument is completed + previous_words=( "${COMP_WORDS[@]}" ) + unset previous_words[-1] + commands=$(compgen -c | grep -vFx "$(compgen -k)" | grep -vE '^([.:[]|foot)$' | sort -u) + i=0 + for word in "${previous_words[@]}" ; do + match=$(printf "$commands" | grep -Fx "$word" 2>/dev/null) + if [[ ! -z "$match" ]] ; then + if [[ ${COMP_WORDS[i-1]} =~ ^(--app-id|--config|--font|--log-level|--term|--title|--window-size-pixels|--window-size-chars|--working-directory)$ ]] ; then + (( i++ )) + continue + fi + # positional argument found + offset=$i + fi + (( i++ )) + done + + if [[ ! -z "$offset" ]] ; then + # depends on bash_completion being available + declare -F _command_offset >/dev/null || return 1 + _command_offset $offset + elif [[ ${cur} == --* ]] ; then + COMPREPLY=( $(compgen -W "${flags}" -- ${cur}) ) + elif [[ ${prev} =~ ^(--config|--print-pid|--server)$ ]] ; then + compopt -o default + elif [[ ${prev} == '--working-directory' ]] ; then + compopt -o dirnames + elif [[ ${prev} == '--term' ]] ; then + # check if toe is available + which toe > /dev/null || return 1 + COMPREPLY=( $(compgen -W "$(toe -a | awk '$1 ~ /[+]/ {next}; {print $1}')" -- ${cur}) ) + elif [[ ${prev} == '--font' ]] ; then + # check if fc-list is available + which fc-list > /dev/null || return 1 + COMPREPLY=( $(compgen -W "$(fc-list : family | sed 's/,/\n/g' | uniq | tr -d ' ')" -- ${cur}) ) + elif [[ ${prev} == '--log-level' ]] ; then + COMPREPLY=( $(compgen -W "error warning info" -- ${cur}) ) + elif [[ ${prev} == '--log-colorize' ]] ; then + COMPREPLY=( $(compgen -W "never always auto" -- ${cur}) ) + elif [[ ${prev} =~ ^(--app-id|--help|--title|--version|--window-size-chars|--window-size-pixels|--check-config)$ ]] ; then + : # don't autocomplete for these flags + else + # complete commands from $PATH + COMPREPLY=( $(compgen -c ${cur}) ) + fi + + return 0 +} + +complete -F _foot foot diff --git a/completions/bash/footclient b/completions/bash/footclient new file mode 100644 index 00000000..6e065a97 --- /dev/null +++ b/completions/bash/footclient @@ -0,0 +1,74 @@ +# Bash completion script for footclient +_footclient() +{ + COMPREPLY=() + + local cur prev flags word commands match previous_words i offset + flags=( + "--app-id" + "--fullscreen" + "--help" + "--hold" + "--login-shell" + "--log-level" + "--log-colorize" + "--maximized" + "--server-socket" + "--term" + "--title" + "--version" + "--window-size-pixels" + "--window-size-chars" + "--working-directory" + ) + flags="${flags[@]}" + cur=${COMP_WORDS[COMP_CWORD]} + prev=${COMP_WORDS[COMP_CWORD-1]} + + # check if positional argument is completed + previous_words=( "${COMP_WORDS[@]}" ) + unset previous_words[-1] + commands=$(compgen -c | grep -vFx "$(compgen -k)" | grep -vE '^([.:[]|footclient)$' | sort -u) + i=0 + for word in "${previous_words[@]}" ; do + match=$(printf "$commands" | grep -Fx "$word" 2>/dev/null) + if [[ ! -z "$match" ]] ; then + if [[ ${COMP_WORDS[i-1]} =~ ^(--app-id|--log-level|--server-socket|--term|--title|--window-size-pixels|--window-size-chars|--working-directory)$ ]] ; then + (( i++ )) + continue + fi + # positional argument found + offset=$i + fi + (( i++ )) + done + + if [[ ! -z "$offset" ]] ; then + # depends on bash_completion being available + declare -F _command_offset >/dev/null || return 1 + _command_offset $offset + elif [[ ${cur} == --* ]] ; then + COMPREPLY=( $(compgen -W "${flags}" -- ${cur}) ) + elif [[ ${prev} == '--server-socket' ]] ; then + compopt -o default + elif [[ ${prev} == '--working-directory' ]] ; then + compopt -o dirnames + elif [[ ${prev} == '--term' ]] ; then + # check if toe is available + which toe > /dev/null || return 1 + COMPREPLY=( $(compgen -W "$(toe -a | awk '$1 ~ /[+]/ {next}; {print $1}')" -- ${cur}) ) + elif [[ ${prev} == '--log-level' ]] ; then + COMPREPLY=( $(compgen -W "error warning info" -- ${cur}) ) + elif [[ ${prev} == '--log-colorize' ]] ; then + COMPREPLY=( $(compgen -W "never always auto" -- ${cur}) ) + elif [[ ${prev} =~ ^(--app-id|--help|--title|--version|--window-size-chars|--window-size-pixels|)$ ]] ; then + : # don't autocomplete for these flags + else + # complete commands from $PATH + COMPREPLY=( $(compgen -c ${cur}) ) + fi + + return 0 +} + +complete -F _footclient footclient diff --git a/completions/meson.build b/completions/meson.build index 82198d0b..b0da9455 100644 --- a/completions/meson.build +++ b/completions/meson.build @@ -1,6 +1,9 @@ zsh_install_dir = join_paths(get_option('datadir'), 'zsh', 'site-functions') fish_install_dir = join_paths(get_option('datadir'), 'fish', 'vendor_completions.d') +bash_install_dir = join_paths(get_option('datadir'), 'bash-completion', 'completions') install_data('zsh/_foot', install_dir: zsh_install_dir) install_data('zsh/_footclient', install_dir: zsh_install_dir) install_data('fish/foot.fish', install_dir: fish_install_dir) install_data('fish/footclient.fish', install_dir: fish_install_dir) +install_data('bash/foot', install_dir: bash_install_dir) +install_data('bash/footclient', install_dir: bash_install_dir)