do not emit term codes if not term
[feisty_meow.git] / scripts / tty / terminal_titler.sh
1 #!/bin/bash
2
3 source "$FEISTY_MEOW_SCRIPTS/core/functions.sh"
4
5 # just saying this is an array...
6 #declare -a PRIOR_TERMINAL_TITLES
7
8 # set the stack position if not already set.
9 if [ -z "$PTT_STACK_INDEX" ]; then
10   # this variable records where we will push new items on the stack.
11   PTT_STACK_INDEX=0
12 fi
13
14 # returns okay (0) if the stack is empty, or non-zero if not empty.
15 function ptt_stack_empty()
16 {
17   if [ -z "$PTT_STACK_INDEX" ]; then
18     # fix the index value.
19     PTT_STACK_INDEX=0
20     true
21   else
22     test $PTT_STACK_INDEX -le 0
23   fi
24 }
25
26 # a debugging function that should never have been necessary.
27 # a little bit furious the restore is failing during regenerate right now.
28 function show_terminal_titles()
29 {
30   sep 14
31   echo "[terminal title list now has...]"
32   local i=${#PRIOR_TERMINAL_TITLES[@]}
33   if ptt_stack_empty; then
34     echo the terminal title list is empty.
35   else
36     while ((i--)); do
37       echo "ent #$i: '${PRIOR_TERMINAL_TITLES[$i]}'"
38     done
39   fi
40   sep 14
41 }
42
43 # adds an entry into the stack of terminal titles.
44 function push_ptt_stack()
45 {
46   PRIOR_TERMINAL_TITLES[$PTT_STACK_INDEX]="$*"
47   ((PTT_STACK_INDEX++))
48 #echo stack index incremented and now at $PTT_STACK_INDEX
49 #show_terminal_titles
50 }
51
52 function pop_ptt_stack()
53 {
54   if [ $PTT_STACK_INDEX -le 0 ]; then
55     echo nothing to pop from prior terminal titles stack.
56   else
57     ((PTT_STACK_INDEX--))
58 #echo stack index decremented and now at $PTT_STACK_INDEX
59     CURRENT_TERM_TITLE="${PRIOR_TERMINAL_TITLES[$PTT_STACK_INDEX]}"
60 #echo "got the last title as '$CURRENT_TERM_TITLE'"
61 #show_terminal_titles
62   fi
63 }
64
65 # puts a specific textual label on the terminal title bar.
66 # this doesn't consider any previous titles; it just labels the terminal.
67 # the title may not be visible in some window managers.
68 function set_terminal_title()
69 {
70   title="$*"
71   # if we weren't given a title, then use just the hostname.  this is the degraded functionality.
72   if [ -z "${title}" ]; then
73     title="$(hostname)"
74   fi
75   
76   if [ -z "$PS1" ]; then
77     # not running interactively, so just echo the title.
78     sep
79     echo ${title}
80     sep
81   else
82     echo -n -e "\033]0;${title}\007"
83   fi
84 }
85
86 # reads the current terminal title, if possible, and saves it to our stack of titles.
87 function save_terminal_title()
88 {
89   # save the former terminal title if we're running in X with xterm.
90   which xprop &>/dev/null
91   if [ $? -eq 0 ]; then
92     # make sure we're actually using xterm *and* that we have a window ID.
93     if [[ "$TERM" =~ .*"xterm".* && ! -z "$WINDOWID" ]]; then
94       local prior_title="$(xprop -id $WINDOWID | perl -nle 'print $1 if /^WM_NAME.+= \"(.*)\"$/')"
95       if [ ! -z "$prior_title" ]; then
96 #echo "saving prior terminal title as '$prior_title'"
97         push_ptt_stack "$prior_title"
98 #      else
99 #echo "not saving prior terminal title which was empty"
100       fi
101     fi
102   fi
103 }
104
105 # using our stored terminal title, this replaces the title on the terminal.
106 function restore_terminal_title()
107 {
108 # we don't want to emit anything extra if this is being driven by git.
109 #hmmm...  this could be a problem?
110 #  if [ -z "$(echo $* | grep git)" ]; then
111
112   # run the terminal labeller to restore the prior title, if there was one.
113   if ! ptt_stack_empty; then
114     pop_ptt_stack
115 #echo "restoring prior terminal title of '$CURRENT_TERM_TITLE'"
116     set_terminal_title "$CURRENT_TERM_TITLE"
117   fi
118 }
119
120 # sets a title for the terminal with the hostname and other details.
121 function label_terminal_with_info()
122 {
123   # we only label the terminal anew if there's no saved title.
124   if ptt_stack_empty; then
125     pruned_host=$(echo $HOSTNAME | sed -e 's/^\([^\.]*\)\..*$/\1/')
126     date_string=$(date +"%Y %b %e @ %T")
127     user=$USER
128     if [ -z "$user" ]; then
129       # try snagging the windoze name.
130       user=$USERNAME
131     fi
132     new_title="-- $user@$pruned_host -- [$date_string]"
133     set_terminal_title "$new_title"
134   else
135     # use the former title; paste it back up there just in case.
136 #echo "showing prior terminal title since there was a prior title!"
137     pop_ptt_stack
138 #echo "using prior terminal title of '$CURRENT_TERM_TITLE'"
139     set_terminal_title "$CURRENT_TERM_TITLE"
140     push_ptt_stack "$CURRENT_TERM_TITLE"
141   fi
142 }
143
144