From 72ac2dc6d9832023a269baed0349928411681cc2 Mon Sep 17 00:00:00 2001 From: Kévin Le Gouguec Date: Sat, 4 Oct 2025 22:12:04 +0200 Subject: Season bash to taste MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [ COND ] → test COND if COND then LIST fi → COND && LIST echo -en TEXT → printf $'TEXT' for … → IFS=c eval 'VAR="${ARRAY[*]}" [ A -op B ] → ((A OP B)) --- .bash_prompt | 142 ++++++++++++++++++++++++++--------------------------------- 1 file changed, 63 insertions(+), 79 deletions(-) (limited to '.bash_prompt') diff --git a/.bash_prompt b/.bash_prompt index e3c9a87..b00fb8f 100644 --- a/.bash_prompt +++ b/.bash_prompt @@ -12,7 +12,7 @@ __show-hostname () { case ${PS1_SHOWHOSTNAME} in (''|auto) - [ "${SSH_CONNECTION}" ];; + test "${SSH_CONNECTION}";; (yes) true;; (*) @@ -22,16 +22,13 @@ __show-hostname () __have-gitprompt () { - [ -f /usr/lib/git-core/git-sh-prompt ] + test -f /usr/lib/git-core/git-sh-prompt } __set-title () { local title=${USER} - if __show-hostname - then - title+="@${HOSTNAME}" - fi + __show-hostname && title+=@${HOSTNAME} title+=: title+=${PWD/~/\~} @@ -41,30 +38,26 @@ __set-title () # # ST is a "string terminator", either "ESC \" or "BEL". - echo -ne "\E]2;${title}\a" + printf $'\E]2;%s\a' "${title}" } -# In order to know how much space a prompt takes, Bash needs us to -# delimit non-printing characters with \[ and \]. +# Bash relies on \[ \] to distinguish non-printing characters in prompts. __start-nonprinting () { - if [ ${BUILDING_PS} ] - then - echo -en '\[' - fi + test ${BUILDING_PS} && printf '\[' } __end-nonprinting () { - if [ ${BUILDING_PS} ] - then - echo -en '\]' - fi + test ${BUILDING_PS} && printf '\]' } __fontify () { - local -A codes=( + local -r text=$1 + shift + + local -Ar codes=( [bold]=1 [dim]=2 [reverse]=7 @@ -73,91 +66,88 @@ __fontify () [blue]=34 ) - local text=$1 - shift - - __start-nonprinting - - echo -en '\E[' - - local i - for ((i=1; i<=$#; i++)) + local -a params=() + local c + for c do - echo -en ${codes[${!i}]} - - if ((i<$#)) - then - echo -en ';' - else - echo -en 'm' - fi + params+=(${codes[${c}]}) done + local params_seq + IFS=';' eval 'params_seq="${params[*]}"' + + __start-nonprinting + printf $'\E[%sm' "${params_seq}" __end-nonprinting - echo -en "${text}" + printf %s "${text}" __start-nonprinting - echo -en '\E[0m' + printf $'\E[0m' __end-nonprinting } __current-column () { - # Cf. console_codes(4) § ECMA-48 Status Report Commands + # Cf. console_codes(4) § ECMA-48 Status Report Commands. local position read -sdR -p$'\E[6n' position - local pattern='\[[0-9]+;([0-9]+)' + local -r pattern='\[[0-9]+;([0-9]+)' - if [[ ${position} =~ ${pattern} ]] - then - echo ${BASH_REMATCH[1]} - else + [[ ${position} =~ ${pattern} ]] || { echo 0 - fi + return + } + echo ${BASH_REMATCH[1]} } __signal-no-newline () { - echo $(__fontify ∅ bold red) + __fontify ∅ bold red + echo } __draw-rule () { - local i line - - for ((i=$(__current-column); i