Merge branch 'master' of github.com:fredhamster/feisty_meow into dev
[feisty_meow.git] / scripts / tty / terminal_titler.sh
index e486b423c8a924f9018139d2e76c59414f9e8a41..939c1cce14a7b0f3e1078c59cdbbd2fddc82a7f0 100644 (file)
@@ -1,92 +1,99 @@
 #!/bin/bash
 
-source $FEISTY_MEOW_SCRIPTS/core/functions.sh
+source "$FEISTY_MEOW_SCRIPTS/core/functions.sh"
+source "$FEISTY_MEOW_SCRIPTS/core/common.alias"
 
-# just saying this is an array...
-#declare -a PRIOR_TERMINAL_TITLES
+# uncomment this to get extra noisy debugging.
+#export DEBUG_TERM_TITLE=true
 
-# set the stack position if not already set.
-if [ -z "$PTT_STACK_INDEX" ]; then
-  # this variable records where we will push new items on the stack.
-  PTT_STACK_INDEX=0
-fi
-
-# returns okay (0) if the stack is empty, or non-zero if not empty.
-function ptt_stack_empty()
+# puts a specific textual label on the terminal title bar.
+# this doesn't consider any previous titles; it just labels the terminal.
+# the title may not be visible in some window managers.
+function apply_title_to_terminal()
 {
-  if [ -z "$PTT_STACK_INDEX" ]; then
-    # fix the index value.
-    PTT_STACK_INDEX=0
-    true
-  else
-    test $PTT_STACK_INDEX -le 0
+  title="$*"
+  # if we weren't given a title, then use just the hostname.  this is the degraded functionality.
+  if [ -z "${title}" ]; then
+    title="$(hostname)"
   fi
-}
-
-# a debugging function that should never have been necessary.
-# a little bit furious the restore is failing during regenerate right now.
-function show_terminal_titles()
-{
-  echo terminal title list now has:
-  local i=${#PRIOR_TERMINAL_TITLES[@]}
-  if ptt_stack_empty; then
-    echo the list is empty
+  
+  if [ "${TERM}" != "dumb" -a -z "$PBS_ENVIRONMENT" -a \
+        ! -z "$PS1" -a "${TERM}" != "linux" ]; then
+    echo -n -e "\033]0;${title}\007"
   else
-    while ((i--)); do
-      echo "ent #$i: '${PRIOR_TERMINAL_TITLES[$i]}'"
-    done
+    # not running interactively, so just echo the title.
+    sep
+    echo ${title}
+    sep
   fi
 }
 
-# adds an entry into the stack of terminal titles.
-function push_ptt_stack()
+# user friendly version that saves the title being added.
+function set_terminal_title()
 {
-  PRIOR_TERMINAL_TITLES[$PTT_STACK_INDEX]="$*"
-  ((PTT_STACK_INDEX++))
-echo stack index incremented and now at $PTT_STACK_INDEX
-show_terminal_titles
-}
+  apply_title_to_terminal $*
 
-function pop_ptt_stack()
-{
-  if [ $PTT_STACK_INDEX -le 0 ]; then
-    echo nothing to pop from prior terminal titles stack.
-  else
-    ((PTT_STACK_INDEX--))
-echo stack index decremented and now at $PTT_STACK_INDEX
-    CURRENT_TERM_TITLE="${PRIOR_TERMINAL_TITLES[$PTT_STACK_INDEX]}"
-echo "got the last title as '$CURRENT_TERM_TITLE'"
-show_terminal_titles
-  fi
+#tricky attempts to get it to be available when we ask for it in get_terminal_title
+  sync
+#  echo -n
+
+#  # we're enforcing a new title from here on.
+#  unset PRIOR_TERMINAL_TITLE
+  save_terminal_title
 }
 
-# puts a specific textual label on the terminal title bar.
-# this doesn't consider any previous titles; it just labels the terminal.
-# the title may not be visible in some window managers.
-function set_terminal_title()
+# echoes back the current title on the terminal window, if we can acquire it.
+function get_terminal_title()
 {
-  title="$*"
-  # if we weren't given a title, then use just the hostname.  this is the degraded functionality.
-  if [ -z "${title}" ]; then
-    title="$(hostname)"
+#hmmm: totally failing since gnome doesn't provide the info we need like it should; WINDOWID is empty.
+#hmmm: need to revise this somehow to work around that, but nothing is quite right so far.
+#hmmm: had to disable the xwininfo approach because it's super messy (console noise) and still isn't right (just like xdotool approach wasn't right).
+
+  # this is an important value now; it is checked for in save_terminal_title.
+  local term_title_found="unknown"
+  # save the former terminal title if we're running in X with xterm.
+
+  # are we even running a terminal?
+#hmmm: abstract out that condition below to check against a list of names.
+  if [[ "$TERM" =~ .*"xterm".* ]]; then
+    # check that we have a window ID.
+    if [[ ! -z "$WINDOWID" ]]; then
+      # the window id exists in the variable; can we get its info?
+      if [[ ! -z "$(which xprop)" ]]; then
+        # sweet, we have all the info we need to get this done.
+        term_title_found="$(xprop -id $WINDOWID | perl -nle 'print $1 if /^WM_NAME.+= \"(.*)\"$/')"
+      fi
+    else
+      # gnome-terminal doesn't set WINDOWID currently; we can try to work around this.
+      false
+#hmmm: so far, none of these approaches are any good.
+#      # not good solution; gets wrong titles.  plus uses xdotool which is not installed by default in ubuntu.
+#      if [[ ! -z "$(which xdotool)" ]]; then
+#        term_title_found="$(xprop -id $(xdotool getactivewindow) | perl -nle 'print $1 if /^WM_NAME.+= \"(.*)\"$/')"
+#      fi
+#      # this solution also fails by getting the wrong window title if this one isn't focussed.
+#      if [[ ! -z "$(which xwininfo)" ]]; then
+#        term_title_found=$(xwininfo -id $(xprop -root | awk '/NET_ACTIVE_WINDOW/ { print $5; exit }') | awk -F\" '/xwininfo:/ { print $2; exit }')
+#      fi
+    fi
   fi
-  echo -n -e "\033]0;${title}\007"
+  echo -n "$term_title_found"
 }
 
-# reads the current terminal title, if possible, and saves it to our stack of titles.
+# reads the current terminal title, if possible, and saves it to our record.
 function save_terminal_title()
 {
-  # save the former terminal title if we're running in X with xterm.
-  which xprop &>/dev/null
-  if [ $? -eq 0 ]; then
-    if [[ "$TERM" =~ .*"xterm".* ]]; then
-      local prior_title="$(xprop -id $WINDOWID | perl -nle 'print $1 if /^WM_NAME.+= \"(.*)\"$/')"
-      if [ ! -z "$prior_title" ]; then
-echo "saving prior terminal title as '$prior_title'"
-        push_ptt_stack "$prior_title"
-      else
-echo "not saving prior terminal title which was empty"
-      fi
+  local title="$(get_terminal_title)"
+  if [ "$title" != "unknown" ]; then
+    # there was a title, so save it.
+    if [ ! -z "$DEBUG_TERM_TITLE" ]; then
+      echo "saving prior terminal title as '$title'"
+    fi
+    export PRIOR_TERMINAL_TITLE="$title"
+  else
+    # the terminal had no title, or we couldn't access it, or there's no terminal.
+    if [ ! -z "$DEBUG_TERM_TITLE" ]; then
+      echo "not saving prior terminal title which was empty"
     fi
   fi
 }
@@ -99,12 +106,11 @@ function restore_terminal_title()
 #  if [ -z "$(echo $* | grep git)" ]; then
 
   # run the terminal labeller to restore the prior title, if there was one.
-  if ptt_stack_empty; then
-echo prior titles were empty, so doing nothing.
-  else
-    pop_ptt_stack
-echo "restoring prior terminal title of '$CURRENT_TERM_TITLE'"
-    set_terminal_title "$CURRENT_TERM_TITLE"
+  if [ ! -z "$PRIOR_TERMINAL_TITLE" ]; then
+    if [ ! -z "$DEBUG_TERM_TITLE" ]; then
+      echo "restoring prior terminal title of '$PRIOR_TERMINAL_TITLE'"
+    fi
+    apply_title_to_terminal "$PRIOR_TERMINAL_TITLE"
   fi
 }
 
@@ -112,24 +118,27 @@ echo "restoring prior terminal title of '$CURRENT_TERM_TITLE'"
 function label_terminal_with_info()
 {
   # we only label the terminal anew if there's no saved title.
-  if ptt_stack_empty; then
+  if [ -z "$PRIOR_TERMINAL_TITLE" ]; then
+    if [ ! -z "$DEBUG_TERM_TITLE" ]; then
+      echo "showing new generated title since prior title was empty"
+    fi
     pruned_host=$(echo $HOSTNAME | sed -e 's/^\([^\.]*\)\..*$/\1/')
     date_string=$(date +"%Y %b %e @ %T")
-    user=$USER
+    user="$(logname)"
     if [ -z "$user" ]; then
       # try snagging the windoze name.
       user=$USERNAME
     fi
     new_title="-- $user@$pruned_host -- [$date_string]"
-    set_terminal_title "$new_title"
+    apply_title_to_terminal "$new_title"
+    save_terminal_title
   else
-    # restore the former title.
-#no    restore_terminal_title
-echo "showing prior terminal title since there was a prior title!"
-    pop_ptt_stack
-echo "using prior terminal title of '$CURRENT_TERM_TITLE'"
-    set_terminal_title "$CURRENT_TERM_TITLE"
-    push_ptt_stack "$CURRENT_TERM_TITLE"
+    # use the former title; paste it back up there just in case.
+    if [ ! -z "$DEBUG_TERM_TITLE" ]; then
+      echo "showing prior terminal title since there was a prior title!"
+      echo "using prior terminal title of '$PRIOR_TERMINAL_TITLE'"
+    fi
+    apply_title_to_terminal "$PRIOR_TERMINAL_TITLE"
   fi
 }