Skip to content

Instantly share code, notes, and snippets.

@chipbite
Created November 8, 2024 12:16
Show Gist options
  • Save chipbite/f51d62653dc8e790b3b6416af7ecc5b7 to your computer and use it in GitHub Desktop.
Save chipbite/f51d62653dc8e790b3b6416af7ecc5b7 to your computer and use it in GitHub Desktop.
How I almost added RHS info to git-bash, in /.config/git/git-prompt.sh
# ----------------------------------------------------------------
# prompt config for (git-)bash
# this is called from /etc/profile.d/git-prompt.sh
# (if this file exists, which it apparently does, since you are reading it...)
# src: https://github.com/git-for-windows/build-extra/blob/main/git-extra/git-prompt.sh
#
# why this file? /etc/profile.d/git-prompt.sh is overwritten with git (for windows) is updated.
# starting point (default setup for the prompt) can be found in /etc/profile.d/git-prompt.sh
#
# Config info: https://gist.github.com/adojos/fd30baa716dbfbe282fe147ab717b664
# edits:
# terminal window, commented newline and out all of "line 2", just keeping prompt character
# ----------------------------------------------------------------
# src: https://stackoverflow.com/a/43729966/3701019
#
# Right hand side of prompt
#
# Select git info displayed, see /usr/lib/git-core/git-sh-prompt for more
# export GIT_PS1_SHOWCOLORHINTS=1 # Make pretty colours inside $PS1
# export GIT_PS1_SHOWDIRTYSTATE=1 # '*'=unstaged, '+'=staged
# export GIT_PS1_SHOWSTASHSTATE=1 # '$'=stashed
# export GIT_PS1_SHOWUNTRACKEDFILES=1 # '%'=untracked
# export GIT_PS1_SHOWUPSTREAM="verbose" # 'u='=no difference, 'u+1'=ahead by 1 commit
# export GIT_PS1_STATESEPARATOR='' # No space between branch and index status
# export GIT_PS1_DESCRIBE_STYLE="describe" # Detached HEAD style:
# describe relative to older annotated tag (v1.6.3.1-13-gdd42c2f)
# contains relative to newer annotated tag (v1.6.3.2~35)
# branch relative to newer tag or branch (master~4)
# default exactly eatching tag
# Sets prompt like:
# ravi@boxy:~/prj/sample_app[exit]$ master*% u= | 30 Apr 22:27
function _set_bash_prompt() {
# Set left hand side of the prompt
# PS1="\u@\h:\w\$ "
#
# Git status
#
# Save current state of user shopt settings promptvars and extglob
local user_shopt
user_shopt=$(shopt -p promptvars extglob)
# __git_ps1 usually returns literal text "${__git_ps1_branch_name}" rather
# than the contained branch name, eg "master". This prevents calculating
# the length of the printable characers in the RHS string (used to move the
# cursor that many columns left from the terminal's right edge.) However if
# "shopt promptvars" is unset, __git_ps1 it will include the dereferenced
# branch name instead.
shopt -qu promptvars
# extglob is required for the ${variable//@(pattern)/} replacements
shopt -qs extglob
# Allow disabling git status and no error if __git_ps1 undefined
if [[ ! -v _disable_git_prompt && $(type -t __git_ps1 2>/dev/null) == function ]]; then
# __git_ps1 will only make pretty colours inside $PS1
local old_PS1=$PS1
__git_ps1 "" "" "%s" # force colour; no default round bracket (decorations)
# Strip "\[" and "\[": non-printable character markers. __git_ps1 outputs
# them however the whole of the RHS prompt needs to be included in these
# markers, and they can't be nested.
git=${PS1//@(\\@(\[|\]))/}
PS1=$old_PS1
fi
#
# Right hand side of prompt
#
local rhs="" # String to be printed on the right hand side of terminal
# Create a string like: "25 Apr 13:15"
local date_time
printf -v date_time "%(%e %b %H:%M)T" -1 # -1 is current time
# Format the RHS prompt
[[ -n $git ]] && rhs="$git | " #"
rhs+="\e[0;1;31m${date_time}"
# Strip ANSI CSI commands (eg colours) to enble counting the length of
# printable characters, giving offset of cursor from terminal RHS edge (from
# https://www.commandlinefu.com/commands/view/12043/remove-color-special-escape-ansi-codes-from-text-with-sed)
# Neither bash not sed support lookbehind zero-length assertions, so it's not
# possible to ignore "\\e", (ie a literal '\' followed by a literal 'e'), yet
# still remove "\e" (ie ESC)
local rhs_printable=${rhs//@(\\@(\[|]|[Ee]\[*([0-9;])[a-zA-Z]))/}
# or, in using sed (but requires exec):
# local rhs_printable=$(sed -e 's,\\[][]\|\\[Ee]\[\([0-9;]\)*[A-Za-z],,g' <<< "$rhs")
# Reference: https://en.wikipedia.org/wiki/ANSI_escape_code
local Save='\e[s' # Save cursor position
local Rest='\e[u' # Restore cursor to save point
# Save cursor position, jump to (right hand edge minus N columns) where N is
# the length of the printable RHS string. Print the RHS string, then return
# to the saved position and print the LHS prompt.
# Note: "\[" and "\]" are used so that bash can calculate the number of
# printed characters so that the prompt doesn't do strange things when
# command line editing/browsing/completion. Ensure that these are not nested.
PS1="\[\e[0m${Save}\e[$((COLUMNS - ${#rhs_printable}))G${rhs}${Rest}\]${PS1}"
eval "$user_shopt"
}
export -f _set_bash_prompt
# eval "$_options"; unset _options # Restore previous shell options from line 2
# ----------------------------------------------------------------
# This section is based on git-prompt.sh:
#
PS1='\[\033]0;${TITLEPREFIX}\W `__git_ps1` [ \D{%H:%M - %a %d %b} ]\007\]' # set window title
# PS1="$PS1"'\n' # new line
# PS1="$PS1"'\[\033[32m\]' # change to green
# PS1="$PS1"'\u@\h ' # user@host<space>
# PS1="$PS1"'\[\033[35m\]' # change to purple
# PS1="$PS1"'$MSYSTEM ' # show MSYSTEM
# PS1="$PS1"'\[\033[33m\]' # change to brownish yellow
# PS1="$PS1"'\w' # current working directory
if test -z "$WINELOADERNOEXEC" # this cannot be commented out, since this part (1) sets up git cli and (2) prompt.
then
GIT_EXEC_PATH="$(git --exec-path 2>/dev/null)"
COMPLETION_PATH="${GIT_EXEC_PATH%/libexec/git-core}"
COMPLETION_PATH="${COMPLETION_PATH%/lib/git-core}"
COMPLETION_PATH="$COMPLETION_PATH/share/git/completion"
if test -f "$COMPLETION_PATH/git-prompt.sh"
then
. "$COMPLETION_PATH/git-completion.bash"
. "$COMPLETION_PATH/git-prompt.sh"
# PS1="$PS1"'\[\033[36m\]' # change color to cyan
# PS1="$PS1"'`__git_ps1`' # bash function
fi
fi
# PS1="$PS1"'\n' # new line
PS1="$PS1"'\[\033[32m\]' # change to green
PS1="$PS1"'$ ' # prompt: always $
PS1="$PS1"'\[\033[0m\]' # change color
# _set_bash_prompt
# ----------------------------------------------------------------
@chipbite
Copy link
Author

chipbite commented Nov 8, 2024

This version comments out the actual call (line 140), and so does nothing.

The edit I made to the proposed script was line 37 here, commenting out PS1, since that should already exist and have been built.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment