3 source "$FEISTY_MEOW_SCRIPTS/core/functions.sh"
4 source "$FEISTY_MEOW_SCRIPTS/core/common.alias"
6 # just saying this is an array...
7 #declare -a PRIOR_TERMINAL_TITLES
9 # set the stack position if not already set.
10 if [ -z "$PTT_STACK_INDEX" ]; then
11 # this variable records where we will push new items on the stack.
15 # returns okay (0) if the stack is empty, or non-zero if not empty.
16 function ptt_stack_empty()
18 if [ -z "$PTT_STACK_INDEX" ]; then
19 # fix the index value.
23 test $PTT_STACK_INDEX -le 0
27 # a debugging function that should never have been necessary.
28 # a little bit furious the restore is failing during regenerate right now.
29 function show_terminal_titles()
32 echo "[terminal title list now has...]"
33 local i=${#PRIOR_TERMINAL_TITLES[@]}
34 if ptt_stack_empty; then
35 echo the terminal title list is empty.
38 echo "ent #$i: '${PRIOR_TERMINAL_TITLES[$i]}'"
44 # adds an entry into the stack of terminal titles.
45 function push_ptt_stack()
47 PRIOR_TERMINAL_TITLES[$PTT_STACK_INDEX]="$*"
49 #echo stack index incremented and now at $PTT_STACK_INDEX
53 function pop_ptt_stack()
55 if [ $PTT_STACK_INDEX -le 0 ]; then
56 echo nothing to pop from prior terminal titles stack.
59 #echo stack index decremented and now at $PTT_STACK_INDEX
60 CURRENT_TERM_TITLE="${PRIOR_TERMINAL_TITLES[$PTT_STACK_INDEX]}"
61 #echo "got the last title as '$CURRENT_TERM_TITLE'"
66 # puts a specific textual label on the terminal title bar.
67 # this doesn't consider any previous titles; it just labels the terminal.
68 # the title may not be visible in some window managers.
69 function set_terminal_title()
72 # if we weren't given a title, then use just the hostname. this is the degraded functionality.
73 if [ -z "${title}" ]; then
77 if [ "${TERM}" != "dumb" -a -z "$PBS_ENVIRONMENT" -a ! -z "$PS1" ]; then
78 echo -n -e "\033]0;${title}\007"
80 # not running interactively, so just echo the title.
87 # reads the current terminal title, if possible, and saves it to our stack of titles.
88 function save_terminal_title()
90 # save the former terminal title if we're running in X with xterm.
91 which xprop &>/dev/null
93 # make sure we're actually using xterm *and* that we have a window ID.
94 if [[ "$TERM" =~ .*"xterm".* && ! -z "$WINDOWID" ]]; then
95 local prior_title="$(xprop -id $WINDOWID | perl -nle 'print $1 if /^WM_NAME.+= \"(.*)\"$/')"
96 if [ ! -z "$prior_title" ]; then
97 #echo "saving prior terminal title as '$prior_title'"
98 push_ptt_stack "$prior_title"
100 #echo "not saving prior terminal title which was empty"
106 # using our stored terminal title, this replaces the title on the terminal.
107 function restore_terminal_title()
109 # we don't want to emit anything extra if this is being driven by git.
110 #hmmm... this could be a problem?
111 # if [ -z "$(echo $* | grep git)" ]; then
113 # run the terminal labeller to restore the prior title, if there was one.
114 if ! ptt_stack_empty; then
116 #echo "restoring prior terminal title of '$CURRENT_TERM_TITLE'"
117 set_terminal_title "$CURRENT_TERM_TITLE"
121 # sets a title for the terminal with the hostname and other details.
122 function label_terminal_with_info()
124 # we only label the terminal anew if there's no saved title.
125 if ptt_stack_empty; then
126 pruned_host=$(echo $HOSTNAME | sed -e 's/^\([^\.]*\)\..*$/\1/')
127 date_string=$(date +"%Y %b %e @ %T")
129 if [ -z "$user" ]; then
130 # try snagging the windoze name.
133 new_title="-- $user@$pruned_host -- [$date_string]"
134 set_terminal_title "$new_title"
136 # use the former title; paste it back up there just in case.
137 #echo "showing prior terminal title since there was a prior title!"
139 #echo "using prior terminal title of '$CURRENT_TERM_TITLE'"
140 set_terminal_title "$CURRENT_TERM_TITLE"
141 push_ptt_stack "$CURRENT_TERM_TITLE"