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
8 #and that whole approach fails; arrays aren't passed to subshells, so any commands
9 # that execute in a subshell don't know about the contents and thus don't do what we want.
10 #hmmm: revise back to single level memory.
12 # set the stack position if not already set.
13 if [ -z "$PTT_STACK_INDEX" ]; then
14 # this variable records where we will push new items on the stack.
18 # returns okay (0) if the stack is empty, or non-zero if not empty.
19 function ptt_stack_empty()
21 if [ -z "$PTT_STACK_INDEX" ]; then
22 # fix the index value.
26 test $PTT_STACK_INDEX -le 0
30 # a debugging function that should never have been necessary.
31 # a little bit furious the restore is failing during regenerate right now.
32 function show_terminal_titles()
35 echo "[terminal title list now has...]"
36 local i=${#PRIOR_TERMINAL_TITLES[@]}
37 if ptt_stack_empty; then
38 echo the terminal title list is empty.
41 echo "ent #$i: '${PRIOR_TERMINAL_TITLES[$i]}'"
47 # adds an entry into the stack of terminal titles.
48 function push_ptt_stack()
50 PRIOR_TERMINAL_TITLES[$PTT_STACK_INDEX]="$*"
52 echo stack index incremented and now at $PTT_STACK_INDEX
56 function pop_ptt_stack()
58 if [ $PTT_STACK_INDEX -le 0 ]; then
59 echo nothing to pop from prior terminal titles stack.
62 echo stack index decremented and now at $PTT_STACK_INDEX
63 CURRENT_TERM_TITLE="${PRIOR_TERMINAL_TITLES[$PTT_STACK_INDEX]}"
64 echo "got the last title as '$CURRENT_TERM_TITLE'"
69 # puts a specific textual label on the terminal title bar.
70 # this doesn't consider any previous titles; it just labels the terminal.
71 # the title may not be visible in some window managers.
72 function reset_terminal_title()
75 # if we weren't given a title, then use just the hostname. this is the degraded functionality.
76 if [ -z "${title}" ]; then
80 if [ "${TERM}" != "dumb" -a -z "$PBS_ENVIRONMENT" -a ! -z "$PS1" ]; then
81 echo -n -e "\033]0;${title}\007"
83 # not running interactively, so just echo the title.
90 # user friendly version that saves the title being added.
91 function set_terminal_title()
94 reset_terminal_title $*
95 #reading a line made it update.
96 #echo "what is title now?"
98 #this doesn't make it update. echo -n
103 # reads the current terminal title, if possible, and saves it to our stack of titles.
104 function save_terminal_title()
106 # save the former terminal title if we're running in X with xterm.
107 which xprop &>/dev/null
108 if [ $? -eq 0 ]; then
109 # make sure we're actually using xterm *and* that we have a window ID.
110 if [[ "$TERM" =~ .*"xterm".* && ! -z "$WINDOWID" ]]; then
111 local prior_title="$(xprop -id $WINDOWID | perl -nle 'print $1 if /^WM_NAME.+= \"(.*)\"$/')"
112 if [ ! -z "$prior_title" ]; then
113 echo "saving prior terminal title as '$prior_title'"
114 push_ptt_stack "$prior_title"
116 echo "not saving prior terminal title which was empty"
122 # using our stored terminal title, this replaces the title on the terminal.
123 function restore_terminal_title()
125 # we don't want to emit anything extra if this is being driven by git.
126 #hmmm... this could be a problem?
127 # if [ -z "$(echo $* | grep git)" ]; then
129 # run the terminal labeller to restore the prior title, if there was one.
130 if ! ptt_stack_empty; then
132 echo "restoring prior terminal title of '$CURRENT_TERM_TITLE'"
133 reset_terminal_title "$CURRENT_TERM_TITLE"
137 # sets a title for the terminal with the hostname and other details.
138 function label_terminal_with_info()
140 # we only label the terminal anew if there's no saved title.
141 if ptt_stack_empty; then
142 echo "showing new generated title since title stack was empty"
143 pruned_host=$(echo $HOSTNAME | sed -e 's/^\([^\.]*\)\..*$/\1/')
144 date_string=$(date +"%Y %b %e @ %T")
146 if [ -z "$user" ]; then
147 # try snagging the windoze name.
150 new_title="-- $user@$pruned_host -- [$date_string]"
151 reset_terminal_title "$new_title"
153 # use the former title; paste it back up there just in case.
154 echo "showing prior terminal title since there was a prior title!"
156 echo "using prior terminal title of '$CURRENT_TERM_TITLE'"
157 reset_terminal_title "$CURRENT_TERM_TITLE"
158 push_ptt_stack "$CURRENT_TERM_TITLE"