Merge pull request #562 from dylanaraps/pid

Term: Move pid handling to separate functions
This commit is contained in:
Dylan Araps 2016-12-25 08:40:50 +11:00 committed by GitHub
commit d7c99ecbc9
1 changed files with 62 additions and 39 deletions

101
neofetch
View File

@ -1507,47 +1507,28 @@ get_term() {
# If function was run, stop here. # If function was run, stop here.
(( "$term_run" == 1 )) && return (( "$term_run" == 1 )) && return
# Workaround for macOS systems that
# don't support the block below.
case "$TERM_PROGRAM" in
"iTerm.app") term="iTerm2" ;;
"Terminal.app") term="Apple Terminal" ;;
"Hyper") term="HyperTerm" ;;
*) term="${TERM_PROGRAM/\.app}" ;;
esac
# Check $PPID for terminal emulator. # Check $PPID for terminal emulator.
case "$os" in while [[ -z "$term" ]]; do
"Mac OS X") parent="$(get_ppid "$parent")"
# Workaround for macOS systems that name="$(get_process_name "$parent")"
# don't support the block below.
case "$TERM_PROGRAM" in
"iTerm.app") term="iTerm2" ;;
"Terminal.app") term="Apple Terminal" ;;
"Hyper") term="HyperTerm" ;;
*) term="${TERM_PROGRAM/\.app}" ;;
esac
return
;;
"Windows") case "${name// }" in
parent="$(ps -p "${1:-$PPID}" | awk '{printf $2}')" "${SHELL/*\/}" | *"sh" | "tmux"* | "screen" | "su"*) ;;
parent="${parent/'PPID'}" "login"* | *"Login"* | "init" | "(init)") term="$(tty)" ;;
"ruby" | "1" | "systemd" | "sshd"* | "python"* | "USER"*"PID"*) break ;;
name="$(ps -p "$parent" | awk '{printf $8}')" "gnome-terminal-") term="gnome-terminal" ;;
name="${name/'COMMAND'}" *) term="${name##*/}" ;;
name="${name/*\/}" esac
;; done
"Linux")
parent="$(grep -i -F "PPid:" "/proc/${1:-$PPID}/status")"
name="$(< "/proc/$(trim "${parent/PPid:}")/comm")"
;;
*)
parent="$(ps -p "${1:-$PPID}" -o ppid=)"
name="$(ps -p "$parent" -o comm=)"
;;
esac
case "${name// }" in
"${SHELL/*\/}" | *"sh" | "tmux"* | "screen" | "su") get_term "$parent" ;;
"login"* | *"Login"* | "init" | "(init)") term="$(tty)" ;;
"ruby" | "1" | "systemd" | "sshd"* | "python"* | "USER"*"PID"*) unset term ;;
"gnome-terminal-") term="gnome-terminal" ;;
*) term="${name##*/}" ;;
esac
# Log that the function was run. # Log that the function was run.
term_run=1 term_run=1
@ -3066,6 +3047,48 @@ convert_time() {
printf "%s" "$week_day $day $month $year $time" printf "%s" "$week_day $day $month $year $time"
} }
get_ppid() {
# Get parent process ID of PID.
case "$os" in
"Windows")
ppid="$(ps -p "${1:-$PPID}" | awk '{printf $2}')"
ppid="${ppid/'PPID'}"
;;
"Linux")
ppid="$(grep -i -F "PPid:" "/proc/${1:-$PPID}/status")"
ppid="$(trim "${ppid/PPid:}")"
;;
*)
ppid="$(ps -p "${1:-$PPID}" -o ppid=)"
;;
esac
printf "%s" "$ppid"
}
get_process_name() {
# Get PID name.
case "$os" in
"Windows")
name="$(ps -p "${1:-$PPID}" | awk '{printf $8}')"
name="${name/'COMMAND'}"
name="${name/*\/}"
;;
"Linux")
name="$(< "/proc/${1:-$PPID}/comm")"
;;
*)
name="$(ps -p "${1:-$PPID}" -o comm=)"
;;
esac
printf "%s" "$name"
}
# FINISH UP # FINISH UP
usage() { printf "%s" "\ usage() { printf "%s" "\