Skip to content

Instantly share code, notes, and snippets.

@kriswill
Last active September 10, 2016 06:57
Show Gist options
  • Save kriswill/352cd8b5021977feba90edf59214936c to your computer and use it in GitHub Desktop.
Save kriswill/352cd8b5021977feba90edf59214936c to your computer and use it in GitHub Desktop.
Prints information about the terminal application you are using
# prints information about the terminal application you are using
function terminfo() {
# make OPTIND local to prevent odd behaviour with getops when running a function multiple times
local OPTIND v
local _verbose=0
while getopts "v" OPTION
do
case $OPTION in
v) _verbose=1
;;
esac
done
# OPTIND = The value of the last option argument processed by the getopts builtin command
shift "$((OPTIND-1))"
# get the process ID for the current shell
local pid=$$
# search for the top-most process parent, extracting the command
while true; do
# store the last pid, so we can recover it when we hit the root process
lastpid=$pid
# get the parent pid, xargs to trim whitespace off
pid=$(ps -h -o ppid= -p $pid 2>/dev/null | xargs)
# if we hit root pid (1), jump out of the loop
[[ $(echo $pid) == 1 ]] && pid=$lastpid && break
# get the full executable path
comm=$(ps -h -o comm= -p $pid 2>/dev/null)
# basename gets only the executable name
base=`basename "${comm}" 2>/dev/null`
done
# find the app version number if on a Mac
if [[ "$OSTYPE" == "darwin"* ]]; then
if [[ $comm == *".app"* ]]; then
# parse out the beginning of the path, up to xxx.app -- bash-foo
macapp="${comm%".app"*}.app"
# use plutil to parse out app's info plist, which is XML, blech
version=$(plutil -p "${macapp}/Contents/Info.plist" | grep "CFBundleShortVersionString")
# if there is a value, then get the real version number
if [[ $? -ne 0 ]]; then
version=$(plutil -p "${macapp}/Contents/Info.plist" | grep "CFBundleVersion")
fi
# parse out the digits
version=$(echo $version | grep -o '"[[:digit:].]*"' | sed 's/"//g')
fi
fi
# print the info - in color!
B="\033[0;34m" # blue
C="\033[0;49;96m" # cyan
W="\033[0;37m" # white
printf "\n${C}Application${B}: ${W}${base}"
printf "\n${C}Process ID${B}: ${W}${pid}"
test ${version+x} &&
printf "\n${C}Version${B}: ${W}${version}"
if [[ $_verbose -ne 0 ]]; then
test ${macapp+x} &&
printf "\n${C}Full Path${B}: ${W}${macapp}"
# TODO: maybe add some more info here
fi
printf "\n\n"
}
@kriswill
Copy link
Author

In iTerm2, example:

→ terminfo

Application: iTerm2
Process ID:  67017
Version:     3.0.8

or Terminal:

→ terminfo

Application: Terminal
Process ID:  44476
Version:     2.6.1

or HyperTerm:

→ terminfo

Application: HyperTerm
Process ID:  542
Version:     0.7.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment