Skip to content

Commit

Permalink
Close widget on single ESC (#597)
Browse files Browse the repository at this point in the history
Fixes #546 and #553
  • Loading branch information
denisidoro authored Aug 7, 2021
1 parent 425ce80 commit a36c26f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 45 deletions.
2 changes: 2 additions & 0 deletions scripts/fix
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@ cargo clippy || true
_commit
log::note "dot code beautify..."
find scripts -type f | xargs -I% dot code beautify % || true
dot code beautify "${NAVI_HOME}/shell/navi.plugin.bash" || true
dot code beautify "${NAVI_HOME}/shell/navi.plugin.zsh" || true
dot code beautify "${NAVI_HOME}/tests/core.bash" || true
dot code beautify "${NAVI_HOME}/tests/run" || true
40 changes: 20 additions & 20 deletions shell/navi.plugin.bash
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
#!/usr/bin/env bash

_navi_call() {
local result="$(navi "$@" </dev/tty)"
if [ -z "${result}" ]; then
result="$(navi --print </dev/tty)"
fi
printf "%s" "$result"
local result="$(navi "$@" </dev/tty)"
printf "%s" "$result"
}

_navi_widget() {
local -r input="${READLINE_LINE}"
local -r last_command="$(echo "${input}" | navi fn widget::last_command)"
local -r input="${READLINE_LINE}"
local -r last_command="$(echo "${input}" | navi fn widget::last_command)"

if [ -z "${last_command}" ]; then
local -r output="$(FZF_OVERRIDES="${FZF_OVERRIDES:-} --no-select-1" _navi_call --print)"
else
local -r find="$last_command"
local -r replacement="$(_navi_call --print --query "${last_command}")"
local -r output="${input//$find/$replacement}"
fi
if [ -z "${last_command}" ]; then
local -r output="$(_navi_call --print)"
else
local -r find="$last_command"
local -r replacement="$(_navi_call --print --query "$last_command")"
local output="$input"
if [ -n "$replacement" ]; then
output="${input//$find/$replacement}"
fi
fi

READLINE_LINE="$output"
READLINE_POINT=${#READLINE_LINE}
READLINE_LINE="$output"
READLINE_POINT=${#READLINE_LINE}
}

_navi_widget_legacy() {
_navi_call --print
_navi_call --print
}

if [ ${BASH_VERSION:0:1} -lt 4 ]; then
bind '"\C-g": " \C-b\C-k \C-u`_navi_widget_legacy`\e\C-e\C-a\C-y\C-h\C-e\e \C-y\ey\C-x\C-x\C-f"'
if [ ${BASH_VERSION:0:1} -lt 4 ]; then
bind '"\C-g": " \C-b\C-k \C-u`_navi_widget_legacy`\e\C-e\C-a\C-y\C-h\C-e\e \C-y\ey\C-x\C-x\C-f"'
else
bind -x '"\C-g": _navi_widget'
bind -x '"\C-g": _navi_widget'
fi
45 changes: 22 additions & 23 deletions shell/navi.plugin.zsh
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
#!/usr/bin/env zsh

_navi_call() {
local result="$(navi "$@" </dev/tty)"
if [ -z "${result}" ]; then
result="$(navi --print </dev/tty)"
fi
printf "%s" "$result"
local result="$(navi "$@" </dev/tty)"
printf "%s" "$result"
}

_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"
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
replacement="$(FZF_OVERRIDES="${FZF_OVERRIDES:-} --no-select-1" _navi_call --print)"
elif [ "${LASTWIDGET}" = "_navi_widget" ] && [ "$input" = "$previous_output" ]; then
find="$input"
replacement="$(_navi_call --print --query "${previous_last_command:-$last_command}")"
else
replacement="$(_navi_call --print --best-match --query "${last_command}")"
fi
if [ -z "$last_command" ]; then
replacement="$(_navi_call --print)"
elif [ "$LASTWIDGET" = "_navi_widget" ] && [ "$input" = "$previous_output" ]; then
replacement="$(_navi_call --print --query "$last_command")"
else
replacement="$(_navi_call --print --best-match --query "$last_command")"
fi

previous_last_command="$last_command"
previous_output="${input//$find/$replacement}"
if [ -n "$replacement" ]; then
previous_output="${input//$find/$replacement}"
else
previous_output="$input"
fi

zle kill-whole-line
LBUFFER="${previous_output}"
region_highlight=("P0 100 bold")
zle redisplay
zle kill-whole-line
LBUFFER="${previous_output}"
region_highlight=("P0 100 bold")
zle redisplay
}

zle -N _navi_widget
Expand Down
4 changes: 2 additions & 2 deletions tests/core.bash
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ test::run() {
}

test::_escape() {
tr '\n' "$NEWLINE_CHAR" | sed -E "s/[\s$(printf "$NEWLINE_CHAR") ]+$//g"
tr '\n' "$NEWLINE_CHAR" | sed -E "s/[\s$(printf "$NEWLINE_CHAR") ]+$//g"
}

test::equals() {
local -r actual="$(cat)"
local -r expected="$(echo "${1:-}")"
local -r actual2="$(echo "$actual" | test::_escape)"
local -r expected2="$(echo "$expected" | test::_escape)"

Expand Down

0 comments on commit a36c26f

Please sign in to comment.