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 echo "empty title: pushing current title again"
45 if [ -z "$title" ]; then
46 echo "there was no saved title, so we're ignoring the save attempt."
51 #hmmm: need a validation step here to remove bad chars that conflict with our title compression scheme.
53 # only slap a comma after the existing value if it wasn't empty.
54 if [ -z "$TERMINAL_TITLE_STACK" ]; then
55 export TERMINAL_TITLE_STACK="\"$title\""
57 export TERMINAL_TITLE_STACK="$TERMINAL_TITLE_STACK,\"$title\""
60 echo new terminal title stack is:
61 echo $TERMINAL_TITLE_STACK
64 # takes a terminal title off the stack and sets the LAST_TITLE variable.
65 function pop_title_stack()
67 # whack our output variable, just in case.
71 # trim the popped item out of the stack.
72 if [ ! -z "$TERMINAL_TITLE_STACK" ]; then
73 TERMINAL_TITLE_STACK="$(echo $TERMINAL_TITLE_STACK | sed -n -e 's/\(.*\),[^,]*$/\1/p')"
77 # like pop, but does not change the stack, effectively handing you the most
79 function peek_title_stack()
81 # whack our output variable, just in case.
84 if [ ! -z "$TERMINAL_TITLE_STACK" ]; then
85 LAST_TITLE="$(echo $TERMINAL_TITLE_STACK | sed -n -e 's/.*","\([^,]*\)"$/\1/p')"
86 if [ -z "$LAST_TITLE" ]; then
87 LAST_TITLE="$(echo $TERMINAL_TITLE_STACK | sed -n -e 's/"//gp')"
92 # using our stored terminal title, this replaces the title on the terminal.
93 function restore_terminal_title()
95 # run the terminal labeller to restore the prior title, if there was one.
98 if [ ! -z "$LAST_TITLE" ]; then
99 if [ ! -z "$DEBUG_TERM_TITLE" ]; then
100 echo "restoring prior terminal title of '$LAST_TITLE'"
101 echo "while new title stack is: $TERMINAL_TITLE_STACK"
103 apply_title_to_terminal "$LAST_TITLE"
107 # sets a title for the terminal with the hostname and other details.
108 function label_terminal_with_info()
110 # we only label the terminal anew if there's no saved title.
111 if [ -z "$TERMINAL_TITLE_STACK" ]; then
112 if [ ! -z "$DEBUG_TERM_TITLE" ]; then
113 echo "showing new generated title since prior title was empty"
115 pruned_host=$(echo $HOSTNAME | sed -e 's/^\([^\.]*\)\..*$/\1/')
116 date_string=$(date +"%Y %b %e @ %T")
118 if [ -z "$user" ]; then
119 # try snagging the windoze name.
122 new_title="-- $user@$pruned_host -- [$date_string]"
123 apply_title_to_terminal "$new_title"
124 save_terminal_title "$new_title"
126 # use the former title; paste it back up there just in case.
128 apply_title_to_terminal "$LAST_TITLE"
134 # user friendly version sets the terminal title and saves the title being added.
135 function set_terminal_title()
137 apply_title_to_terminal $*
138 save_terminal_title $*