Skip to content

Commit

Permalink
Add _lp_username() to return username
Browse files Browse the repository at this point in the history
Make the return codes to have a distinct code for when the username is
disabled (LP_USER_ALWAYS=-1) vs just not shown (logged in user).

Add valid value for LP_USER_ALWAYS: -1, which disables username display.

This prompted a hack in the Powerline theme to get the possibility of no
username to work, so I need to make a more elegant seperator solution.
  • Loading branch information
Rycieos committed Dec 4, 2020
1 parent bc120d5 commit debb794
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 24 deletions.
44 changes: 31 additions & 13 deletions liquidprompt
Original file line number Diff line number Diff line change
Expand Up @@ -936,16 +936,31 @@ _lp_software_collections_color() {
# return: 0 if login user, 1 if not, 2 if root
_lp_user() {
if (( EUID == 0 )); then
# if user is root
# user is root
return 2
elif [[ "${USER}" != "$(logname 2>/dev/null || printf '%s' "${LOGNAME-}")" ]]; then
# if user is not login user
# user is not login user
return 1
else
return 0
fi
}

# Return the username (if we should display one).
_lp_username() {
if (( LP_USER_ALWAYS == -1 )); then
# No username ever
lp_username=''
return 2
elif (( LP_USER_ALWAYS )) || ! _lp_user; then
lp_username=${_LP_USER_SYMBOL}
return 0
else
lp_username=''
return 1
fi
}

# Test the code with the commands:
# sudo id # sudo, enter your credentials
# sudo -K # revoke your credentials
Expand Down Expand Up @@ -2417,23 +2432,26 @@ _lp_default_theme_activate() {
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}"
local -i user_type=$?

if _lp_username; then
# Yellow for root, bold if the user is not the login one, else no color.
if (( user_type == 2 )); then
LP_USER="${LP_COLOR_USER_ROOT}${lp_username}${NO_COL}"
elif (( user_type == 1 )); then
LP_USER="${LP_COLOR_USER_ALT}${lp_username}${NO_COL}"
else
LP_USER=
LP_USER="${LP_COLOR_USER_LOGGED}${lp_username}${NO_COL}"
fi
else
LP_USER=
fi

if (( user_type < 2 )); then # if user is not root
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
Expand Down
10 changes: 5 additions & 5 deletions liquidpromptrc-dist
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ LP_HOSTNAME_ALWAYS=0
# the hostname is displayed
LP_ENABLE_FQDN=0

# Always display the user name, even if the user is the same as the one logged
# in.
# Defaults to 1 (always display the user name)
# set to 0 if you want to hide the logged user (it will always display different
# users)
# When to display the user name:
# 1: always display the user name
# 0: hide the logged user (always display different users)
# -1: never display the user name
# Default value is 1
LP_USER_ALWAYS=1

# Display the percentages of load/batteries along with their
Expand Down
32 changes: 26 additions & 6 deletions themes/powerline/powerline.theme
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ _lp_powerline_theme_activate() {
[[ -z ${POWERLINE_ERROR_COLOR[@]+x} ]] && POWERLINE_ERROR_COLOR=(231 52 0 0 7 1)

__powerline_hostname_generate
__powerline_username_generate
}

__powerline_hostname_generate() {
Expand All @@ -38,13 +39,26 @@ __powerline_hostname_generate() {
fi
}

__powerline_username_generate() {
_POWERLINE_USERNAME=
local lp_username
if _lp_username; then
_POWERLINE_USERNAME=${lp_username}
fi
}

_lp_powerline_theme_directory() {
# Not all terminals support Powerline special characters in the title
local title=${_LP_USER_SYMBOL}
local title=
if [[ -n $_POWERLINE_USERNAME ]]; then
title+=${_POWERLINE_USERNAME}
fi
if [[ -n $_POWERLINE_HOSTNAME ]]; then
title+="@${_POWERLINE_HOSTNAME}"
fi
title+=":${lp_path}"

[[ -n $title ]] && title+=":"
title+="${lp_path}"

_lp_raw_title "$title"
}
Expand All @@ -55,13 +69,19 @@ _lp_powerline_theme_prompt() {

if [[ -n $_POWERLINE_HOSTNAME ]]; then
lp_terminal_format "${POWERLINE_HOST_COLOR[@]}"
__powerline_section_arrow "${POWERLINE_USER_COLOR[@]}"

PS1+="${lp_terminal_format} ${_POWERLINE_HOST_ICON}${_POWERLINE_HOSTNAME} ${section_arrow}"
PS1+="${lp_terminal_format} ${_POWERLINE_HOST_ICON}${_POWERLINE_HOSTNAME} "

if [[ -n $_POWERLINE_USERNAME ]]; then
__powerline_section_arrow "${POWERLINE_USER_COLOR[@]}"
PS1+="${section_arrow}"
fi
fi

lp_terminal_format "${POWERLINE_USER_COLOR[@]}"
PS1+="${lp_terminal_format} ${_LP_USER_SYMBOL} "
if [[ -n $_POWERLINE_USERNAME ]]; then
lp_terminal_format "${POWERLINE_USER_COLOR[@]}"
PS1+="${lp_terminal_format} ${_POWERLINE_USERNAME} "
fi

local lp_python_env
if _lp_python_env; then
Expand Down

0 comments on commit debb794

Please sign in to comment.