diff --git a/pure.zsh b/pure.zsh index 3d230920..28bd1836 100644 --- a/pure.zsh +++ b/pure.zsh @@ -484,10 +484,26 @@ prompt_pure_state_setup() { 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. - # Ignore local X sessions displayed as (:1). - if who am i | grep -v '(:[0-9]*)$' | grep -q '(.*)$' &>/dev/null; then + local who_out + who_out=$(who -m 2>/dev/null) + if (( $? )); then + # Who am I not supported, fallback to plain who. + who_out=$(who 2>/dev/null | grep ${TTY#/dev/}) + fi + + local reIPv6='([a-f0-9:]+:+)+[a-f0-9]+' # Simplified, but matches loopback as well (::1). + 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=true fi + unset MATCH MBEGIN MEND fi # show username@host if logged in through SSH