Skip to content

Commit

Permalink
Simplified color coding and some function logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Rexhepi-Lindberg committed Feb 8, 2023
1 parent c4591f7 commit 16113f6
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions .bash_prompt
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
#!/bin/bash

prompt_git() {
local s=''
local branchName=''

# return if `git` is not available
if ! command -v git >/dev/null 2>&1; then
return
fi

# Check if the current directory is in a Git repository.
if [ "$(git rev-parse --is-inside-work-tree &>/dev/null; echo "${?}")" == '0' ]; then

Expand Down Expand Up @@ -43,7 +50,7 @@ prompt_git() {

[ -n "${s}" ] && s=" [${s}]"

echo -e "${1}${branchName}\e[1;34m${s}"
echo -e "${PINK}${branchName}${PURPLE_2}${s}"
else
return
fi
Expand All @@ -52,31 +59,39 @@ prompt_git() {
prompt_kubectl() {
local kubectl_context

if command -v kubectl >/dev/null 2>&1; then
if kubectl_context=$(kubectl config current-context 2> /dev/null); then
echo -e "${1}${kubectl_context}${2}"
fi
else
# return if `kubectl` is not available
if ! command -v kubectl >/dev/null 2>&1; then
return
fi

if kubectl_context=$(kubectl config current-context 2> /dev/null); then
echo -e "${WHITE}(${BLUE}${kubectl_context}${WHITE})${CLEAR} "
fi
}

PURPLE="$(tput setaf 105)" # username
ORANGE="$(tput setaf 214)" # hostname
WHITE="$(tput setaf 15)" # prepositions
MAGENTA="$(tput setaf 36)" # path
PINK=$(tput setaf 219) # git branch
PURPLE_2="$(tput setaf 183)" # git status
BLUE="$(tput setaf 33)" # kubectl context
CLEAR="$(tput sgr0)" # unset color

# Set the terminal title to the current working directory.
PS1="\\[\\033]0;\\w\\007\\]";
PS1+="\\[\033[38;5;33m\]\\u"; # username
PS1+="\\[\\e[1;37m\\] at ";
PS1+="\\[\033[38;5;37m\\]\\h"; # host
PS1+="\\[\\e[1;37m\\] in ";
PS1+="\\[\e[1;32m\\]\\w"; # working directory
PS1+="\$(prompt_kubectl \"\e[1;37m (\033[38;5;33m\]\" \"\e[1;37m\])\")" # kubectl context
PS1+="\$(prompt_git \"\e[1;37m on \e[1;35m\")"; # Git repository details
PS1+="\\n";
PS1+="\\[\\e[1;37m\\]\$ \\[\\e[0m\\]"; # `$` (and reset color)
PS1="${PURPLE}\u${CLEAR} "; # username
PS1+="${WHITE}at${CLEAR} ";
PS1+="${ORANGE}\h${CLEAR} "; # host
PS1+="${WHITE}in${CLEAR} ";
PS1+="${MAGENTA}\w${CLEAR} "; # working directory
PS1+="\$(prompt_kubectl)${CLEAR}" # kubectl context
PS1+="\$(prompt_git)${CLEAR}"; # git repository details
PS1+="\n";
PS1+="${WHITE}\$ ${CLEAR}"; # `$` (and reset color)

if [ "$TERM" = "dumb" ]; then
export PS1="> "
else
export PS1;
PS2="\\[\\e[1;33m\\]→ \\[\\e[0m\\]";
export PS2;
fi

0 comments on commit 16113f6

Please sign in to comment.