Skip to content

Commit

Permalink
Breakout sudo functionality into theme parts
Browse files Browse the repository at this point in the history
Add _lp_sudo_active(), just the sudo check as a function.
Add _lp_sudo_active_color(), return the correct color string if sudo is
active.
Breakout user detection to _lp_user().
  • Loading branch information
Rycieos committed Nov 25, 2020
1 parent 0200b99 commit 9ba5d28
Showing 1 changed file with 68 additions and 39 deletions.
107 changes: 68 additions & 39 deletions liquidprompt
Original file line number Diff line number Diff line change
Expand Up @@ -469,53 +469,24 @@ lp_activate() {
###############
# Who are we? #
###############
command -v _lp_sudo_check >/dev/null && unset -f _lp_sudo_check

# Yellow for root, bold if the user is not the login one, else no color.
if (( EUID != 0 )); then # if user is not root
# if user is not login user
if [[ "${USER}" != "$(logname 2>/dev/null || echo "$LOGNAME")" ]]; then
LP_USER="${LP_COLOR_USER_ALT}${_LP_USER_SYMBOL}${NO_COL}"
elif (( LP_USER_ALWAYS )); then
LP_USER="${LP_COLOR_USER_LOGGED}${_LP_USER_SYMBOL}${NO_COL}"
else
LP_USER=""
fi
_lp_user
local -i user=$?

if (( user < 2 )); then # if user is not root
# "sudo -n" is only supported from sudo 1.7.0
if (( LP_ENABLE_SUDO )) \
&& command -v sudo >/dev/null \
&& LC_MESSAGES=C sudo -V | GREP_OPTIONS= \grep -qE '^Sudo version (1(\.([789]\.|[1-9][0-9])|[0-9])|[2-9])'
then
LP_COLOR_MARK_NO_SUDO="$LP_COLOR_MARK"
# Test the code with the commands:
# sudo id # sudo, enter your credentials
# sudo -K # revoke your credentials
_lp_sudo_check()
{
if sudo -n true 2>/dev/null; then
LP_COLOR_MARK=$LP_COLOR_MARK_SUDO
else
LP_COLOR_MARK=$LP_COLOR_MARK_NO_SUDO
fi
}
if (( LP_ENABLE_SUDO )); then
command -v sudo >/dev/null \
&& LC_MESSAGES=C sudo -V | GREP_OPTIONS= \grep -qE '^Sudo version (1(\.([789]\.|[1-9][0-9])|[0-9])|[2-9])' \
|| LP_ENABLE_SUDO=0
fi
else # root!
LP_USER="${LP_COLOR_USER_ROOT}${_LP_USER_SYMBOL}${NO_COL}"
LP_COLOR_MARK="${LP_COLOR_MARK_ROOT}"
LP_COLOR_PATH="${LP_COLOR_PATH_ROOT}"
# Disable VCS info for all paths
LP_ENABLE_SUDO=0
if (( ! LP_ENABLE_VCS_ROOT )); then
LP_DISABLED_VCS_PATHS=("/")
LP_MARK_DISABLED="$LP_MARK_DEFAULT"
fi
fi

# Empty _lp_sudo_check if root or sudo disabled
if ! command -v _lp_sudo_check >/dev/null; then
_lp_sudo_check() { :; }
fi


#################
# Where are we? #
#################
Expand Down Expand Up @@ -789,6 +760,37 @@ _lp_multiplexer() {
return 1
}

# return: 0 if login user, 1 if not, 2 if root
_lp_user() {
if (( EUID == 0 )); then
# if user is root
return 2
elif [[ "${USER}" != "$(logname 2>/dev/null || printf '%s' "${LOGNAME-}")" ]]; then
# if user is not login user
return 1
else
return 0
fi
}

# Test the code with the commands:
# sudo id # sudo, enter your credentials
# sudo -K # revoke your credentials
_lp_sudo_active() {
(( LP_ENABLE_SUDO )) || return 2
\sudo -n true 2>/dev/null || return 1
}

_lp_sudo_active_color() {
(( LP_ENABLE_SUDO )) || return 2

if _lp_sudo_active; then
lp_sudo_active_color=$LP_COLOR_MARK_SUDO
else
lp_sudo_active_color=$LP_COLOR_MARK_NO_SUDO
fi
}

# Put the hostname if not locally connected
# color it in cyan within SSH, and a warning red if within telnet
# else display the host without color
Expand Down Expand Up @@ -2194,6 +2196,31 @@ _lp_theme_activate() {
# Default value for LP_PERM when LP_ENABLE_PERM is 0
LP_PERM=${LP_MARK_PERM} # without color

_lp_user
local -i user=$?

# Yellow for root, bold if the user is not the login one, else no color.
if (( user < 2 )); then # if user is not root
# if user is not login user
if (( user == 1 )); then
LP_USER="${LP_COLOR_USER_ALT}${_LP_USER_SYMBOL}${NO_COL}"
elif (( LP_USER_ALWAYS )); then
LP_USER="${LP_COLOR_USER_LOGGED}${_LP_USER_SYMBOL}${NO_COL}"
else
LP_USER=
fi
if (( LP_ENABLE_SUDO )); then
LP_COLOR_MARK_NO_SUDO="$LP_COLOR_MARK"
fi
else # root!
LP_USER="${LP_COLOR_USER_ROOT}${_LP_USER_SYMBOL}${NO_COL}"
LP_COLOR_MARK="${LP_COLOR_MARK_ROOT}"
LP_COLOR_PATH="${LP_COLOR_PATH_ROOT}"
if (( ! LP_ENABLE_VCS_ROOT )); then
LP_MARK_DISABLED="$LP_MARK_DEFAULT"
fi
fi

# The connection is not expected to change from inside the shell, so we
# build this just once
_lp_hostname_color
Expand Down Expand Up @@ -2268,7 +2295,9 @@ _lp_theme_prompt() {
LP_TIME="$lp_analog_time_color "
fi

_lp_sudo_check
if _lp_sudo_active_color; then
LP_COLOR_MARK="$lp_sudo_active_color"
fi

# in main prompt: no space
if [[ "$LP_ENABLE_PROXY,${http_proxy-}" = 1,?* ]]; then
Expand Down

0 comments on commit 9ba5d28

Please sign in to comment.