Skip to content

Commit

Permalink
Widget: fix replacements in case of multiple occurrences
Browse files Browse the repository at this point in the history
  • Loading branch information
denisidoro authored Aug 9, 2021
1 parent 3abd3da commit 9396fd5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
6 changes: 6 additions & 0 deletions docs/navi.cheat
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ navi --tldr "<query>"
navi --cheatsh "<query>"


% widget

# Load shell widget
shell="$(basename $SHELL)"; eval "$(navi widget $shell)"


% help

# Read command-line help text
Expand Down
5 changes: 3 additions & 2 deletions shell/navi.plugin.bash
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ _navi_widget() {
if [ -z "${last_command}" ]; then
local -r output="$(_navi_call --print)"
else
local -r find="$last_command"
local -r find="${last_command}_NAVIEND"
local -r replacement="$(_navi_call --print --query "$last_command")"
local output="$input"
if [ -n "$replacement" ]; then
output="${input//$find/$replacement}"
output="${input}_NAVIEND"
output="${output//$find/$replacement}"
fi
fi

Expand Down
5 changes: 3 additions & 2 deletions shell/navi.plugin.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ _navi_call() {
_navi_widget() {
local -r input="${LBUFFER}"
local -r last_command="$(echo "${input}" | navi fn widget::last_command)"
local find="$last_command"
local replacement="$last_command"

if [ -z "$last_command" ]; then
Expand All @@ -20,7 +19,9 @@ _navi_widget() {
fi

if [ -n "$replacement" ]; then
previous_output="${input//$find/$replacement}"
local -r find="${last_command}_NAVIEND"
previous_output="${input}_NAVIEND"
previous_output="${previous_output//$find/$replacement}"
else
previous_output="$input"
fi
Expand Down

0 comments on commit 9396fd5

Please sign in to comment.