style(sudo): clean code style and reorganise logic

This commit is contained in:
Marc Cornellà 2022-01-18 19:03:27 +01:00
parent 540b2200af
commit 957dca698c
No known key found for this signature in database
GPG Key ID: 0314585E776A9C1B

View File

@ -17,9 +17,13 @@
__sudo-replace-buffer() { __sudo-replace-buffer() {
local old=$1 new=$2 space=${2:+ } local old=$1 new=$2 space=${2:+ }
if [[ ${#LBUFFER} -le ${#old} ]]; then
RBUFFER="${space}${BUFFER#$old }" # if the cursor is positioned in the $old part of the text, make
LBUFFER="${new}" # the substitution and leave the cursor after the $new text
if [[ $CURSOR -le ${#old} ]]; then
BUFFER="${new}${space}${BUFFER#$old }"
CURSOR=${#new}
# otherwise just replace $old with $new in the text before the cursor
else else
LBUFFER="${new}${space}${LBUFFER#$old }" LBUFFER="${new}${space}${LBUFFER#$old }"
fi fi
@ -36,18 +40,21 @@ sudo-command-line() {
LBUFFER="${LBUFFER:1}" LBUFFER="${LBUFFER:1}"
fi fi
# If $SUDO_EDITOR or $VISUAL are defined, then use that as $EDITOR {
# Else use the default $EDITOR # If $SUDO_EDITOR or $VISUAL are defined, then use that as $EDITOR
local EDITOR=${SUDO_EDITOR:-${VISUAL:-$EDITOR}} # Else use the default $EDITOR
local EDITOR=${SUDO_EDITOR:-${VISUAL:-$EDITOR}}
# If $EDITOR is not set, just toggle the sudo prefix on and off
if [[ -z "$EDITOR" ]]; then
case "$BUFFER" in
sudo\ -e\ *) __sudo-replace-buffer "sudo -e" "" ;;
sudo\ *) __sudo-replace-buffer "sudo" "" ;;
*) LBUFFER="sudo $LBUFFER" ;;
esac
return
fi
# If $EDITOR is not set, just toggle the sudo prefix on and off
if [[ -z "$EDITOR" ]]; then
case "$BUFFER" in
sudo\ -e\ *) __sudo-replace-buffer "sudo -e" "" ;;
sudo\ *) __sudo-replace-buffer "sudo" "" ;;
*) LBUFFER="sudo $LBUFFER" ;;
esac
else
# Check if the typed command is really an alias to $EDITOR # Check if the typed command is really an alias to $EDITOR
# Get the first part of the typed command # Get the first part of the typed command
@ -72,7 +79,8 @@ sudo-command-line() {
if [[ "$realcmd" = (\$EDITOR|$editorcmd|${editorcmd:c}) \ if [[ "$realcmd" = (\$EDITOR|$editorcmd|${editorcmd:c}) \
|| "${realcmd:c}" = ($editorcmd|${editorcmd:c}) ]] \ || "${realcmd:c}" = ($editorcmd|${editorcmd:c}) ]] \
|| builtin which -a "$realcmd" | command grep -Fx -q "$editorcmd"; then || builtin which -a "$realcmd" | command grep -Fx -q "$editorcmd"; then
editorcmd="$cmd" # replace $editorcmd with the typed command so it matches below __sudo-replace-buffer "$cmd" "sudo -e"
return
fi fi
# Check for editor commands in the typed command and replace accordingly # Check for editor commands in the typed command and replace accordingly
@ -83,13 +91,13 @@ sudo-command-line() {
sudo\ *) __sudo-replace-buffer "sudo" "" ;; sudo\ *) __sudo-replace-buffer "sudo" "" ;;
*) LBUFFER="sudo $LBUFFER" ;; *) LBUFFER="sudo $LBUFFER" ;;
esac esac
fi } always {
# Preserve beginning space
LBUFFER="${WHITESPACE}${LBUFFER}"
# Preserve beginning space # Redisplay edit buffer (compatibility with zsh-syntax-highlighting)
LBUFFER="${WHITESPACE}${LBUFFER}" zle redisplay
}
# Redisplay edit buffer (compatibility with zsh-syntax-highlighting)
zle redisplay
} }
zle -N sudo-command-line zle -N sudo-command-line