refactored bash.bashrc

This commit is contained in:
pvincent
2026-09-12 11:48:44 +04:00
parent a6aaaa1b92
commit 20705a758f
2 changed files with 76 additions and 51 deletions
+2
View File
@@ -1 +1,3 @@
# shellcheck project directives
disable=SC1090,SC1091 disable=SC1090,SC1091
+74 -51
View File
@@ -1,50 +1,43 @@
#!/bin/bash # Global bashrc
# shellcheck disable=SC2139
# TODO: needs some refactoring # If not running interactively, don't do anything
function __miaou_prompt_command { [[ $- != *i* ]] && return
local EXIT="$?" # This needs to be first
local R='\[\e[0m\]' ## Functions
local Gre='\[\e[32m\]'
local Yel='\[\e[93m\]'
local Cya='\[\e[36m\]'
local Mag='\[\e[95m\]'
local Gra='\[\e[90m\]'
local DIVERGENT_GIT_SYMBOL="${Yel}➾${R}" function __miaou_prompt_container {
local SYNCHRONIZED_GIT_SYMBOL="${Gre}✔${R}" if [[ -v CONTAINER_TYPE && -v CONTAINER_HOST ]]; then
local PUSHED_NEEDED_SYMBOL='⥱' [[ -v CONTAINER_PURPOSE ]] || CONTAINER_PURPOSE='development'
local TAG_NEEDED_SYMBOL='⤽' local bg_purpose fg_purpose
case "$CONTAINER_PURPOSE" in
local previous_IFS="$IFS" prod | prd | production)
# red
# set xterm title bg_purpose=41
echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007" fg_purpose=31
;;
PS1='' beta | sta | stg | staging)
IFS=' ' # yellow
bg_purpose=43
if [[ -v HOST_TYPE && -v HOST_LOCATION ]]; then fg_purpose=33
PS1+="\[\e[100;37m\] ${HOST_TYPE} \[\e[44;90m\]\[\e[44;30m\] ${HOST_LOCATION} \[\e[49;34m\]${R} " ;;
*)
# green
bg_purpose=42
fg_purpose=32
;;
esac
# ▶ alternative
PS1+="\[\e[100;37m\] ${CONTAINER_TYPE} \[\e[${bg_purpose};90m\]\[\e[30m\] ${CONTAINER_HOST} \[\e[49;${fg_purpose}m\]${R} "
fi fi
}
if [[ "$UID" -eq 0 ]]; then function __miaou_prompt_git {
PS1+="$Yel\u$R" if command -v git >/dev/null 2>&1; then
PROMPT='$'
else
PS1+="$Gre\u$R"
PROMPT='#'
fi
PS1+="${Gra}@${R}${HOSTNAME}"
local pwd='~'
[ "$PWD" != "$HOME" ] && pwd=${PWD/#$HOME\//\~\/}
PS1+=" ${Cya}${pwd}${R}"
if hash git 2>&-; then
# git command exists # git command exists
local DIVERGENT_GIT_SYMBOL="${Yel}➾${R}"
local SYNCHRONIZED_GIT_SYMBOL="${Gre}✔${R}"
local PUSHED_NEEDED_SYMBOL='⥱'
local TAG_NEEDED_SYMBOL='⤽'
if git rev-parse --git-dir >/dev/null 2>&1; then if git rev-parse --git-dir >/dev/null 2>&1; then
# current dir is version controlled # current dir is version controlled
@@ -81,24 +74,54 @@ function __miaou_prompt_command {
PS1+="${R}]" PS1+="${R}]"
fi fi
fi fi
}
if [ $EXIT != 0 ]; then function __miaou_prompt_login {
PS1+="$Mag" # Add red if exit code non 0 [[ "$UID" -eq 0 ]] && PS1+="$Yel\u$R" || PS1+="$Gre\u$R"
else PS1+="${Gra}@${R}${HOSTNAME}"
PS1+="$Gra" }
fi
PS1+=" $PROMPT ${R}" function __miaou_prompt_location {
IFS="$previous_IFS" local pwd='~'
[ "$PWD" != "$HOME" ] && pwd=${PWD/#$HOME\//\~\/}
xterm_title="${USER}@${HOSTNAME}: ${pwd}"
PS1+=" ${Cya}${pwd}${R}"
}
function __miaou_prompt_sign {
[[ $exit_code != 0 ]] && PS1+="$Mag" || PS1+="$Gra"
[[ "$UID" -eq 0 ]] && PS1+=' $ ' || PS1+=' # '
PS1+="${R}"
}
function set_xterm_title {
echo -ne "\033]0;$1\007"
}
function __miaou_prompt_command {
local exit_code="$?" # This needs to be first
local R='\[\e[0m\]'
local Gre='\[\e[32m\]'
local Yel='\[\e[93m\]'
local Cya='\[\e[36m\]'
local Mag='\[\e[95m\]'
local Gra='\[\e[90m\]'
local xterm_title=''
PS1=''
__miaou_prompt_container
__miaou_prompt_login
__miaou_prompt_location
__miaou_prompt_git
__miaou_prompt_sign
set_xterm_title "${xterm_title}"
} }
###------ ###------
### MAIN ### MAIN
###------ ###------
# If not running interactively, don't do anything
[[ $- != *i* ]] && return
# TODO: use /etc/environment for MIAOU_BASH_DIR and leave /etc/profile to reset PROMPT_AFTER after tilix/cve # TODO: use /etc/environment for MIAOU_BASH_DIR and leave /etc/profile to reset PROMPT_AFTER after tilix/cve
if [[ ! -v MIAOU_BASH_DIR ]]; then if [[ ! -v MIAOU_BASH_DIR ]]; then
source /etc/profile.d/zz-miaou-bash.sh source /etc/profile.d/zz-miaou-bash.sh