diff --git a/zsh.d/prompts/pure/pure.zsh b/zsh.d/prompts/pure/pure.zsh index f62a67a..2a4a79b 100644 --- a/zsh.d/prompts/pure/pure.zsh +++ b/zsh.d/prompts/pure/pure.zsh @@ -621,6 +621,51 @@ prompt_pure_reset_vim_prompt_widget() { } prompt_pure_state_setup() { + setopt localoptions noshwordsplit + + # Check SSH_CONNECTION and the current state. + local ssh_connection=${SSH_CONNECTION:-$PROMPT_PURE_SSH_CONNECTION} + local username hostname + + if [[ -z $ssh_connection ]] && (( $+commands[who] )); then + # When changing user on a remote system, the $SSH_CONNECTION + # environment variable can be lost. Attempt detection via `who`. + local who_out + who_out=$(who -m 2>/dev/null) + if (( $? )); then + # Who am I not supported, fallback to plain who. + local -a who_in + who_in=( ${(f)"$(who 2>/dev/null)"} ) + who_out="${(M)who_in:#*[[:space:]]${TTY#/dev/}[[:space:]]*}" + fi + + local reIPv6='(([0-9a-fA-F]+:)|:){2,}[0-9a-fA-F]+' # Simplified, only checks partial pattern. + local reIPv4='([0-9]{1,3}\.){3}[0-9]+' # Simplified, allows invalid ranges. + # Here we assume two non-consecutive periods represents a + # hostname. This matches `foo.bar.baz`, but not `foo.bar`. + local reHostname='([.][^. ]+){2}' + + # Usually the remote address is surrounded by parenthesis, but + # not on all systems (e.g. busybox). + local -H MATCH MBEGIN MEND + if [[ $who_out =~ "\(?($reIPv4|$reIPv6|$reHostname)\)?\$" ]]; then + ssh_connection=$MATCH + + # Export variable to allow detection propagation inside + # shells spawned by this one (e.g. tmux does not always + # inherit the same tty, which breaks detection). + export PROMPT_PURE_SSH_CONNECTION=$ssh_connection + fi + unset MATCH MBEGIN MEND + fi + + hostname='%F{$prompt_pure_colors[host]}@%m%f' + # Show `username@host` if logged in through SSH. + [[ -n $ssh_connection ]] && username='%F{$prompt_pure_colors[user]}%n%f'"$hostname" + + # # Show `username@host` if root, with username in default color. + # [[ $UID -eq 0 ]] && username='%F{$prompt_pure_colors[user:root]}%n%f'"$hostname" + typeset -gA prompt_pure_state prompt_pure_state[version]="1.11.0" prompt_pure_state+=(