terminal title back in black
[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 ! -z "$PS1" ]; then
21     echo -n -e "\033]0;${title}\007"
22   else
23     # not running interactively, so just echo the title.
24     sep
25     echo ${title}
26     sep
27   fi
28 }
29
30 # user friendly version that saves the title being added.
31 function set_terminal_title()
32 {
33   apply_title_to_terminal $*
34   save_terminal_title
35 }
36
37 # reads the current terminal title, if possible, and saves it to our record.
38 function save_terminal_title()
39 {
40   # save the former terminal title if we're running in X with xterm.
41   which xprop &>/dev/null
42   if [ $? -eq 0 ]; then
43     # make sure we're actually using xterm *and* that we have a window ID.
44     if [[ "$TERM" =~ .*"xterm".* && ! -z "$WINDOWID" ]]; then
45       local prior_title="$(xprop -id $WINDOWID | perl -nle 'print $1 if /^WM_NAME.+= \"(.*)\"$/')"
46       if [ ! -z "$prior_title" ]; then
47         if [ ! -z "$DEBUG_TERM_TITLE" ]; then
48           echo "saving prior terminal title as '$prior_title'"
49         fi
50         export PRIOR_TERMINAL_TITLE="$prior_title"
51       else
52         if [ ! -z "$DEBUG_TERM_TITLE" ]; then
53           echo "not saving prior terminal title which was empty"
54         fi
55       fi
56     fi
57   fi
58 }
59
60 # using our stored terminal title, this replaces the title on the terminal.
61 function restore_terminal_title()
62 {
63 # we don't want to emit anything extra if this is being driven by git.
64 #hmmm...  this could be a problem?
65 #  if [ -z "$(echo $* | grep git)" ]; then
66
67   # run the terminal labeller to restore the prior title, if there was one.
68   if [ ! -z "$PRIOR_TERMINAL_TITLE" ]; then
69     if [ ! -z "$DEBUG_TERM_TITLE" ]; then
70       echo "restoring prior terminal title of '$PRIOR_TERMINAL_TITLE'"
71     fi
72     apply_title_to_terminal "$PRIOR_TERMINAL_TITLE"
73   fi
74 }
75
76 # sets a title for the terminal with the hostname and other details.
77 function label_terminal_with_info()
78 {
79   # we only label the terminal anew if there's no saved title.
80   if [ -z "$PRIOR_TERMINAL_TITLE" ]; then
81     if [ ! -z "$DEBUG_TERM_TITLE" ]; then
82       echo "showing new generated title since prior title was empty"
83     fi
84     pruned_host=$(echo $HOSTNAME | sed -e 's/^\([^\.]*\)\..*$/\1/')
85     date_string=$(date +"%Y %b %e @ %T")
86     user=$USER
87     if [ -z "$user" ]; then
88       # try snagging the windoze name.
89       user=$USERNAME
90     fi
91     new_title="-- $user@$pruned_host -- [$date_string]"
92     apply_title_to_terminal "$new_title"
93     save_terminal_title
94   else
95     # use the former title; paste it back up there just in case.
96     if [ ! -z "$DEBUG_TERM_TITLE" ]; then
97       echo "showing prior terminal title since there was a prior title!"
98       echo "using prior terminal title of '$PRIOR_TERMINAL_TITLE'"
99     fi
100     apply_title_to_terminal "$PRIOR_TERMINAL_TITLE"
101   fi
102 }
103
104