Created
February 6, 2017 21:25
-
-
Save philpennock/4959c3ef7327bf6d8d5a7cb4ee15de45 to your computer and use it in GitHub Desktop.
Extracts from 'prompt pdp' for SSH
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function prompt_query_ssh_status { | |
local suffix oIFS ss keycount | |
local -a keylines | |
if [[ -z ${SSH_AUTH_SOCK:-} ]]; then print "n/a"; return; fi | |
suffix=R | |
[[ -n ${SSH_AGENT_PID:-} ]] && suffix=L | |
if [[ $OSTYPE == darwin* ]]; then | |
case $SSH_AUTH_SOCK in | |
(/tmp/launch-*) suffix=D ;; | |
(/private/tmp/com.apple.launchd.*/Listeners) suffix=D ;; | |
esac | |
fi | |
# When listing keys in OpenSSH 6.4p1, if some blobs can't be decoded, | |
# then output from "ssh-add -l" only goes to stdout if stdout is a tty | |
# (and no I don't understand why). "ssh-add -L" is more reliable. | |
# It's a little more output, but it's also less work inside ssh-add, | |
# so we use -L here. | |
# Note: -L will include output even for keys of types not recognised | |
# locally; after the stderr will be " comment" to stdout. | |
# So because we want only _usable_ keys, we want to remove from the | |
# count any lines where the first character is a space. | |
oIFS="$IFS" | |
IFS=$'\n' | |
keylines=($(ssh-add -L 2>/dev/null)) | |
ss=$? | |
IFS="$oIFS" | |
# "The agent has no identities." goes to stdout. | |
# 255 returned when unrecognized parts because remote ssh-agent | |
# supports key-types which we don't support locally. Not | |
# documented. It's the result of a fatal() call. However, | |
# by using -L, we don't invoke the code-path which can call | |
# fatal(). nb-for-gist: this was fixed a few OpenSSH releases ago | |
if [[ $ss -ne 0 ]]; then | |
keycount=0 | |
else | |
keycount="${#keylines:# *}" | |
fi | |
#local keycount="${#${(f):-"$(ssh-add -l)"}}" | |
print "${keycount}${suffix}" | |
} | |
#... | |
function prompt_pdp_precmd { | |
#... | |
if (( _prompt_pdp_ssh )); then | |
if [[ $_prompt_pdp_ssh_redo == yes ]]; then | |
unset _prompt_pdp_ssh_redo | |
_prompt_pdp_cache_ssh_status="$(prompt_query_ssh_status)" | |
fi | |
psvar[6]="$_prompt_pdp_cache_ssh_status" | |
psvar[7]=t | |
[[ $psvar[6] == 0? ]] && psvar[7]='' | |
fi | |
#... | |
} | |
function prompt_pdp_preexec_ssh { | |
# todo: use not-=~ but something which works when =~ is not advanced? | |
if [[ $1 =~ '(?:^.*/)|\bssh-add\b' ]]; then | |
_prompt_pdp_ssh_redo=yes | |
fi | |
} | |
#... | |
function prompt_pdp_setup { | |
#... | |
_prompt_pdp_ssh=0 | |
zstyle -t "$style_ctx" show-sshkeys && _prompt_pdp_ssh=1 | |
#... | |
if (( _prompt_pdp_ssh )) && (( poor_regex )); then | |
print -u2 " ... so skipping ssh-agent support" | |
zstyle "$style_ctx" show-gpgagent off | |
elif (( _prompt_pdp_ssh )); then | |
local sshstatus | |
add-zsh-hook preexec prompt_pdp_preexec_ssh | |
if is-at-least 4.3.7 ; then | |
sshstatus="%(7V,%{$fg[%sshkeys]%},%{$fg[%sshkeys_none]%})%6v$rs" | |
_prompt_pdp_ssh_redo=yes | |
else | |
local ss ckey='%sshkeys' | |
ss=$(prompt_query_ssh_status) | |
[[ $ss == 0? ]] && ckey='%sshkeys_none' | |
sshstatus="%{$fg[$ckey]%}${ss}$rs" | |
unset ss ckey | |
fi | |
identities+=("$sshstatus") | |
fi | |
#... | |
local idsep="%{$fg[%divider]%}⋮$rs" | |
eval identities_section="\${(j,${idsep},)identities}" | |
#... | |
local p1 | |
local -a pa | |
local empty='%{%}' | |
#... | |
pa+=( $identities_section ) # kerberos, ssh, etc | |
p1="${(j::)pa}" | |
PS1="${p1//$empty/}" | |
#... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment