#! /bin/bash
#
# xterm_title - print the contents of the xterm title bar
#
# Derived from http://www.clark.net/pub/dickey/xterm/xterm.faq.html#how2_title
#
P=${0##*/}
[ -z "$DISPLAY" ] && {
	echo "${P}: not running X" >&2
	exit 1
}

if [ -z "$TERM" ] || [ "$TERM" != "xterm" ]; then
	echo "${P}: not running in an xterm" >&2
	exit 1
fi

array=( "6n:cursor position:??:?:\([0-9]*\);\([0-9]*\)/line \1, col \2"
        "11t:window state:??:?"
        "13t:window position:????:?:\(^\|;\)/+"
        "14t:window size in pix:????:?:;/x"
        "18t:window size in char:????:?:;/x"
        "19t:screen size in char:????:?:;/x"
        "20t:window icon label:???:??"
        "21t:window title:???:??")

exec </dev/tty
old=$(stty -g)
stty raw -echo min 0  time 3
IFS=$'\n'
for parm in ${array[@]}
do
    IFS=':'
    set -f
    set -- $parm
    echo -e "\233${1}\c" > /dev/tty
    IFS=''
    read -r a
    b=${a#$3}
    [ "$5" ] && b=$(echo $b | sed -e "s/${5}/g")
    printf "%-20s %s\n\r" "$2" "${b%$4}" # adding \r because raw tty
    done
stty $old
exit 0
# no need to resseting IFS=$' \t\n' because end off script;)

