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