From 2ada4142668496fef6802e7cbc871c0836b43abb Mon Sep 17 00:00:00 2001 From: John D Pell Date: Fri, 24 Sep 2021 00:38:54 -0700 Subject: [PATCH 1/3] plugins/percol: use `_command_exists` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Addresses bash-it/bash-it#1632 And use `_log_warning`. Alsö, code style cleanup: quote things, handle unbound parameters, &c. Alsö alsö, short-circuit if not installed or inadequate _Bash_ version. --- plugins/available/percol.plugin.bash | 33 +++++++++++++++------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/plugins/available/percol.plugin.bash b/plugins/available/percol.plugin.bash index c1fc807c..cc83683e 100644 --- a/plugins/available/percol.plugin.bash +++ b/plugins/available/percol.plugin.bash @@ -1,3 +1,4 @@ +# shellcheck shell=bash cite about-plugin about-plugin 'Search&Select history with percol' @@ -12,24 +13,26 @@ about-plugin 'Search&Select history with percol' # Usage ## C-r to search&select from history -_replace_by_history() { - if command -v tac>/dev/null; then +_command_exists percol || return + +if [[ ${BASH_VERSINFO[0]} -lt 4 ]]; then + _log_warning "You have to upgrade Bash to Bash v4.x to use the 'percol' plugin." + _log_warning "Your current Bash version is $BASH_VERSION." + return +else + bind -x '"\C-r": _replace_by_history' +fi + +function _replace_by_history() { + local HISTTIMEFORMAT= # Ensure we can parse history properly + if _command_exists tac + then alias _tac=tac else alias _tac="tail -r" fi - local l=$(HISTTIMEFORMAT= history | _tac | sed -e 's/^\ *[0-9]*\ *//' | percol --query "$READLINE_LINE") - READLINE_LINE="$l" + #TODO: "${histlines[@]/*( )+([[:digit:]])*( )/}" + local l="$(history | _tac | sed -e 's/^\ *[0-9]*\ *//' | percol --query "${READLINE_LINE:-}")" + READLINE_LINE="${l}" READLINE_POINT=${#l} } - - -if command -v percol>/dev/null; then - current_version=${BASH_VERSION%%[^0-9]*} - if [ $current_version -lt 4 ]; then - echo -e "\033[91mWarning: You have to upgrade Bash to Bash v4.x to use the 'percol' plugin.\033[m" - echo -e "\033[91m Your current Bash version is $BASH_VERSION.\033[m" - else - bind -x '"\C-r": _replace_by_history' - fi -fi From 92282c479550994105c10c5b64034d22a5af23a0 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Fri, 24 Sep 2021 00:39:07 -0700 Subject: [PATCH 2/3] plugin/percol: `shellcheck` & `shfmt` According to `shellcheck`, the `_tac` alias simply doesn't work. At all. Ever. See SC2262 and SC2263. --- clean_files.txt | 1 + plugins/available/percol.plugin.bash | 21 ++++++++------------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/clean_files.txt b/clean_files.txt index 2242ef5e..cf8d082a 100644 --- a/clean_files.txt +++ b/clean_files.txt @@ -92,6 +92,7 @@ plugins/available/hub.plugin.bash plugins/available/jump.plugin.bash plugins/available/node.plugin.bash plugins/available/nodenv.plugin.bash +plugins/available/percol.plugin.bash plugins/available/plenv.plugin.bash plugins/available/pyenv.plugin.bash plugins/available/rbenv.plugin.bash diff --git a/plugins/available/percol.plugin.bash b/plugins/available/percol.plugin.bash index cc83683e..97986ccf 100644 --- a/plugins/available/percol.plugin.bash +++ b/plugins/available/percol.plugin.bash @@ -16,23 +16,18 @@ about-plugin 'Search&Select history with percol' _command_exists percol || return if [[ ${BASH_VERSINFO[0]} -lt 4 ]]; then - _log_warning "You have to upgrade Bash to Bash v4.x to use the 'percol' plugin." - _log_warning "Your current Bash version is $BASH_VERSION." - return + _log_warning "You have to upgrade Bash to Bash v4.x to use the 'percol' plugin." + _log_warning "Your current Bash version is $BASH_VERSION." + return else - bind -x '"\C-r": _replace_by_history' + bind -x '"\C-r": _replace_by_history' fi function _replace_by_history() { local HISTTIMEFORMAT= # Ensure we can parse history properly - if _command_exists tac - then - alias _tac=tac - else - alias _tac="tail -r" - fi #TODO: "${histlines[@]/*( )+([[:digit:]])*( )/}" - local l="$(history | _tac | sed -e 's/^\ *[0-9]*\ *//' | percol --query "${READLINE_LINE:-}")" - READLINE_LINE="${l}" - READLINE_POINT=${#l} + local l + l="$(history | tail -r | sed -e 's/^\ *[0-9]*\ *//' | percol --query "${READLINE_LINE:-}")" + READLINE_LINE="${l}" + READLINE_POINT=${#l} } From a4e4f30ff14209ea69ddee1e6a028627573d839a Mon Sep 17 00:00:00 2001 From: John D Pell Date: Sun, 26 Sep 2021 11:54:32 -0700 Subject: [PATCH 3/3] plugins/percol: `bind` Move `bind` below function definition --- plugins/available/percol.plugin.bash | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/plugins/available/percol.plugin.bash b/plugins/available/percol.plugin.bash index 97986ccf..027dfdc4 100644 --- a/plugins/available/percol.plugin.bash +++ b/plugins/available/percol.plugin.bash @@ -19,8 +19,6 @@ if [[ ${BASH_VERSINFO[0]} -lt 4 ]]; then _log_warning "You have to upgrade Bash to Bash v4.x to use the 'percol' plugin." _log_warning "Your current Bash version is $BASH_VERSION." return -else - bind -x '"\C-r": _replace_by_history' fi function _replace_by_history() { @@ -31,3 +29,4 @@ function _replace_by_history() { READLINE_LINE="${l}" READLINE_POINT=${#l} } +bind -x '"\C-r": _replace_by_history'