still spelunking in terminal title dirt
[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   echo terminal title list now has:
31   local i=${#PRIOR_TERMINAL_TITLES[@]}
32   if ptt_stack_empty; then
33     echo the list is empty
34   else
35     while ((i--)); do
36       echo "ent #$i: '${PRIOR_TERMINAL_TITLES[$i]}'"
37     done
38   fi
39 }
40
41 # adds an entry into the stack of terminal titles.
42 function push_ptt_stack()
43 {
44   PRIOR_TERMINAL_TITLES[$PTT_STACK_INDEX]="$*"
45   ((PTT_STACK_INDEX++))
46 echo stack index incremented and now at $PTT_STACK_INDEX
47 show_terminal_titles
48 }
49
50 function pop_ptt_stack()
51 {
52   if [ $PTT_STACK_INDEX -le 0 ]; then
53     echo nothing to pop from prior terminal titles stack.
54   else
55     ((PTT_STACK_INDEX--))
56 echo stack index decremented and now at $PTT_STACK_INDEX
57     CURRENT_TERM_TITLE="${PRIOR_TERMINAL_TITLES[$PTT_STACK_INDEX]}"
58 echo "got the last title as '$CURRENT_TERM_TITLE'"
59 show_terminal_titles
60   fi
61 }
62
63 # puts a specific textual label on the terminal title bar.
64 # this doesn't consider any previous titles; it just labels the terminal.
65 # the title may not be visible in some window managers.
66 function set_terminal_title()
67 {
68   title="$*"
69   # if we weren't given a title, then use just the hostname.  this is the degraded functionality.
70   if [ -z "${title}" ]; then
71     title="$(hostname)"
72   fi
73   echo -n -e "\033]0;${title}\007"
74 }
75
76 # reads the current terminal title, if possible, and saves it to our stack of titles.
77 function save_terminal_title()
78 {
79   # save the former terminal title if we're running in X with xterm.
80   which xprop &>/dev/null
81   if [ $? -eq 0 ]; then
82     if [[ "$TERM" =~ .*"xterm".* ]]; then
83       local prior_title="$(xprop -id $WINDOWID | perl -nle 'print $1 if /^WM_NAME.+= \"(.*)\"$/')"
84       if [ ! -z "$prior_title" ]; then
85 echo "saving prior terminal title as '$prior_title'"
86         push_ptt_stack "$prior_title"
87       else
88 echo "not saving prior terminal title which was empty"
89       fi
90     fi
91   fi
92 }
93
94 # using our stored terminal title, this replaces the title on the terminal.
95 function restore_terminal_title()
96 {
97 # we don't want to emit anything extra if this is being driven by git.
98 #hmmm...  this could be a problem?
99 #  if [ -z "$(echo $* | grep git)" ]; then
100
101   # run the terminal labeller to restore the prior title, if there was one.
102   if ptt_stack_empty; then
103 echo prior titles were empty, so doing nothing.
104   else
105     pop_ptt_stack
106 echo "restoring prior terminal title of '$CURRENT_TERM_TITLE'"
107     set_terminal_title "$CURRENT_TERM_TITLE"
108   fi
109 }
110
111 # sets a title for the terminal with the hostname and other details.
112 function label_terminal_with_info()
113 {
114   # we only label the terminal anew if there's no saved title.
115   if ptt_stack_empty; then
116     pruned_host=$(echo $HOSTNAME | sed -e 's/^\([^\.]*\)\..*$/\1/')
117     date_string=$(date +"%Y %b %e @ %T")
118     user=$USER
119     if [ -z "$user" ]; then
120       # try snagging the windoze name.
121       user=$USERNAME
122     fi
123     new_title="-- $user@$pruned_host -- [$date_string]"
124     set_terminal_title "$new_title"
125   else
126     # restore the former title.
127 #no    restore_terminal_title
128 echo "showing prior terminal title since there was a prior title!"
129     pop_ptt_stack
130 echo "using prior terminal title of '$CURRENT_TERM_TITLE'"
131     set_terminal_title "$CURRENT_TERM_TITLE"
132     push_ptt_stack "$CURRENT_TERM_TITLE"
133   fi
134 }
135
136