Merge branch 'master' of github.com:fredhamster/feisty_meow into dev
[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 # puts a specific textual label on the terminal title bar.
10 # this doesn't consider any previous titles; it just labels the terminal.
11 # the title may not be visible in some window managers.
12 function apply_title_to_terminal()
13 {
14   title="$*"
15   # if we weren't given a title, then use just the hostname.  this is the degraded functionality.
16   if [ -z "${title}" ]; then
17     title="$(hostname)"
18   fi
19   
20   if [ "${TERM}" != "dumb" -a -z "$PBS_ENVIRONMENT" -a \
21         ! -z "$PS1" -a "${TERM}" != "linux" ]; then
22     echo -n -e "\033]0;${title}\007"
23   else
24     # not running interactively, so just echo the title.
25     sep
26     echo ${title}
27     sep
28   fi
29 }
30
31 # user friendly version that saves the title being added.
32 function set_terminal_title()
33 {
34   apply_title_to_terminal $*
35
36 #tricky attempts to get it to be available when we ask for it in get_terminal_title
37   sync
38 #  echo -n
39
40 #  # we're enforcing a new title from here on.
41 #  unset PRIOR_TERMINAL_TITLE
42   save_terminal_title
43 }
44
45 # echoes back the current title on the terminal window, if we can acquire it.
46 function get_terminal_title()
47 {
48 #hmmm: totally failing since gnome doesn't provide the info we need like it should; WINDOWID is empty.
49 #hmmm: need to revise this somehow to work around that, but nothing is quite right so far.
50 #hmmm: had to disable the xwininfo approach because it's super messy (console noise) and still isn't right (just like xdotool approach wasn't right).
51
52   # this is an important value now; it is checked for in save_terminal_title.
53   local term_title_found="unknown"
54   # save the former terminal title if we're running in X with xterm.
55
56   # are we even running a terminal?
57 #hmmm: abstract out that condition below to check against a list of names.
58   if [[ "$TERM" =~ .*"xterm".* ]]; then
59     # check that we have a window ID.
60     if [[ ! -z "$WINDOWID" ]]; then
61       # the window id exists in the variable; can we get its info?
62       if [[ ! -z "$(which xprop)" ]]; then
63         # sweet, we have all the info we need to get this done.
64         term_title_found="$(xprop -id $WINDOWID | perl -nle 'print $1 if /^WM_NAME.+= \"(.*)\"$/')"
65       fi
66     else
67       # gnome-terminal doesn't set WINDOWID currently; we can try to work around this.
68       false
69 #hmmm: so far, none of these approaches are any good.
70 #      # not good solution; gets wrong titles.  plus uses xdotool which is not installed by default in ubuntu.
71 #      if [[ ! -z "$(which xdotool)" ]]; then
72 #        term_title_found="$(xprop -id $(xdotool getactivewindow) | perl -nle 'print $1 if /^WM_NAME.+= \"(.*)\"$/')"
73 #      fi
74 #      # this solution also fails by getting the wrong window title if this one isn't focussed.
75 #      if [[ ! -z "$(which xwininfo)" ]]; then
76 #        term_title_found=$(xwininfo -id $(xprop -root | awk '/NET_ACTIVE_WINDOW/ { print $5; exit }') | awk -F\" '/xwininfo:/ { print $2; exit }')
77 #      fi
78     fi
79   fi
80   echo -n "$term_title_found"
81 }
82
83 # reads the current terminal title, if possible, and saves it to our record.
84 function save_terminal_title()
85 {
86   local title="$(get_terminal_title)"
87   if [ "$title" != "unknown" ]; then
88     # there was a title, so save it.
89     if [ ! -z "$DEBUG_TERM_TITLE" ]; then
90       echo "saving prior terminal title as '$title'"
91     fi
92     export PRIOR_TERMINAL_TITLE="$title"
93   else
94     # the terminal had no title, or we couldn't access it, or there's no terminal.
95     if [ ! -z "$DEBUG_TERM_TITLE" ]; then
96       echo "not saving prior terminal title which was empty"
97     fi
98   fi
99 }
100
101 # using our stored terminal title, this replaces the title on the terminal.
102 function restore_terminal_title()
103 {
104 # we don't want to emit anything extra if this is being driven by git.
105 #hmmm...  this could be a problem?
106 #  if [ -z "$(echo $* | grep git)" ]; then
107
108   # run the terminal labeller to restore the prior title, if there was one.
109   if [ ! -z "$PRIOR_TERMINAL_TITLE" ]; then
110     if [ ! -z "$DEBUG_TERM_TITLE" ]; then
111       echo "restoring prior terminal title of '$PRIOR_TERMINAL_TITLE'"
112     fi
113     apply_title_to_terminal "$PRIOR_TERMINAL_TITLE"
114   fi
115 }
116
117 # sets a title for the terminal with the hostname and other details.
118 function label_terminal_with_info()
119 {
120   # we only label the terminal anew if there's no saved title.
121   if [ -z "$PRIOR_TERMINAL_TITLE" ]; then
122     if [ ! -z "$DEBUG_TERM_TITLE" ]; then
123       echo "showing new generated title since prior title was empty"
124     fi
125     pruned_host=$(echo $HOSTNAME | sed -e 's/^\([^\.]*\)\..*$/\1/')
126     date_string=$(date +"%Y %b %e @ %T")
127     user="$(logname)"
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     apply_title_to_terminal "$new_title"
134     save_terminal_title
135   else
136     # use the former title; paste it back up there just in case.
137     if [ ! -z "$DEBUG_TERM_TITLE" ]; then
138       echo "showing prior terminal title since there was a prior title!"
139       echo "using prior terminal title of '$PRIOR_TERMINAL_TITLE'"
140     fi
141     apply_title_to_terminal "$PRIOR_TERMINAL_TITLE"
142   fi
143 }
144
145