3 source "$FEISTY_MEOW_SCRIPTS/core/functions.sh"
4 source "$FEISTY_MEOW_SCRIPTS/core/common.alias"
6 # uncomment this to get extra noisy debugging.
7 #export DEBUG_TERM_TITLE=true
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()
15 # if we weren't given a title, then use just the hostname. this is the degraded functionality.
16 if [ -z "${title}" ]; then
20 if [ "${TERM}" != "dumb" -a -z "$PBS_ENVIRONMENT" -a \
21 ! -z "$PS1" -a "${TERM}" != "linux" ]; then
22 echo -n -e "\033]0;${title}\007"
24 # not running interactively, so just echo the title.
31 # user friendly version that saves the title being added.
32 function set_terminal_title()
34 apply_title_to_terminal $*
36 #tricky tries to get it to be available when we ask for it in get_terminal_title
40 # # we're enforcing a new title from here on.
41 # unset PRIOR_TERMINAL_TITLE
45 # echoes back the current title on the terminal window, if we can acquire it.
46 function get_terminal_title()
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
53 # make sure we're actually using xterm *and* that we have a window ID.
54 if [[ ! -z "$GNOME_TERMINAL_SCREEN" ]]; then
55 term_title_found="$(xprop -id $WINDOWID | perl -nle 'print $1 if /^WM_NAME.+= \"(.*)\"$/')"
56 elif [[ "$TERM" =~ .*"xterm".* && ! -z "$WINDOWID" ]]; then
57 term_title_found="$(xprop -id $WINDOWID | perl -nle 'print $1 if /^WM_NAME.+= \"(.*)\"$/')"
60 echo -n "$term_title_found"
63 # reads the current terminal title, if possible, and saves it to our record.
64 function save_terminal_title()
66 local title="$(get_terminal_title)"
67 if [ "$title" != "unknown" ]; then
68 # there was a title, so save it.
69 if [ ! -z "$DEBUG_TERM_TITLE" ]; then
70 echo "saving prior terminal title as '$title'"
72 export PRIOR_TERMINAL_TITLE="$title"
74 # the terminal had no title, or we couldn't access it, or there's no terminal.
75 if [ ! -z "$DEBUG_TERM_TITLE" ]; then
76 echo "not saving prior terminal title which was empty"
81 # using our stored terminal title, this replaces the title on the terminal.
82 function restore_terminal_title()
84 # we don't want to emit anything extra if this is being driven by git.
85 #hmmm... this could be a problem?
86 # if [ -z "$(echo $* | grep git)" ]; then
88 # run the terminal labeller to restore the prior title, if there was one.
89 if [ ! -z "$PRIOR_TERMINAL_TITLE" ]; then
90 if [ ! -z "$DEBUG_TERM_TITLE" ]; then
91 echo "restoring prior terminal title of '$PRIOR_TERMINAL_TITLE'"
93 apply_title_to_terminal "$PRIOR_TERMINAL_TITLE"
97 # sets a title for the terminal with the hostname and other details.
98 function label_terminal_with_info()
100 # we only label the terminal anew if there's no saved title.
101 if [ -z "$PRIOR_TERMINAL_TITLE" ]; then
102 if [ ! -z "$DEBUG_TERM_TITLE" ]; then
103 echo "showing new generated title since prior title was empty"
105 pruned_host=$(echo $HOSTNAME | sed -e 's/^\([^\.]*\)\..*$/\1/')
106 date_string=$(date +"%Y %b %e @ %T")
108 if [ -z "$user" ]; then
109 # try snagging the windoze name.
112 new_title="-- $user@$pruned_host -- [$date_string]"
113 apply_title_to_terminal "$new_title"
116 # use the former title; paste it back up there just in case.
117 if [ ! -z "$DEBUG_TERM_TITLE" ]; then
118 echo "showing prior terminal title since there was a prior title!"
119 echo "using prior terminal title of '$PRIOR_TERMINAL_TITLE'"
121 apply_title_to_terminal "$PRIOR_TERMINAL_TITLE"