Skip to content

Commit

Permalink
Slightly optimised Bash prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
hmlendea committed Mar 26, 2024
1 parent b5967c4 commit 4cfefd8
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions rc/shell_prompt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ENABLE_GIT_STATUS=true
function get_git_prompt {
! ${ENABLE_GIT} && return

BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
local BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)

[ -z "${BRANCH_NAME}" ] && return

Expand Down Expand Up @@ -66,26 +66,26 @@ function get_git_prompt {
}

function get_git_branch_status {
UPSTREAM=${1:-'@{u}'}
REMOTE=$(git rev-parse "$UPSTREAM" 2>/dev/null)
local UPSTREAM=${1:-'@{u}'}
local REMOTE=$(git rev-parse "${UPSTREAM}" 2>/dev/null)

if [ -z ${REMOTE} ]; then
echo "local-only"
return
fi

LOCAL=$(git rev-parse @)
local LOCAL=$(git rev-parse @)

if [ ${LOCAL} = ${REMOTE} ]; then
LOCAL_CHANGES=$(git diff-index --name-only HEAD --)
local LOCAL_CHANGES=$(git diff-index --name-only HEAD --)

if [ -n "${LOCAL_CHANGES}" ]; then
echo "local-changes"
else
echo "up-to-date"
fi
else
BASE=$(git merge-base @ "$UPSTREAM")
local BASE=$(git merge-base @ "${UPSTREAM}")

if [ ${LOCAL} = ${BASE} ]; then
echo "behind"
Expand All @@ -101,7 +101,7 @@ function get_path {
MAX_LENGTH=24
CWD=${PWD##*/}

if [ "$HOME" = "$PWD" ]; then
if [ "${HOME}" = "${PWD}" ]; then
echo "~"
elif [ ${#CWD} -gt $((MAX_LENGTH)) ]; then
echo ${CWD:0:$((MAX_LENGTH/2-1))}..${CWD:(-$((MAX_LENGTH/2-1)))}
Expand Down Expand Up @@ -137,8 +137,10 @@ function set_custom_prompt {
local separator2=":"
local separator3=">"

[ "$(whoami)" = "root" ] && userColour="31" # Root = red
[ "$(whoami)" = "guest" ] && userColour="33" # Guest = yellow
local CURRENT_USERNAME="$(whoami)"

[ "${CURRENT_USERNAME}" = "root" ] && userColour="31" # Root = red
[ "${CURRENT_USERNAME}" = "guest" ] && userColour="33" # Guest = yellow

if [[ ${HOSTNAME} = *Assistant ]]; then
hostColour="94" # HomeAssistant = Light Blue
Expand All @@ -154,8 +156,8 @@ function set_custom_prompt {
hostColour="93" # Phone = Light Yellow
fi

local ps1user="$USER"
local ps1host='$HOSTNAME' # "\h"
local ps1user="${USER}"
local ps1host='${HOSTNAME}' # "\h"
local ps1path='$(get_path)'

if ${ENABLE_COLOURS}; then
Expand Down

0 comments on commit 4cfefd8

Please sign in to comment.