Skip to content

Commit

Permalink
Split out data pieces from hostname
Browse files Browse the repository at this point in the history
Rename _lp_hostname() to _lp_hostname_color() as it doesn't do any data.
Move calling of _lp_hostname_color() to _lp_theme_activate().
Break out _lp_connected_display() and _lp_chroot(). Both are very
simple, but maybe someone will want to add more checks.
Fix chroot not being printed if local and LP_HOSTNAME_ALWAYS not set.
  • Loading branch information
Rycieos committed Nov 25, 2020
1 parent a8aa8c9 commit c946155
Showing 1 changed file with 51 additions and 33 deletions.
84 changes: 51 additions & 33 deletions liquidprompt
Original file line number Diff line number Diff line change
Expand Up @@ -524,8 +524,6 @@ lp_activate() {
# Where are we? #
#################

_lp_hostname

if (( LP_ENABLE_SHORTEN_PATH )); then
if (( LP_PATH_KEEP == -1 )); then
# __lp_shorten_path becomes a noop
Expand Down Expand Up @@ -749,74 +747,89 @@ __lp_set_dirtrim() {
# Environment #
###############

# If we are connected with a X11 support
_lp_connected_display() {
[[ -n "${DISPLAY-}" ]]
}

_lp_connection() {
if [[ -n "${SSH_CLIENT-}${SSH2_CLIENT-}${SSH_TTY-}" ]]; then
lp_connection=ssh
return
fi

# tmux: see GH #304
# TODO check on *BSD
local whoami="$(LC_ALL=C who am i)"
if [[ x"$whoami" != *'('* || x"$whoami" = *'(:'* || x"$whoami" = *'(tmux'* ]]; then
lp_connection=lcl # Local
return
fi

local sess_parent="$(ps -o comm= -p $PPID 2> /dev/null)"
if [[ "$sess_parent" = "su" || "$sess_parent" = "sudo" ]]; then
lp_connection=su # Remote su/sudo
else
# tmux: see GH #304
# TODO check on *BSD
local whoami="$(LC_ALL=C who am i)"
local sess_parent="$(ps -o comm= -p $PPID 2> /dev/null)"
if [[ x"$whoami" != *'('* || x"$whoami" = *'(:'* || x"$whoami" = *'(tmux'* ]]; then
lp_connection=lcl # Local
elif [[ "$sess_parent" = "su" || "$sess_parent" = "sudo" ]]; then
lp_connection=su # Remote su/sudo
else
lp_connection=tel # Telnet
fi
lp_connection=tel # Telnet
fi
}

_lp_hostname() {
# 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
# The connection is not expected to change from inside the shell, so we
# build this just once
LP_HOST=""
_lp_chroot() {
if [[ -r /etc/debian_chroot ]]; then
IFS= read -r lp_chroot </etc/debian_chroot
[[ -n "$lp_chroot" ]] && return 0
fi
return 1
}

# 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
_lp_hostname_color() {
lp_hostname_color=

# Only process hostname elements if we haven't turned them off
if (( LP_HOSTNAME_ALWAYS != -1 )); then

[[ -r /etc/debian_chroot ]] && LP_HOST="($(< /etc/debian_chroot))"

# Which host symbol should we use?
if (( LP_ENABLE_FQDN )); then
LP_HOST_SYMBOL="${_LP_FQDN_SYMBOL}"
else
LP_HOST_SYMBOL="${_LP_HOST_SYMBOL}"
fi

# If we are connected with a X11 support
if [[ -n "${DISPLAY-}" ]]; then
LP_HOST="${LP_COLOR_X11_ON}${LP_HOST}@${NO_COL}"
if _lp_connected_display; then
lp_hostname_color="${LP_COLOR_X11_ON}"
else
LP_HOST="${LP_COLOR_X11_OFF}${LP_HOST}@${NO_COL}"
lp_hostname_color="${LP_COLOR_X11_OFF}"
fi

if _lp_chroot; then
lp_hostname_color+="(${lp_chroot})"
fi

_lp_connection
case "$lp_connection" in
lcl)
if (( LP_HOSTNAME_ALWAYS )); then
LP_HOST+="${LP_COLOR_HOST}${LP_HOST_SYMBOL}${NO_COL}"
lp_hostname_color+="@${LP_COLOR_HOST}${LP_HOST_SYMBOL}${NO_COL}"
else
# FIXME do we want to display the chroot if local?
LP_HOST="" # no hostname if local
lp_hostname_color+="$NO_COL" # no hostname if local
fi
;;
ssh)
# If we want a different color for each host
(( LP_ENABLE_SSH_COLORS )) && LP_COLOR_SSH="$LP_COLOR_HOST_HASH"
LP_HOST+="${LP_COLOR_SSH}${LP_HOST_SYMBOL}${NO_COL}"
lp_hostname_color+="@${LP_COLOR_SSH}${LP_HOST_SYMBOL}${NO_COL}"
;;
su)
LP_HOST+="${LP_COLOR_SU}${LP_HOST_SYMBOL}${NO_COL}"
lp_hostname_color+="@${LP_COLOR_SU}${LP_HOST_SYMBOL}${NO_COL}"
;;
tel)
LP_HOST+="${LP_COLOR_TELNET}${LP_HOST_SYMBOL}${NO_COL}"
lp_hostname_color+="@${LP_COLOR_TELNET}${LP_HOST_SYMBOL}${NO_COL}"
;;
*)
LP_HOST+="${LP_HOST_SYMBOL}" # defaults to no color
lp_hostname_color+="@${NO_COL}${LP_HOST_SYMBOL}" # defaults to no color
;;
esac
fi
Expand Down Expand Up @@ -2174,6 +2187,11 @@ _lp_theme_activate() {
# Default value for LP_PERM when LP_ENABLE_PERM is 0
LP_PERM=${LP_MARK_PERM} # without color

# The connection is not expected to change from inside the shell, so we
# build this just once
_lp_hostname_color
LP_HOST="$lp_hostname_color"

# Same as bash '\l', but inlined as a constant as the value will not change
# during the shell's life
LP_TTYN="$(basename -- "$(tty)" 2>/dev/null)"
Expand Down

0 comments on commit c946155

Please sign in to comment.