Set the Xterm title from the bash prompt.
authorSteve Youngs <steve@steveyoungs.com>
Sun, 18 Apr 2021 01:13:34 +0000 (11:13 +1000)
committerSteve Youngs <steve@steveyoungs.com>
Sun, 18 Apr 2021 01:13:34 +0000 (11:13 +1000)
* etc/pkgusr/bash_profile (_settitle): New, to set the Xterm title
in the prompt.
(make_prompt): Use it.

Signed-off-by: Steve Youngs <steve@steveyoungs.com>
etc/pkgusr/bash_profile

index 0123438..d9c82b4 100644 (file)
@@ -82,8 +82,8 @@ export SUPPRESSLOCALEDIR CHECKUPDATES SLACKPKG
 #  inside a git repo display the branch and other pertinent info, if
 #  the previous command had a non-zero exit display the code, display
 #  the current time, and current working directory (truncated to
-#  prevent "prompt-creep").  Colourised so that often you won't even
-#  need to read it to take in the info.
+#  prevent "prompt-creep"), and set the Xterm title.  Colourised so
+#  that often you won't even need to read it to take in the info.
 
 # Colours (doesn't include black (30))
 CO="\[\e[0m\]"        # Colour Off
@@ -110,14 +110,25 @@ _sg()
     [[ $(id -gn) != $(id -un) ]] && echo "$WTE:$CYN$(id -gn)"
 }
 
+# Set the Xterm title
+_settitle() 
+{
+    [[ "$TERM" = "xterm" ]] && echo "\[\e]2;PKG(\u)\a\]"
+}
+
 # This function, called from $PROMPT_COMMAND, puts all the pieces of
 # the prompt together.
 make_prompt()
 {
     rc=$?                      # Return code from previous cmd
-    [[ $rc -gt 0 ]] && prc="$BWTE[$BRED$rc$BWTE] " || prc=
 
-    local sg gitp p
+    # Normally you'd define your local vars at the top of the function,
+    # well I normally do, but in this case it will overwrite the value
+    # of '$?' so we need to preserve it first.
+    local gitp p prc sg title
+
+    # Previous cmd's return code, but only if it was non-zero
+    [[ $rc -gt 0 ]] && prc="$BWTE[$BRED$rc$BWTE] " || prc=
 
     # Using a subgroup
     sg=$(_sg)
@@ -127,11 +138,15 @@ make_prompt()
 
     p="pkgusr"
 
+    # Xterm title
+    title=$(_settitle)
+
     # Build up the PS1
     PS1="$BWTE[$WTE$p $BWTE($CYN\u$sg$BWTE)]"  # [pkgusr (usr:grp)]
     PS1+="$CO"                                 # reset to not mess up git prompt
     PS1+="$gitp"                               # (gitstuff)
     PS1+="\n$prc$BYEL\A$WTE:$BMAG\w$BWTE>$CO " # [rc]TIME:CWD>
+    PS1+="$title"                             # Xterm title
 }
 
 # The git goodies