From 948030b9cbd160ff02897e4887c4c95ef45a79a1 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Tue, 21 Nov 2023 13:26:40 -0700 Subject: [PATCH] ref: move shell integrations to their own functions --- webi/webi.sh | 201 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 131 insertions(+), 70 deletions(-) diff --git a/webi/webi.sh b/webi/webi.sh index 099f050..cd83b02 100755 --- a/webi/webi.sh +++ b/webi/webi.sh @@ -239,88 +239,31 @@ __webi_main() { } webi_shell_init() { ( + a_shell="${2:-}" + + fn_shell_integrate_bash "" + fn_shell_integrate_zsh "" + fn_shell_integrate_fish "" + # update completions now webi_list > /dev/null if [ $# -eq 1 ]; then - if [ ! -f ~/.bashrc ] || ! grep -q 'webi --init' ~/.bashrc; then - # shellcheck disable=SC2016 - { - echo '' - echo '# added by Webi for bash' - echo 'eval "$(webi --init bash)"' - } >> ~/.bashrc - fi - if command -v zsh > /dev/null; then - touch ~/.zshrc - if ! grep -q 'webi --init' ~/.zshrc; then - # shellcheck disable=SC2016 - { - echo '' - echo '# added by Webi for zsh' - echo 'eval "$(webi --init zsh)"' - } >> ~/.zshrc - fi - fi - if command -v fish > /dev/null; then - mkdir -p ~/.config/fish - touch ~/.config/fish/config.fish - if ! grep -q 'webi --init' ~/.config/fish/config.fish; then - # shellcheck disable=SC2016 - { - echo '' - echo '# added by Webi for fish' - echo 'webi --init fish | source' - } >> ~/.config/fish/config.fish - fi - fi exit 0 fi - case "$2" in + case "${a_shell}" in bash) - # shellcheck disable=SC2016 - { - echo '_webi() {' - echo ' COMPREPLY=()' - echo ' local cur="${COMP_WORDS[COMP_CWORD]}"' - echo ' if [ "$COMP_CWORD" -eq 1 ]; then' - echo ' local completions=$(webi --list | cut -d" " -f1)' - echo ' COMPREPLY=( $(compgen -W "$completions" -- "$cur") )' - echo ' fi' - echo '}' - echo '' - echo 'complete -F _webi webi' - } + fn_shell_integrate_bash "force" + fn_shell_init_bash ;; zsh) - # shellcheck disable=SC2016 - { - echo '_webi() {' - echo ' local -a list completions' - echo ' list=$(webi --list | cut -d" " -f1)' - echo ' completions=(${(f)list})' - echo ' _describe -t commands "command" completions && ret=0' - echo '}' - echo '' - echo 'autoload -Uz compinit && compinit' - echo 'compdef _webi webi' - } + fn_shell_integrate_zsh "force" + fn_shell_init_zsh ;; fish) - # shellcheck disable=SC2016 - { - echo 'function __fish_webi_needs_command' - echo ' set cmd (commandline -opc)' - echo ' if [ (count $cmd) -eq 1 -a $cmd[1] = "webi" ]' - echo ' return 0' - echo ' end' - echo ' return 1' - echo 'end' - echo '' - echo 'set completions (webi --list | cut -d" " -f1)' - echo 'complete -f -c webi -n __fish_webi_needs_command -a "$completions"' - } + fn_shell_integrate_fish "force" + fn_shell_init_fish ;; *) echo >&2 "Unsupported shell: $2" @@ -329,6 +272,124 @@ webi_shell_init() { ( esac ) } +fn_shell_integrate_bash() { ( + a_force="${1}" + if test -z "${a_force}"; then + if ! command -v bash > /dev/null; then + return 0 + fi + + if ! test -e ~/.bashrc && ! test -e ~/.bash_history; then + return 0 + fi + fi + + touch -a ~/.bashrc + if grep -q 'webi --init' ~/.bashrc; then + return 0 + fi + + echo >&2 " Edit ~/.bashrc to add 'eval \"\$(webi --init bash)\"'" + # shellcheck disable=SC2016 + { + echo '' + echo '# Generated by Webi. Do not edit.' + echo 'eval "$(webi --init bash)"' + } >> ~/.bashrc +); } + +# shellcheck disable=SC2016 +fn_shell_init_bash() { ( + echo '_webi() {' + echo ' COMPREPLY=()' + echo ' local cur="${COMP_WORDS[COMP_CWORD]}"' + echo ' if [ "$COMP_CWORD" -eq 1 ]; then' + echo ' local completions=$(webi --list | cut -d" " -f1)' + echo ' COMPREPLY=( $(compgen -W "$completions" -- "$cur") )' + echo ' fi' + echo '}' + echo '' + echo 'complete -F _webi webi' +); } + +fn_shell_integrate_zsh() { ( + a_force="${1}" + if test -z "${a_force}"; then + if ! command -v zsh > /dev/null; then + return 0 + fi + + if ! test -e ~/.zshrc && + ! test -e ~/.zsh_sessions && + ! test -e ~/.zsh_history; then + return 0 + fi + fi + + touch -a ~/.zshrc + if grep -q 'webi --init' ~/.zshrc; then + return 0 + fi + + echo >&2 " Edit ~/.zshrc to add 'eval \"\$(webi --init zsh)\"'" + # shellcheck disable=SC2016 + { + echo '' + echo '# Generated by Webi. Do not edit.' + echo 'eval "$(webi --init zsh)"' + } >> ~/.zshrc +); } + +# shellcheck disable=SC2016 +fn_shell_init_zsh() { ( + echo '_webi() {' + echo ' local -a list completions' + echo ' list=$(webi --list | cut -d" " -f1)' + echo ' completions=(${(f)list})' + echo ' _describe -t commands "command" completions && ret=0' + echo '}' + echo '' + echo 'autoload -Uz compinit && compinit' + echo 'compdef _webi webi' +); } + +fn_shell_integrate_fish() { ( + a_force="${1}" + if test -z "${a_force}"; then + if ! command -v fish > /dev/null; then + return 0 + fi + fi + + mkdir -p ~/.config/fish + touch -a ~/.config/fish/config.fish + if grep -q 'webi --init' ~/.config/fish/config.fish; then + return 0 + fi + + echo >&2 " Edit ~/.config/fish/config.fish to add 'webi --init fish | source'" + # shellcheck disable=SC2016 + { + echo '' + echo '# Generated by Webi. Do not edit.' + echo 'webi --init fish | source' + } >> ~/.config/fish/config.fish +); } + +# shellcheck disable=SC2016 +fn_shell_init_fish() { ( + echo 'function __fish_webi_needs_command' + echo ' set cmd (commandline -opc)' + echo ' if [ (count $cmd) -eq 1 -a $cmd[1] = "webi" ]' + echo ' return 0' + echo ' end' + echo ' return 1' + echo 'end' + echo '' + echo 'set completions (webi --list | cut -d" " -f1)' + echo 'complete -f -c webi -n __fish_webi_needs_command -a "$completions"' +); } + webi_list() { ( # make sure there's always a cache dir and timestamp file mkdir -p ~/.local/share/webi/var/