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