zsh:keys:cmd-on-enter: Refactor for real cmd cycle

Refactor the function to keep an array of commands with requirements
that have to be met. Those commands are then cycled over a wrap around
index instead of the inflexible switch statement.
This commit is contained in:
2021-12-29 18:58:45 +01:00
parent b6d2299444
commit fe8a656dfe

View File

@@ -98,21 +98,26 @@ function rationalize_dots {
zle -N rationalize_dots zle -N rationalize_dots
bindkey . rationalize_dots bindkey . rationalize_dots
CMDS_ON_ENTER=(ll gs)
REQUIREMENTS_CMDS_ON_ENTER=(true "git rev-parse")
function cmd-on-enter { function cmd-on-enter {
if [[ -z $BUFFER ]]; then if [[ -z $BUFFER ]]; then
# Overwrite BUFFER and default to ll # Overwrite BUFFER and default to ll
BUFFER="${CMD_ON_ENTER:=ll}" BUFFER="${CMDS_ON_ENTER[${cmd_on_enter_idx:=1}]}"
# Cycle through ll and git status # Cycle through ll and git status
case "$CMD_ON_ENTER" in local idx=$cmd_on_enter_idx
ll) idx=$((idx < $#CMDS_ON_ENTER ? idx + 1 : 1))
! git rev-parse &>/dev/null || CMD_ON_ENTER='gs';; until
gs) $REQUIREMENTS_CMDS_ON_ENTER[$idx] &>/dev/null \
CMD_ON_ENTER='ll';; || [[ $idx = $cmd_on_enter_idx ]]
esac do
idx=$((idx < $#CMDS_ON_ENTER ? idx + 1 : 1))
done
cmd_on_enter_idx=$idx
else else
# Reset if other command is executed # Reset if other command is executed
CMD_ON_ENTER=ll cmd_on_enter_idx=1
fi fi
zle accept-line zle accept-line
} }