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