Allow running multiple PROMPT_COMMAND snippets.
authorIain Patterson <me@iain.cx>
Mon, 22 Oct 2018 12:27:56 +0000 (13:27 +0100)
committerIain Patterson <me@iain.cx>
Sat, 8 May 2021 08:47:22 +0000 (10:47 +0200)
Files matching the  .prompt.d/*.prompt glob will be sourced before printing
the prompt.

.profile.d/prompt.bashrc
.prompt.d/title.prompt [new file with mode: 0644]

index da94e97..e67a8fd 100644 (file)
@@ -1,48 +1,13 @@
-# Set the title of an xterm to the hostname unless the remote IP is matched by 
-# a regular expression in ~/.ssh/forwarded.
-SSH_FORWARDED=$(get_remote_ip)
-if [ ! -z "$SSH_FORWARDED" ]; then
-  if [ "$SSH_FORWARDED" = "localhost" ]; then
-    SSH_FORWARDED=
-  elif [ -f ${PROFILE_HOME:-~}/.ssh/forwarded ]; then
-    for forwarded in $(< ${PROFILE_HOME:-~}/.ssh/forwarded); do
-      if echo "$SSH_FORWARDED" | grep "$forwarded" &>/dev/null; then
-        SSH_FORWARDED=
-      fi
-    done
-    unset forwarded
-  fi
-  [ ! -z "$SSH_FORWARDED" ] && SSH_FORWARDED=" (from $(get_remote_ip -n))"
-fi
-
-# Unscientific check for grid jobs.
-case ${SSH_CLIENT##* } in
-  ""|22|2222);;
-  *) SSH_FORWARDED="$SSH_FORWARDED [grid]"
-esac
-
-info="$HOSTNAME$SSH_FORWARDED"
-
-# Check for vim.
-vimchild=" [vim]"
-if ps -o comm= -p $PPID 2>/dev/null | grep '^vim*$' &>/dev/null; then
-  # Clear [vim] when exiting.
-  trap "echo -ne '\\033]0;$info\\033\\0134'" exit
-  info="$info$vimchild"
-fi
-if [ "${TERM##screen}" = "$TERM" -o "${TERM%%-bce}" = "$TERM" ]; then
-  PROMPT_COMMAND="echo -ne '\033]0;$info\033\0134\r\033[K'"
-else
-  PROMPT_COMMAND='echo -ne "\033k\033\0134"'
-fi
-unset blank info vimchild SSH_FORWARDED
+_prompt_command_escape=1
 
 # Actual dtterm barfs on escape sequences.
 if [ "$TERMINAL_EMULATOR" = "dtterm" ]; then
-  unset PROMPT_COMMAND
+  unset _prompt_command_escape
 fi
 
 # As does screen on an ancient Solaris host.
 if [ -n "$OLDSOLARIS" -a ! "${TERM##screen}" = "$TERM" ]; then
-  unset PROMPT_COMMAND
+  unset _prompt_command_escape
 fi
+
+PROMPT_COMMAND='for snippet in ${PROFILE_HOME:-~}/.prompt.d/*.prompt; do . $snippet; done; unset snippet'
diff --git a/.prompt.d/title.prompt b/.prompt.d/title.prompt
new file mode 100644 (file)
index 0000000..beadd34
--- /dev/null
@@ -0,0 +1,40 @@
+if [ -n "$_prompt_command_escape" ]; then
+  # Set the title of an xterm to the hostname unless the remote IP is matched by 
+  # a regular expression in ~/.ssh/forwarded.
+  SSH_FORWARDED=$(get_remote_ip)
+  if [ ! -z "$SSH_FORWARDED" ]; then
+    if [ "$SSH_FORWARDED" = "localhost" ]; then
+      SSH_FORWARDED=
+    elif [ -f ${PROFILE_HOME:-~}/.ssh/forwarded ]; then
+      for forwarded in $(< ${PROFILE_HOME:-~}/.ssh/forwarded); do
+        if echo "$SSH_FORWARDED" | grep "$forwarded" &>/dev/null; then
+          SSH_FORWARDED=
+        fi
+      done
+      unset forwarded
+    fi
+    [ ! -z "$SSH_FORWARDED" ] && SSH_FORWARDED=" (from $(get_remote_ip -n))"
+  fi
+
+  # Unscientific check for grid jobs.
+  case ${SSH_CLIENT##* } in
+    ""|22|2222);;
+    *) SSH_FORWARDED="$SSH_FORWARDED [grid]"
+  esac
+
+  info="$HOSTNAME$SSH_FORWARDED"
+
+  # Check for vim.
+  vimchild=" [vim]"
+  if ps -o comm= -p $PPID 2>/dev/null | grep '^vim*$' &>/dev/null; then
+    # Clear [vim] when exiting.
+    trap "echo -ne '\\033]0;$info\\033\\0134'" exit
+    info="$info$vimchild"
+  fi
+  if [ "${TERM##screen}" = "$TERM" -o "${TERM%%-bce}" = "$TERM" ]; then
+    echo -ne '\033]0;'"$info"'\033\0134\r\033[K'
+  else
+    echo -ne "\033k\033\0134"
+  fi
+  unset blank info vimchild SSH_FORWARDED
+fi