diff options
| author | Syndamia <kamen@syndamia.com> | 2024-11-03 14:56:56 +0200 |
|---|---|---|
| committer | Syndamia <kamen@syndamia.com> | 2024-11-03 14:56:56 +0200 |
| commit | 5a6c0ac91eac9d5184e9f16e26c15d5027375362 (patch) | |
| tree | 0981423b0263c05718817e01bda3c1a34c3cf780 /.bashrc | |
| parent | c4ee2f878f954b01f667f2033e5747bafabefffb (diff) | |
| download | dotfiles-5a6c0ac91eac9d5184e9f16e26c15d5027375362.tar dotfiles-5a6c0ac91eac9d5184e9f16e26c15d5027375362.tar.gz dotfiles-5a6c0ac91eac9d5184e9f16e26c15d5027375362.zip | |
feat(bash): Simplified and improved slab logic
Diffstat (limited to '.bashrc')
| -rw-r--r-- | .bashrc | 54 |
1 files changed, 30 insertions, 24 deletions
@@ -17,17 +17,13 @@ printf "\001\e[39m\e[49m\002" } - ___first="n" # $1 cap colour [OPTIONAL] # $2 cap character [OPTIONAL] # # Entering "o" for $1 and $2 or leaving them unset uses the defaults left_cap() { - defleft="$PS_LEFT_SEMI" - [ -z "$___first" ] && { defleft=" $PS_TRIANGLE█"; ___first="n"; } - [ "$1" = 'o' -o -z "$1" ] && set "$PS_USER_BG" "$2" - [ "$2" = 'o' -o -z "$2" ] && set "$1" "$defleft" + [ "$2" = 'o' -o -z "$2" ] && set "$1" "$PS_LEFT_SEMI" colors_prt "$1" "$PS_BG" printf "$2" @@ -52,36 +48,43 @@ hc_mode && PS_TRIANGLE='' || PS_TRIANGLE='/' hc_mode && PS_TRIANGLE_BOT='' || PS_TRIANGLE='\' hc_mode && PS_LEFT_SEMI='' || PS_LEFT_SEMI='(' + hc_mode && PS_LEFT_SLAB='█' || PS_LEFT_SLAB='/ ' + hc_mode && PS_RIGHT_SLAB='█' || PS_RIGHT_SLAB=' /' hc_mode && PS_RIGHT_SEMI='' || PS_RIGHT_SEMI=')' hc_mode && PS_PREC='' || PS_PREC='>' ___dirchanged="" - ___slab="" - ps_condition() { + ps_commands() { has="" for i in $@ do case $i in + (git) + [ -n "$___git_printed" ] && has="y" && break ;; (ex) [ "$EXSTAT" != '0' ] && has="y" && break ;; (dur) - [ -n "$___timer" -a "$seconds" -gt 10 ] && has="y" && break ;; + [ -z "$___timer" -a "$seconds" -gt 10 ] && has="y" && break ;; (dir) [ -n "$___dirchanged" ] && has="y" && break ;; esac done - return $([ -n $has ]; echo $?) - } - left_slab() { - ps_condition $@ && echo "█" + [ -n "$has" ] } right_slab() { - ps_condition $@ && echo "█" + ps_commands $@ && echo $PS_RIGHT_SLAB + } + + ps_space() { + ps_commands git ex dur dir && printf ' ' + } + ps_nl() { + ps_commands git ex dur dir && printf '\n' } ps_git() { - if [ -n "$___dirchanged" ] && git rev-parse --is-inside-work-tree >/dev/null 2>&1 + if git rev-parse --is-inside-work-tree >/dev/null 2>&1 then branch_name=$(git symbolic-ref -q HEAD) branch_name=${branch_name##refs/heads/} @@ -89,17 +92,20 @@ ___changes="$(git status --porcelain)" - left_cap $PS_USER_BG - printf "$branch_name" - - right_cap $PS_USER_BG "$(right_slab ex dur dir)" + if [ -n "$___dirchanged" ] + then + left_cap $PS_USER_BG $PS_LEFT_SLAB + printf "$branch_name" + right_cap $PS_USER_BG "$(right_slab ex dur dir)" + ___git_printed="y" + fi fi } ps_exit() { if [ "$EXSTAT" != '0' ] then - left_cap $PS_ERR "$(left_slab ex)" + left_cap $PS_ERR $PS_LEFT_SLAB printf "$EXSTAT" right_cap $PS_ERR "$(right_slab dur dir)" @@ -111,7 +117,7 @@ if [ "$seconds" -gt 10 ] then - left_cap $PS_INFO "$(left_slab ex dur)" + left_cap $PS_INFO $PS_LEFT_SLAB if [ "$seconds" -lt 60 ]; then printf '%ds' "$seconds" elif [ "$seconds" -le 3600 ]; then printf '%(%Mm %Ss)T' "$seconds" else printf '%($Hh %Mm $%Ss)T' "$seconds" @@ -129,7 +135,7 @@ if [ -n "$DIR" ] then - left_cap $DIRCOLOR "$(left_slab ex dur dir)" + left_cap $DIRCOLOR $PS_LEFT_SLAB printf "$DIR" right_cap $DIRCOLOR fi @@ -154,7 +160,7 @@ } ps_precursor() { - if [ -n "$___changes" ] + if [ -n "$___git_printed" ] then printf "" else @@ -164,11 +170,11 @@ trap "SECONDS=0;___timer=''" SIGUSR2 - export PS_COMM="ps_git; ps_exit; ps_duration; ps_dir" + export PS_COMM="ps_space; ps_git; ps_exit; ps_duration; ps_dir; ps_nl" export PS1="$(left_cap)\$(ps_user)$(right_cap) \$(ps_precursor) " export PS0=" $(left_cap 'o' "$PS_TRIANGLE_BOT") \A$(right_cap)\n\$(kill -s SIGUSR2 $$)" - export PROMPT_COMMAND='EXSTAT="$?";seconds="$SECONDS";___first="";___slab="";'$PS_COMM'; ___timer="n"; [ -n "$___first" ] && printf "\n"' + export PROMPT_COMMAND='EXSTAT="$?";seconds="$SECONDS";___changes="";___git_printed="";'$PS_COMM'; ___timer="n"' # Honorable mention to: https://lists.gnu.org/archive/html/help-bash/2022-02/msg00023.html # }}} |
