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
+73 -50
View File
@@ -1,51 +1,44 @@
#!/bin/bash
# shellcheck disable=SC2139
# Global bashrc
# TODO: needs some refactoring
function __miaou_prompt_command {
local EXIT="$?" # This needs to be first
# If not running interactively, don't do anything
[[ $- != *i* ]] && return
local R='\[\e[0m\]'
local Gre='\[\e[32m\]'
local Yel='\[\e[93m\]'
local Cya='\[\e[36m\]'
local Mag='\[\e[95m\]'
local Gra='\[\e[90m\]'
## Functions
function __miaou_prompt_container {
if [[ -v CONTAINER_TYPE && -v CONTAINER_HOST ]]; then
[[ -v CONTAINER_PURPOSE ]] || CONTAINER_PURPOSE='development'
local bg_purpose fg_purpose
case "$CONTAINER_PURPOSE" in
prod | prd | production)
# red
bg_purpose=41
fg_purpose=31
;;
beta | sta | stg | staging)
# yellow
bg_purpose=43
fg_purpose=33
;;
*)
# 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
}
function __miaou_prompt_git {
if command -v git >/dev/null 2>&1; then
# git command exists
local DIVERGENT_GIT_SYMBOL="${Yel}➾${R}"
local SYNCHRONIZED_GIT_SYMBOL="${Gre}✔${R}"
local PUSHED_NEEDED_SYMBOL='⥱'
local TAG_NEEDED_SYMBOL='⤽'
local previous_IFS="$IFS"
# set xterm title
echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"
PS1=''
IFS=' '
if [[ -v HOST_TYPE && -v HOST_LOCATION ]]; then
PS1+="\[\e[100;37m\] ${HOST_TYPE} \[\e[44;90m\]\[\e[44;30m\] ${HOST_LOCATION} \[\e[49;34m\]${R} "
fi
if [[ "$UID" -eq 0 ]]; then
PS1+="$Yel\u$R"
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
if git rev-parse --git-dir >/dev/null 2>&1; then
# current dir is version controlled
@@ -81,24 +74,54 @@ function __miaou_prompt_command {
PS1+="${R}]"
fi
fi
}
if [ $EXIT != 0 ]; then
PS1+="$Mag" # Add red if exit code non 0
else
PS1+="$Gra"
fi
function __miaou_prompt_login {
[[ "$UID" -eq 0 ]] && PS1+="$Yel\u$R" || PS1+="$Gre\u$R"
PS1+="${Gra}@${R}${HOSTNAME}"
}
PS1+=" $PROMPT ${R}"
IFS="$previous_IFS"
function __miaou_prompt_location {
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
###------
# 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
if [[ ! -v MIAOU_BASH_DIR ]]; then
source /etc/profile.d/zz-miaou-bash.sh