From 20705a758fa96120033581a7e8a7b93ae8073bfd Mon Sep 17 00:00:00 2001 From: pvincent Date: Sat, 12 Sep 2026 11:48:44 +0400 Subject: [PATCH] refactored bash.bashrc --- .shellcheckrc | 2 + etc/bash.bashrc | 125 ++++++++++++++++++++++++++++-------------------- 2 files changed, 76 insertions(+), 51 deletions(-) diff --git a/.shellcheckrc b/.shellcheckrc index 0459386..55cb14e 100644 --- a/.shellcheckrc +++ b/.shellcheckrc @@ -1 +1,3 @@ +# shellcheck project directives disable=SC1090,SC1091 + diff --git a/etc/bash.bashrc b/etc/bash.bashrc index abe1fe8..b76f4ec 100644 --- a/etc/bash.bashrc +++ b/etc/bash.bashrc @@ -1,50 +1,43 @@ -#!/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 - 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} " +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 +} - 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 +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='⤽' 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