Reimplemented using only shell builtins and more 10 time faster the retrieval of a given First Ancestor Process of a given PID in zsh's `powermeli` theme

master
Meliurwen 3 years ago
parent 3162ee16e2
commit 92afae2bc7
Signed by: meliurwen
GPG Key ID: 818A8B35E9F1CE10
  1. 62
      zsh/.config/zsh/plugins/powermeli.zsh-theme

@ -28,30 +28,40 @@ prompt_segment() {
unset separator unset separator
} }
# Return the first ancestor PID of the PID of this process with the name issued # Get First Ancestor Process
# From: https://unix.stackexchange.com/a/459007 # Requires: <TARGET_PNAME> <PID_TARGET_OR_CHILD>
# TODO: Consider reading from `/proc/<pid>/*` using only POSIX shell builtins # It returns: <PID> <PPID> <PNAME>
# Note: the `/proc` is not guaranteed to be on all unices (see BSD, Mac...) # If TARGET_PNAME not found returns nothing
# See: # Note: `/proc` is not guaranteed to be on all unices (BSD, Solaris, Mac...)
# - https://stackoverflow.com/a/50457487 __get_fap(){
# - https://stackoverflow.com/a/1525673 pname_target=$1
get_apid() { pname=""
ps -Ao pid= -o ppid= -o comm= | pid=""
awk -v p="$$" -v ancestor="$1" ' ppid=$2
{ while read -r line; do
pid = $1; ppid[pid] = $2 case $line in
sub(/([[:space:]]*[[:digit:]]+){2}[[:space:]]*/, "") "Name:"*)
name[pid] = $0 pname=${line##Name: }
} ;;
END { "Pid:"*)
while (p) { pid=${line##Pid: }
if ( name[p] == ancestor ) { ;;
print p, name[p] "PPid:"*)
exit ppid=${line##PPid: }
} if [ "$pname" = "$pname_target" ]; then
p = ppid[p] printf "%s %s %s\n" "$pid" "$ppid" "$pname"
} elif [ "$ppid" = "0" ]; then
}' :
else
__get_fap "$pname_target" "$ppid"
fi
unset pname_target pname pid ppid
break
;;
*)
;;
esac
done < "/proc/$ppid/status"
} }
prompt_context() { prompt_context() {
@ -61,7 +71,7 @@ prompt_context() {
prompt_segment "$SEGMENT_SEPARATOR" black default prompt_segment "$SEGMENT_SEPARATOR" black default
fi fi
# Check if inside an ssh session, also if using `sudo su` # Check if inside an ssh session, also if using `sudo su`
if [ -z ${SSH_CLIENT+x} ] && [ -z ${SSH_TTY+x} ] && [ -z "$(get_apid sshd)" ]; then if [ -z ${SSH_CLIENT+x} ] && [ -z ${SSH_TTY+x} ] && [ -z "$(__get_fap sshd $$)" ]; then
PROMPT_LINE="$PROMPT_LINE %n " PROMPT_LINE="$PROMPT_LINE %n "
else else
PROMPT_LINE="$PROMPT_LINE %n%{%F{blue}%}@%{%F{cyan}%}%m " PROMPT_LINE="$PROMPT_LINE %n%{%F{blue}%}@%{%F{cyan}%}%m "
@ -149,7 +159,7 @@ build_prompt() {
unset OLD_BG OLD_SEP unset OLD_BG OLD_SEP
printf "%s%s " "%{%f%b%k%}" "$PROMPT_LINE" printf "%s%s " "%{%f%b%k%}" "$PROMPT_LINE"
unset PROMPT_LINE unset PROMPT_LINE
unset -f prompt_segment prompt_status prompt_virtualenv prompt_context prompt_git get_apid unset -f prompt_segment prompt_status prompt_virtualenv prompt_context prompt_git __get_fap
} }
setopt prompt_subst setopt prompt_subst

Loading…
Cancel
Save