- # save the former terminal title if we're running in X with xterm.
- which xprop &>/dev/null
- if [ $? -eq 0 ]; then
- # make sure we're actually using xterm *and* that we have a window ID.
- if [[ "$TERM" =~ .*"xterm".* && ! -z "$WINDOWID" ]]; then
- local prior_title="$(xprop -id $WINDOWID | perl -nle 'print $1 if /^WM_NAME.+= \"(.*)\"$/')"
- if [ ! -z "$prior_title" ]; then
- if [ ! -z "$DEBUG_TERM_TITLE" ]; then
- echo "saving prior terminal title as '$prior_title'"
- fi
- export PRIOR_TERMINAL_TITLE="$prior_title"
- else
- if [ ! -z "$DEBUG_TERM_TITLE" ]; then
- echo "not saving prior terminal title which was empty"
- fi
- fi
+ # whack our output variable, just in case.
+ unset LAST_TITLE
+ # get our stack top.
+ peek_title_stack
+ # trim the popped item out of the stack.
+ if [ ! -z "$TERMINAL_TITLE_STACK" ]; then
+ TERMINAL_TITLE_STACK="$(echo $TERMINAL_TITLE_STACK | sed -n -e 's/\(.*\),[^,]*$/\1/p')"
+ fi
+}
+
+# like pop, but does not change the stack, effectively handing you the most
+# recently set title.
+function peek_title_stack()
+{
+ # whack our output variable, just in case.
+ unset LAST_TITLE
+
+ if [ ! -z "$TERMINAL_TITLE_STACK" ]; then
+ LAST_TITLE="$(echo $TERMINAL_TITLE_STACK | sed -n -e 's/.*","\([^,]*\)"$/\1/p')"
+ if [ -z "$LAST_TITLE" ]; then
+ LAST_TITLE="$(echo $TERMINAL_TITLE_STACK | sed -n -e 's/"//gp')"