#!/bin/sh -e
#
# fgetpid - get pid and pipe to pstree
# dependencies: fzf | pstree

blue=$(tput setaf 6)
nocolor=$(tput sgr0)

if command -v px >/dev/null 2>&1; then
  pid=$(px | sed 1d | awk 'BEGIN { OFS="\t" } { print $1, $2, $NF }' \
    | fzf --height='70%')
else
  pid=$(ps ax -o pid,comm,command | sed 1d | fzf --height='70%')
fi

selected=$(echo "${pid}" | awk '{ print $1 }')
printf '%s\n' "${blue}${selected}${nocolor}"
# shellcheck disable=SC2312
# pstree "$(echo "${pid}" | cut -f 1)"
pstree "${selected}"
