Skip to content

Commit

Permalink
Breakout hostname determine code from color
Browse files Browse the repository at this point in the history
This wasn't done before because I thought that deciding whether to
display the hostname or not would be up to the themes. But I now think
that it is more useful to make the hostname display work the same
accross all themes, and obey the LP_HOSTNAME_ALWAYS config option. This
is what _lp_hostname() now does, and now _lp_hostname_color() only
colors the hostname based on the connection, and adds chroot and a "@"
to it.

Update the Powerline theme to use the new data function.
  • Loading branch information
Rycieos committed Nov 25, 2020
1 parent 230c9d7 commit 8de1a72
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 48 deletions.
62 changes: 39 additions & 23 deletions liquidprompt
Original file line number Diff line number Diff line change
Expand Up @@ -961,12 +961,7 @@ _lp_sudo_active_color() {
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
_lp_hostname_color() {
lp_hostname_color=

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

Expand All @@ -977,40 +972,61 @@ _lp_hostname_color() {
LP_HOST_SYMBOL="${_LP_HOST_SYMBOL}"
fi

if _lp_connected_display; then
lp_hostname_color="${LP_COLOR_X11_ON}"
else
lp_hostname_color="${LP_COLOR_X11_OFF}"
fi
lp_hostname=${LP_HOST_SYMBOL}

if _lp_chroot; then
lp_hostname_color+="(${lp_chroot})"
_lp_connection
if [[ $lp_connection == lcl ]] && ! (( LP_HOSTNAME_ALWAYS )); then
# no hostname if local
return 1
fi
else
return 1
fi
}

_lp_connection
# 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() {
if _lp_connected_display; then
lp_hostname_color="${LP_COLOR_X11_ON}"
else
lp_hostname_color="${LP_COLOR_X11_OFF}"
fi

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

if _lp_hostname; then
case "$lp_connection" in
lcl)
if (( LP_HOSTNAME_ALWAYS )); then
lp_hostname_color+="@${LP_COLOR_HOST}${LP_HOST_SYMBOL}${NO_COL}"
else
lp_hostname_color+="$NO_COL" # no hostname if local
fi
lp_hostname_color+="@${LP_COLOR_HOST}${lp_hostname}${NO_COL}"
;;
ssh)
# If we want a different color for each host
(( LP_ENABLE_SSH_COLORS )) && LP_COLOR_SSH="$LP_COLOR_HOST_HASH"
lp_hostname_color+="@${LP_COLOR_SSH}${LP_HOST_SYMBOL}${NO_COL}"
lp_hostname_color+="@${LP_COLOR_SSH}${lp_hostname}${NO_COL}"
;;
su)
lp_hostname_color+="@${LP_COLOR_SU}${LP_HOST_SYMBOL}${NO_COL}"
lp_hostname_color+="@${LP_COLOR_SU}${lp_hostname}${NO_COL}"
;;
tel)
lp_hostname_color+="@${LP_COLOR_TELNET}${LP_HOST_SYMBOL}${NO_COL}"
lp_hostname_color+="@${LP_COLOR_TELNET}${lp_hostname}${NO_COL}"
;;
*)
lp_hostname_color+="@${NO_COL}${LP_HOST_SYMBOL}" # defaults to no color
lp_hostname_color+="@${NO_COL}${lp_hostname}" # defaults to no color
;;
esac
else
if [[ -n $lp_chroot ]]; then
# End the color of the chroot
lp_hostname_color+=${NO_COL}
else
# Nothing to display
lp_hostname_color=""
return 1
fi
fi
}

Expand Down
43 changes: 18 additions & 25 deletions themes/powerline/powerline.theme
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,28 @@ _lp_powerline_theme_activate() {
[[ -z ${POWERLINE_VCS_STASH_COLOR[@]+x} ]] && POWERLINE_VCS_STASH_COLOR=(220 236 0 0 3 0)
[[ -z ${POWERLINE_ERROR_COLOR[@]+x} ]] && POWERLINE_ERROR_COLOR=(231 52 0 0 7 1)

_LP_DISPLAY_HOST=1
# Which host symbol should we use?
if (( LP_HOSTNAME_ALWAYS != -1 )); then
local lp_connection
_lp_connection

if [[ $lp_connection == lcl ]] && (( LP_HOSTNAME_ALWAYS == 0 )); then
_LP_DISPLAY_HOST=0
else
if (( LP_ENABLE_FQDN )); then
_LP_HOST_SYMBOL="${_LP_FQDN_SYMBOL}"
fi

case "$lp_connection" in
ssh) _LP_HOST_ICON="${POWERLINE_SECURE_MARKER} " ;;
su) _LP_HOST_ICON="${POWERLINE_ROOT_MARKER} " ;;
*) _LP_HOST_ICON="" ;;
esac
fi
else
_LP_DISPLAY_HOST=0
__powerline_hostname_generate
}

__powerline_hostname_generate() {
_POWERLINE_HOSTNAME=
local lp_hostname lp_connection
if _lp_hostname; then
_POWERLINE_HOSTNAME=${lp_hostname}

case "$lp_connection" in
ssh) _POWERLINE_HOST_ICON="${POWERLINE_SECURE_MARKER} " ;;
su) _POWERLINE_HOST_ICON="${POWERLINE_ROOT_MARKER} " ;;
*) _POWERLINE_HOST_ICON="" ;;
esac
fi
}

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

Expand All @@ -60,11 +53,11 @@ _lp_powerline_theme_prompt() {
local section_arrow lp_terminal_format _lp_last_af_color _lp_last_ab_color
PS1=''

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

PS1+="${lp_terminal_format} ${_LP_HOST_ICON}${_LP_HOST_SYMBOL} ${section_arrow}"
PS1+="${lp_terminal_format} ${_POWERLINE_HOST_ICON}${_POWERLINE_HOSTNAME} ${section_arrow}"
fi

lp_terminal_format "${POWERLINE_USER_COLOR[@]}"
Expand Down

0 comments on commit 8de1a72

Please sign in to comment.