terminal stuff is hosed
[feisty_meow.git] / scripts / tty / terminal_titler.sh
1 #!/bin/bash
2
3 source "$FEISTY_MEOW_SCRIPTS/core/functions.sh"
4 source "$FEISTY_MEOW_SCRIPTS/core/common.alias"
5
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.
11
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.
15   PTT_STACK_INDEX=0
16 fi
17
18 # returns okay (0) if the stack is empty, or non-zero if not empty.
19 function ptt_stack_empty()
20 {
21   if [ -z "$PTT_STACK_INDEX" ]; then
22     # fix the index value.
23     PTT_STACK_INDEX=0
24     true
25   else
26     test $PTT_STACK_INDEX -le 0
27   fi
28 }
29
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()
33 {
34   sep 14
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.
39   else
40     while ((i--)); do
41       echo "ent #$i: '${PRIOR_TERMINAL_TITLES[$i]}'"
42     done
43   fi
44   sep 14
45 }
46
47 # adds an entry into the stack of terminal titles.
48 function push_ptt_stack()
49 {
50   PRIOR_TERMINAL_TITLES[$PTT_STACK_INDEX]="$*"
51   ((PTT_STACK_INDEX++))
52 echo stack index incremented and now at $PTT_STACK_INDEX
53 show_terminal_titles
54 }
55
56 function pop_ptt_stack()
57 {
58   if [ $PTT_STACK_INDEX -le 0 ]; then
59     echo nothing to pop from prior terminal titles stack.
60   else
61     ((PTT_STACK_INDEX--))
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'"
65 show_terminal_titles
66   fi
67 }
68
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()
73 {
74   title="$*"
75   # if we weren't given a title, then use just the hostname.  this is the degraded functionality.
76   if [ -z "${title}" ]; then
77     title="$(hostname)"
78   fi
79   
80   if [ "${TERM}" != "dumb" -a -z "$PBS_ENVIRONMENT" -a ! -z "$PS1" ]; then
81     echo -n -e "\033]0;${title}\007"
82   else
83     # not running interactively, so just echo the title.
84     sep
85     echo ${title}
86     sep
87   fi
88 }
89
90 # user friendly version that saves the title being added.
91 function set_terminal_title()
92 {
93   save_terminal_title
94   reset_terminal_title $*
95 #reading a line made it update.
96 #echo "what is title now?"
97 #read line
98 #this doesn't make it update.  echo -n
99 echo
100   save_terminal_title
101 }
102
103 # reads the current terminal title, if possible, and saves it to our stack of titles.
104 function save_terminal_title()
105 {
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"
115       else
116 echo "not saving prior terminal title which was empty"
117       fi
118     fi
119   fi
120 }
121
122 # using our stored terminal title, this replaces the title on the terminal.
123 function restore_terminal_title()
124 {
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
128
129   # run the terminal labeller to restore the prior title, if there was one.
130   if ! ptt_stack_empty; then
131     pop_ptt_stack
132 echo "restoring prior terminal title of '$CURRENT_TERM_TITLE'"
133     reset_terminal_title "$CURRENT_TERM_TITLE"
134   fi
135 }
136
137 # sets a title for the terminal with the hostname and other details.
138 function label_terminal_with_info()
139 {
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")
145     user=$USER
146     if [ -z "$user" ]; then
147       # try snagging the windoze name.
148       user=$USERNAME
149     fi
150     new_title="-- $user@$pruned_host -- [$date_string]"
151     reset_terminal_title "$new_title"
152   else
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!"
155     pop_ptt_stack
156 echo "using prior terminal title of '$CURRENT_TERM_TITLE'"
157     reset_terminal_title "$CURRENT_TERM_TITLE"
158     push_ptt_stack "$CURRENT_TERM_TITLE"
159   fi
160 }
161
162