Tweak the bash prompt.
authorSteve Youngs <steve@steveyoungs.com>
Sat, 24 Apr 2021 12:06:15 +0000 (22:06 +1000)
committerSteve Youngs <steve@steveyoungs.com>
Sat, 24 Apr 2021 12:06:15 +0000 (22:06 +1000)
* etc/pkgusr/bash_profile (_hline): New.
(make_prompt): Use it.
Re-organise the prompt a little.

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

index a75d6ce..3a64b47 100644 (file)
@@ -116,6 +116,24 @@ _settitle()
     [[ "$TERM" = "xterm" ]] && echo "\[\e]2;PKG(\u)\a\]"
 }
 
+# Draw a horizontal line from the end of the top line of the prompt to
+# the right hand edge of the terminal.
+_hline()
+{
+    # 11 is the number of chars taken up by "-(HH:MM)-()"
+    TRIM=$((${#PWD} + 11))
+
+    # If there was a non-zero return there will be an extra 4 + length
+    # of rc chars
+    [[ $rc -gt 0 ]] && TRIM=$(($TRIM + ${#rc} + 4))
+
+    # Allow for $HOME being displayed as ~
+    [[ $HOME == ${PWD::${#HOME}} ]] && TRIM=$(($TRIM - ${#HOME} + 1))
+
+    a=($(seq -s ' ' 1 $(($COLUMNS - $TRIM))))
+    printf '%s' ${a[@]/*/-}
+}
+
 # This function, called from $PROMPT_COMMAND, puts all the pieces of
 # the prompt together.
 make_prompt()
@@ -125,10 +143,10 @@ make_prompt()
     # 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
+    local gitp p prc sg title hline
 
     # Previous cmd's return code, but only if it was non-zero
-    [[ $rc -gt 0 ]] && prc="$BWTE[$BRED$rc$BWTE] " || prc=
+    [[ $rc -gt 0 ]] && prc="$BWTE--[$BRED$rc$BWTE]" || prc=
 
     # Using a subgroup
     sg=$(_sg)
@@ -141,12 +159,17 @@ make_prompt()
     # Xterm title
     title=$(_settitle)
 
+    # Horizontal line
+    hline=$(_hline)
+
     # 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
+    PS1="$BWTE-($BYEL\A$BWTE)-($BMAG\w$BWTE)$prc" # -(TIME)-(CWD)--[rc]
+    PS1+="$BBLU$hline\n"
+    PS1+="$BWTE[$YEL$p $BWTE($CYN\u$sg$BWTE)"   # [pkgusr (usr:grp)
+    PS1+="$CO"                                  # reset to not mess up git prompt
+    PS1+="$gitp"                                # (gitstuff)
+    PS1+="$BWTE]>$CO "                          # ]>
+    PS1+="$title"                              # Xterm title
 }
 
 # The git goodies
@@ -158,9 +181,9 @@ GIT_PS1_SHOWUPSTREAM=auto
 GIT_PS1_DESCRIBE_STYLE=branch
 GIT_PS1_SHOWCOLORHINTS=true
 
-# So the prompt doesn't fly off the right-hand edge of the screen
-# when you're down a long directory tree.
-PROMPT_DIRTRIM=4
+# Set this so the prompt doesn't fly off the right-hand edge of the
+# screen when you're down a long directory tree.
+#PROMPT_DIRTRIM=4
 
 # Finally, make the prompt happen!
 PROMPT_COMMAND=make_prompt