redoing terminal title code
[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 # set the stack position if not already set.
8 if [ -z "$PTT_STACK_INDEX" ]; then
9   # this variable records where we will push new items on the stack.
10   PTT_STACK_INDEX=0
11 fi
12
13 # adds an entry into the stack of terminal titles.
14 function push_ptt_stack()
15 {
16   PRIOR_TERMINAL_TITLES[$PTT_STACK_INDEX]="$*"
17 echo now list has:
18 echo ${PRIOR_TERMINAL_TITLES[@]}
19   ((PTT_STACK_INDEX++))
20 echo stack index incremented and now at $PTT_STACK_INDEX
21 }
22
23 function pop_ptt_stack()
24 {
25   if [ $PTT_STACK_INDEX -le 0 ]; then
26     echo nothing to pop from prior terminal titles stack.
27   else
28     ((PTT_STACK_INDEX--))
29 echo stack index decremented and now at $PTT_STACK_INDEX
30     CURRENT_TERM_TITLE="${PRIOR_TERMINAL_TITLES[$PTT_STACK_INDEX]}"
31   fi
32 }
33
34 # returns okay (0) if the stack is empty, or non-zero if not empty.
35 function ptt_stack_empty()
36 {
37   test $PTT_STACK_INDEX -le 0
38 }
39
40 # puts a specific textual label on the terminal title bar.
41 # this doesn't consider any previous titles; it just labels the terminal.
42 # the title may not be visible in some window managers.
43 function set_terminal_title()
44 {
45   title="$*"
46   # if we weren't given a title, then use just the hostname.  this is the degraded functionality.
47   if [ -z "${title}" ]; then
48     title="$(hostname)"
49   fi
50   echo -n -e "\033]0;${title}\007"
51 }
52
53 # reads the current terminal title, if possible, and saves it to our stack of titles.
54 function save_terminal_title()
55 {
56   # save the former terminal title if we're running in X with xterm.
57   which xprop &>/dev/null
58   if [ $? -eq 0 ]; then
59     if [[ "$TERM" =~ .*"xterm".* ]]; then
60       local prior_title="$(xprop -id $WINDOWID | perl -nle 'print $1 if /^WM_NAME.+= \"(.*)\"$/')"
61 echo "saving prior terminal title as '$prior_title'"
62       push_ptt_stack "$prior_title"
63     fi
64   fi
65 }
66
67 # using our stored terminal title, this replaces the title on the terminal.
68 function restore_terminal_title()
69 {
70 # we don't want to emit anything extra if this is being driven by git.
71 #hmmm...  this could be a problem?
72 #  if [ -z "$(echo $* | grep git)" ]; then
73
74   # run the terminal labeller to restore the prior title, if there was one.
75   if ptt_stack_empty; then
76 echo prior titles were empty, so doing nothing.
77   else
78     pop_ptt_stack
79 echo "restoring prior terminal title of '$CURRENT_TERM_TITLE'"
80     set_terminal_title "$CURRENT_TERM_TITLE"
81   fi
82 }
83
84 # sets a title for the terminal with the hostname and other details.
85 function label_terminal_with_info()
86 {
87   # we only label the terminal anew if there's no saved title.
88 #  if [ -z "$PRIOR_TERMINAL_TITLE" ]; then
89   if ptt_stack_empty; then
90     pruned_host=$(echo $HOSTNAME | sed -e 's/^\([^\.]*\)\..*$/\1/')
91     date_string=$(date +"%Y %b %e @ %T")
92     user=$USER
93     if [ -z "$user" ]; then
94       # try snagging the windoze name.
95       user=$USERNAME
96     fi
97     new_title="-- $user@$pruned_host -- [$date_string]"
98     set_terminal_title "$new_title"
99   else
100     # restore the former title.
101     restore_terminal_title
102   fi
103 }
104
105