7201784c160caa2f52a4c1a88f8614c091d74236
[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   # this is an important value now; it is checked for in save_terminal_title.
49   local term_title_found="unknown"
50   # save the former terminal title if we're running in X with xterm.
51   which xprop &>/dev/null
52   if [ $? -eq 0 ]; then
53     # gnome-terminal doesn't set WINDOWID currently, but we can work around this.
54     if [[ -z "$WINDOWID" && ! -z "$(which wininfo)" ]]; then
55 #not good solution.      term_title_found="$(xprop -id $(xdotool getactivewindow) | perl -nle 'print $1 if /^WM_NAME.+= \"(.*)\"$/')"
56       term_title_found=$(xwininfo -id $(xprop -root | awk '/NET_ACTIVE_WINDOW/ { print $5; exit }') | awk -F\" '/xwininfo:/ { print $2; exit }')
57     # check if we're actually using xterm *and* that we have a window ID.
58     elif [[ "$TERM" =~ .*"xterm".* && ! -z "$WINDOWID" ]]; then
59       term_title_found="$(xprop -id $WINDOWID | perl -nle 'print $1 if /^WM_NAME.+= \"(.*)\"$/')"
60     fi
61   fi
62   echo -n "$term_title_found"
63 }
64
65 # reads the current terminal title, if possible, and saves it to our record.
66 function save_terminal_title()
67 {
68   local title="$(get_terminal_title)"
69   if [ "$title" != "unknown" ]; then
70     # there was a title, so save it.
71     if [ ! -z "$DEBUG_TERM_TITLE" ]; then
72       echo "saving prior terminal title as '$title'"
73     fi
74     export PRIOR_TERMINAL_TITLE="$title"
75   else
76     # the terminal had no title, or we couldn't access it, or there's no terminal.
77     if [ ! -z "$DEBUG_TERM_TITLE" ]; then
78       echo "not saving prior terminal title which was empty"
79     fi
80   fi
81 }
82
83 # using our stored terminal title, this replaces the title on the terminal.
84 function restore_terminal_title()
85 {
86 # we don't want to emit anything extra if this is being driven by git.
87 #hmmm...  this could be a problem?
88 #  if [ -z "$(echo $* | grep git)" ]; then
89
90   # run the terminal labeller to restore the prior title, if there was one.
91   if [ ! -z "$PRIOR_TERMINAL_TITLE" ]; then
92     if [ ! -z "$DEBUG_TERM_TITLE" ]; then
93       echo "restoring prior terminal title of '$PRIOR_TERMINAL_TITLE'"
94     fi
95     apply_title_to_terminal "$PRIOR_TERMINAL_TITLE"
96   fi
97 }
98
99 # sets a title for the terminal with the hostname and other details.
100 function label_terminal_with_info()
101 {
102   # we only label the terminal anew if there's no saved title.
103   if [ -z "$PRIOR_TERMINAL_TITLE" ]; then
104     if [ ! -z "$DEBUG_TERM_TITLE" ]; then
105       echo "showing new generated title since prior title was empty"
106     fi
107     pruned_host=$(echo $HOSTNAME | sed -e 's/^\([^\.]*\)\..*$/\1/')
108     date_string=$(date +"%Y %b %e @ %T")
109     user="$(logname)"
110     if [ -z "$user" ]; then
111       # try snagging the windoze name.
112       user=$USERNAME
113     fi
114     new_title="-- $user@$pruned_host -- [$date_string]"
115     apply_title_to_terminal "$new_title"
116     save_terminal_title
117   else
118     # use the former title; paste it back up there just in case.
119     if [ ! -z "$DEBUG_TERM_TITLE" ]; then
120       echo "showing prior terminal title since there was a prior title!"
121       echo "using prior terminal title of '$PRIOR_TERMINAL_TITLE'"
122     fi
123     apply_title_to_terminal "$PRIOR_TERMINAL_TITLE"
124   fi
125 }
126
127