ENH make hostname lookup more robust in SSH sessions

This commit is contained in:
Nathan Dwarshuis 2022-08-29 12:56:13 -04:00
parent 494bc770d5
commit 0a6f5b37d9
1 changed files with 45 additions and 36 deletions

View File

@ -5,6 +5,39 @@ setopt correctall
bindkey -v
bindkey -v '^?' backward-delete-char
## --------------------------------------------------
## Helper functions
## --------------------------------------------------
exists () {
command -v "$1" &> /dev/null
}
alias_if_N () {
while [ "$1" != "--" ]; do
exists "$1" || return 1
shift
done
shift
for a in "$@"; do
alias "$a"
done
}
alias_if () {
alias_if_N "$1" -- "${@:2}"
}
alias_if_else () {
if ! alias_if_N "$1" -- "$2"; then
alias "$3"
fi
}
alias_if_sudo () {
alias_if_N "$1" -- "${@:2}"
}
## --------------------------------------------------
# autocompletion
## --------------------------------------------------
@ -48,6 +81,7 @@ zstyle ':completion:*:(ssh|scp|rsync):*:hosts' ignored-patterns '*(.|:)*' loopba
## --------------------------------------------------
# syntax highlighting a la fish
## --------------------------------------------------
hlpath="$XDG_DATA_HOME/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"
if [[ -e "$hlpath" ]]; then
. "$hlpath"
@ -86,7 +120,15 @@ fi
HOSTNAME=""
if [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then
HOSTNAME="@%M"
if exists hostnamectl; then
literal_hostname="$(hostnamectl hostname)"
else
literal_hostname="$(cat /etc/hostname)"
fi
if [ -n "$literal_hostname" ]; then
#HOSTNAME="@%M"
HOSTNAME="@$literal_hostname"
fi
fi
## git prompt
@ -185,39 +227,6 @@ export KEYTIMEOUT=1
precmd () { echo -ne "\033]0;urxvt: ${PWD}\007" }
## --------------------------------------------------
## Alias helper functions
## --------------------------------------------------
exists () {
command -v "$1" &> /dev/null
}
alias_if_N () {
while [ "$1" != "--" ]; do
exists "$1" || return 1
shift
done
shift
for a in "$@"; do
alias "$a"
done
}
alias_if () {
alias_if_N "$1" -- "${@:2}"
}
alias_if_else () {
if ! alias_if_N "$1" -- "$2"; then
alias "$3"
fi
}
alias_if_sudo () {
alias_if_N "$1" -- "${@:2}"
}
## --------------------------------------------------
## Aliases
## --------------------------------------------------