dropped terminal title oracle; things are good now
[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 # uncomment this to get extra noisy debugging.
7 export DEBUG_TERM_TITLE=true
8
9 ##############
10
11 # puts a specific textual label on the terminal title bar.
12 # this doesn't consider any previous titles; it just labels the terminal.
13 # the title may not be visible in some window managers.
14 function apply_title_to_terminal()
15 {
16   title="$*"
17   # if we weren't given a title, then use just the hostname.  this is the degraded functionality.
18   if [ -z "${title}" ]; then
19     title="$(hostname)"
20   fi
21   
22   if [ "${TERM}" != "dumb" -a -z "$PBS_ENVIRONMENT" -a \
23         ! -z "$PS1" -a "${TERM}" != "linux" ]; then
24     echo -n -e "\033]0;${title}\007"
25   else
26     # not running interactively, so just echo the title.
27     sep
28     echo ${title}
29     sep
30   fi
31 }
32
33 ##############
34
35 # records the current terminal title, pushing it down on the stack of titles,
36 # possibly prior to a new one being used.
37 function save_terminal_title()
38 {
39   local title="$*"
40
41   if [ -z "$title" ]; then
42 echo "empty title: pushing current title again"
43     peek_title_stack
44     title="$LAST_TITLE"
45     if [ -z "$title" ]; then
46       echo "there was no saved title, so we're ignoring the save attempt."
47       return 1
48     fi
49   fi
50
51 #hmmm: need a validation step here to remove bad chars that conflict with our title compression scheme.
52
53   # only slap a comma after the existing value if it wasn't empty.
54   if [ -z "$TERMINAL_TITLE_STACK" ]; then
55     export TERMINAL_TITLE_STACK="\"$title\""
56   else
57     export TERMINAL_TITLE_STACK="$TERMINAL_TITLE_STACK,\"$title\""
58   fi
59
60 echo new terminal title stack is:
61 echo $TERMINAL_TITLE_STACK
62 }
63
64 # takes a terminal title off the stack and sets the LAST_TITLE variable.
65 function pop_title_stack()
66 {
67   # whack our output variable, just in case.
68   unset LAST_TITLE
69   # get our stack top.
70   peek_title_stack
71   # trim the popped item out of the stack.
72   if [ ! -z "$TERMINAL_TITLE_STACK" ]; then
73     TERMINAL_TITLE_STACK="$(echo $TERMINAL_TITLE_STACK | sed -n -e 's/\(.*\),[^,]*$/\1/p')"
74   fi
75 }
76
77 # like pop, but does not change the stack, effectively handing you the most
78 # recently set title.
79 function peek_title_stack()
80 {
81   # whack our output variable, just in case.
82   unset LAST_TITLE
83
84   if [ ! -z "$TERMINAL_TITLE_STACK" ]; then
85     LAST_TITLE="$(echo $TERMINAL_TITLE_STACK | sed -n -e 's/.*","\([^,]*\)"$/\1/p')"
86     if [ -z "$LAST_TITLE" ]; then
87       LAST_TITLE="$(echo $TERMINAL_TITLE_STACK | sed -n -e 's/"//gp')"
88     fi
89   fi
90 }
91
92 # using our stored terminal title, this replaces the title on the terminal.
93 function restore_terminal_title()
94 {
95   # run the terminal labeller to restore the prior title, if there was one.
96   pop_title_stack
97
98   if [ ! -z "$LAST_TITLE" ]; then
99     if [ ! -z "$DEBUG_TERM_TITLE" ]; then
100       echo "restoring prior terminal title of '$LAST_TITLE'"
101       echo "while new title stack is: $TERMINAL_TITLE_STACK"
102     fi
103     apply_title_to_terminal "$LAST_TITLE"
104   fi
105 }
106
107 # sets a title for the terminal with the hostname and other details.
108 function label_terminal_with_info()
109 {
110   # we only label the terminal anew if there's no saved title.
111   if [ -z "$TERMINAL_TITLE_STACK" ]; then
112     if [ ! -z "$DEBUG_TERM_TITLE" ]; then
113       echo "showing new generated title since prior title was empty"
114     fi
115     pruned_host=$(echo $HOSTNAME | sed -e 's/^\([^\.]*\)\..*$/\1/')
116     date_string=$(date +"%Y %b %e @ %T")
117     user="$(logname)"
118     if [ -z "$user" ]; then
119       # try snagging the windoze name.
120       user=$USERNAME
121     fi
122     new_title="-- $user@$pruned_host -- [$date_string]"
123     apply_title_to_terminal "$new_title"
124     save_terminal_title "$new_title"
125   else
126     # use the former title; paste it back up there just in case.
127     peek_title_stack
128     apply_title_to_terminal "$LAST_TITLE"
129   fi
130 }
131
132 ##############
133
134 # user friendly version sets the terminal title and saves the title being added.
135 function set_terminal_title()
136 {
137   apply_title_to_terminal $*
138   save_terminal_title $*
139 }
140
141 ##############
142
143