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
11 # puts a specific textual label on the terminal title bar.
12 # this doesn't consider any previous titles; it just labels the terminal.
13 # the title may not be visible in some window managers.
14 function apply_title_to_terminal()
17 # if we weren't given a title, then use just the hostname. this is the degraded functionality.
18 if [ -z "${title}" ]; then
22 if [ "${TERM}" != "dumb" -a -z "$PBS_ENVIRONMENT" -a \
23 ! -z "$PS1" -a "${TERM}" != "linux" ]; then
24 echo -n -e "\033]0;${title}\007"
26 # not running interactively, so just echo the title.
35 # records the current terminal title, pushing it down on the stack of titles,
36 # possibly prior to a new one being used.
37 function save_terminal_title()
41 if [ -z "$title" ]; then
42 if [ ! -z "$DEBUG_TERM_TITLE" ]; then
43 echo "terminal_titler: empty title: pushing current title again"
47 if [ -z "$title" ]; then
48 log_feisty_meow_event "terminal_titler: there was no saved title, so we're ignoring the save attempt."
53 #hmmm: need a validation step here to remove bad chars that conflict with our title compression scheme.
55 # only slap a comma after the existing value if it wasn't empty.
56 if [ -z "$TERMINAL_TITLE_STACK" ]; then
57 export TERMINAL_TITLE_STACK="\"$title\""
59 export TERMINAL_TITLE_STACK="$TERMINAL_TITLE_STACK,\"$title\""
62 if [ ! -z "$DEBUG_TERM_TITLE" ]; then
63 echo "terminal_titler: new terminal title stack is:"
64 echo "$TERMINAL_TITLE_STACK"
68 # takes a terminal title off the stack and sets the LAST_TITLE variable.
69 function pop_title_stack()
71 # whack our output variable, just in case.
75 # trim the popped item out of the stack.
76 if [ ! -z "$TERMINAL_TITLE_STACK" ]; then
77 TERMINAL_TITLE_STACK="$(echo $TERMINAL_TITLE_STACK | sed -n -e 's/\(.*\),[^,]*$/\1/p')"
81 # like pop, but does not change the stack, effectively handing you the most
83 function peek_title_stack()
85 # whack our output variable, just in case.
88 if [ ! -z "$TERMINAL_TITLE_STACK" ]; then
89 LAST_TITLE="$(echo $TERMINAL_TITLE_STACK | sed -n -e 's/.*","\([^,]*\)"$/\1/p')"
90 if [ -z "$LAST_TITLE" ]; then
91 LAST_TITLE="$(echo $TERMINAL_TITLE_STACK | sed -n -e 's/"//gp')"
96 # using our stored terminal title, this replaces the title on the terminal.
97 function restore_terminal_title()
99 # run the terminal labeller to restore the prior title, if there was one.
102 if [ ! -z "$LAST_TITLE" ]; then
103 if [ ! -z "$DEBUG_TERM_TITLE" ]; then
104 echo "terminal_titler: restoring prior terminal title of '$LAST_TITLE'"
105 echo "terminal_titler: while new title stack is: $TERMINAL_TITLE_STACK"
107 apply_title_to_terminal "$LAST_TITLE"
111 # sets a title for the terminal with the hostname and other details.
112 function label_terminal_with_info()
114 # we only label the terminal anew if there's no saved title.
115 if [ -z "$TERMINAL_TITLE_STACK" ]; then
116 if [ ! -z "$DEBUG_TERM_TITLE" ]; then
117 echo "terminal_titler: showing new generated title since prior title was empty"
119 pruned_host=$(echo $HOSTNAME | sed -e 's/^\([^\.]*\)\..*$/\1/')
120 date_string=$(date +"%Y %b %e @ %T")
121 user="$(fm_username)"
122 if [ -z "$user" ]; then
123 # try snagging the windoze name.
126 new_title="-- $user@$pruned_host -- [$date_string]"
127 apply_title_to_terminal "$new_title"
128 save_terminal_title "$new_title"
130 # use the former title; paste it back up there just in case.
132 apply_title_to_terminal "$LAST_TITLE"
138 # user friendly version sets the terminal title and saves the title being added.
139 function set_terminal_title()
141 apply_title_to_terminal $*
142 save_terminal_title $*