3 source $FEISTY_MEOW_SCRIPTS/core/functions.sh
5 # just saying this is an array...
6 declare -a PRIOR_TERMINAL_TITLES
7 # set the stack position if not already set.
8 if [ -z "$PTT_STACK_INDEX" ]; then
9 # this variable records where we will push new items on the stack.
13 # adds an entry into the stack of terminal titles.
14 function push_ptt_stack()
16 PRIOR_TERMINAL_TITLES[$PTT_STACK_INDEX]="$*"
18 echo ${PRIOR_TERMINAL_TITLES[@]}
20 echo stack index incremented and now at $PTT_STACK_INDEX
23 function pop_ptt_stack()
25 if [ $PTT_STACK_INDEX -le 0 ]; then
26 echo nothing to pop from prior terminal titles stack.
29 echo stack index decremented and now at $PTT_STACK_INDEX
30 CURRENT_TERM_TITLE="${PRIOR_TERMINAL_TITLES[$PTT_STACK_INDEX]}"
34 # returns okay (0) if the stack is empty, or non-zero if not empty.
35 function ptt_stack_empty()
37 test $PTT_STACK_INDEX -le 0
40 # puts a specific textual label on the terminal title bar.
41 # this doesn't consider any previous titles; it just labels the terminal.
42 # the title may not be visible in some window managers.
43 function set_terminal_title()
46 # if we weren't given a title, then use just the hostname. this is the degraded functionality.
47 if [ -z "${title}" ]; then
50 echo -n -e "\033]0;${title}\007"
53 # reads the current terminal title, if possible, and saves it to our stack of titles.
54 function save_terminal_title()
56 # save the former terminal title if we're running in X with xterm.
57 which xprop &>/dev/null
59 if [[ "$TERM" =~ .*"xterm".* ]]; then
60 local prior_title="$(xprop -id $WINDOWID | perl -nle 'print $1 if /^WM_NAME.+= \"(.*)\"$/')"
61 echo "saving prior terminal title as '$prior_title'"
62 push_ptt_stack "$prior_title"
67 # using our stored terminal title, this replaces the title on the terminal.
68 function restore_terminal_title()
70 # we don't want to emit anything extra if this is being driven by git.
71 #hmmm... this could be a problem?
72 # if [ -z "$(echo $* | grep git)" ]; then
74 # run the terminal labeller to restore the prior title, if there was one.
75 if ptt_stack_empty; then
76 echo prior titles were empty, so doing nothing.
79 echo "restoring prior terminal title of '$CURRENT_TERM_TITLE'"
80 set_terminal_title "$CURRENT_TERM_TITLE"
84 # sets a title for the terminal with the hostname and other details.
85 function label_terminal_with_info()
87 # we only label the terminal anew if there's no saved title.
88 # if [ -z "$PRIOR_TERMINAL_TITLE" ]; then
89 if ptt_stack_empty; then
90 pruned_host=$(echo $HOSTNAME | sed -e 's/^\([^\.]*\)\..*$/\1/')
91 date_string=$(date +"%Y %b %e @ %T")
93 if [ -z "$user" ]; then
94 # try snagging the windoze name.
97 new_title="-- $user@$pruned_host -- [$date_string]"
98 set_terminal_title "$new_title"
100 # restore the former title.
101 restore_terminal_title