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