refactored bash.bashrc
This commit is contained in:
@@ -1 +1,3 @@
|
|||||||
|
# shellcheck project directives
|
||||||
disable=SC1090,SC1091
|
disable=SC1090,SC1091
|
||||||
|
|
||||||
|
|||||||
+73
-50
@@ -1,51 +1,44 @@
|
|||||||
#!/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\]'
|
|
||||||
|
|
||||||
|
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 DIVERGENT_GIT_SYMBOL="${Yel}➾${R}"
|
||||||
local SYNCHRONIZED_GIT_SYMBOL="${Gre}✔${R}"
|
local SYNCHRONIZED_GIT_SYMBOL="${Gre}✔${R}"
|
||||||
local PUSHED_NEEDED_SYMBOL='⥱'
|
local PUSHED_NEEDED_SYMBOL='⥱'
|
||||||
local TAG_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
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user