Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch to color more correctly the command behind "sudo" #610

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions highlighters/main/main-highlighter.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ _zsh_highlight_main_calculate_fallback() {
done
}

_zsh_highlight_main__test_type_builtin() {

if [[ -n ${ZSH_HIGHLIGHT_SPECIAL_PATH} ]]; then
if [ "$this_word" = ":sudo_opt::start:" ]; then
# ZSH_HIGHLIGHT_SPECIAL_PATH exist is not empty : "Admin" says that user has access to commands in these paths with sudo
local PATH="${ZSH_HIGHLIGHT_SPECIAL_PATH}"
fi
fi

builtin type -w -- $1
}

# Get the type of a command.
#
# Uses the zsh/parameter module if available to avoid forks, and a
Expand All @@ -152,6 +164,24 @@ _zsh_highlight_main__type() {
fi
fi

if [[ -v ZSH_HIGHLIGHT_SPECIAL_COMMAND ]]; then
if (( ${ZSH_HIGHLIGHT_SPECIAL_COMMAND[(I)$1]} )); then
# ZSH_HIGHLIGHT_SPECIAL_COMMAND exists : if foo of "sudo foo" is in ZSH_HIGHLIGHT_SPECIAL_COMMAND then "Admin" says that user has access to this command
REPLY=command
return 0
fi
fi

if [[ -v ZSH_HIGHLIGHT_SPECIAL_PATH ]]; then
if [ "$this_word" = ":sudo_opt::start:" ]; then
if [ -z "${ZSH_HIGHLIGHT_SPECIAL_PATH}" ]; then
# ZSH_HIGHLIGHT_SPECIAL_PATH exist and is empty : "Admin" wants to say that user has no access to sudo : foo in "sudo foo" is red
REPLY=none
return 0
fi
fi
fi

# Main logic
if (( $#options_to_set )); then
setopt localoptions $options_to_set;
Expand Down Expand Up @@ -186,7 +216,7 @@ _zsh_highlight_main__type() {
elif { [[ $1 != */* ]] || is-at-least 5.3 } &&
# Add a subshell to avoid a zsh upstream bug; see issue #606.
# ### Remove the subshell when we stop supporting zsh 5.7.1 (I assume 5.8 will have the bugfix).
! (builtin type -w -- $1) >/dev/null 2>&1; then
! (_zsh_highlight_main__test_type_builtin $1) >/dev/null 2>&1; then
REPLY=none
fi
fi
Expand All @@ -201,7 +231,7 @@ _zsh_highlight_main__type() {
# starts with an arithmetic expression [«((…))» as the first thing inside
# «$(…)»], which is area that has had some parsing bugs before 5.6
# (approximately).
REPLY="${$(:; (( aliases_allowed )) || unalias -- $1 2>/dev/null; LC_ALL=C builtin type -w -- $1 2>/dev/null)##*: }"
REPLY="${$(:; (( aliases_allowed )) || unalias -- $1 2>/dev/null; LC_ALL=C _zsh_highlight_main__test_type_builtin $1 2>/dev/null)##*: }"
if [[ $REPLY == 'alias' ]]; then
may_cache=0
fi
Expand Down