Skip to content

Commit

Permalink
plugins/percol: use _command_exists
Browse files Browse the repository at this point in the history
Addresses #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.
  • Loading branch information
gaelicWizard committed Sep 24, 2021
1 parent d6c4c0c commit 2ada414
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions plugins/available/percol.plugin.bash
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# shellcheck shell=bash
cite about-plugin
about-plugin 'Search&Select history with percol'

Expand All @@ -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

0 comments on commit 2ada414

Please sign in to comment.