0

I'm attempting to customise my bash prompt, so that I get this:

enter image description here

i.e. user-text is in bold, but stdout is not.

It was working, but after upgrading my mac it's glitching.

Machine A: MacBook Pro 2017 running Ventura. Machine B: MacBook Pro M2 running Sonoma (Pre-Release).

My bash PS1 prompt (define in ~/.bash_profile) is defined thus:

function rgb {
    # https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
    let "sum = 16 + 36*$1 + 6*$2 + $3"
    # echo "\[\e[${sum}m\]"
    tput setaf ${sum}
}

  BOLD=$(tput bold)
   DIM=$(tput dim)
 RESET=$(tput sgr0)

BRIGHT=$(rgb 5 5 5)
  BLUE=$(rgb 1 1 5)
YELLOW=$(rgb 4 4 1)

PS1="\n"
PS1+="🧢 \[${DIM}\]"
PS1+="\u"
PS1+="${BRIGHT}@"
PS1+="\[${BRIGHT}\]"
PS1+="\h "
PS1+="\[${BLUE}\]"
PS1+="\w "
PS1+="\n"
PS1+="\[${BRIGHT}\]"
PS1+="> "
PS1+="\[${BOLD}\]"

Machine A it works:

A works

Machine B, not quite:

B fails

And I can't figure out why.

On a given machine there's consistency between Terminal.app, iTerm2 and VSCode Integrated Terminal.

So I went back to basics, and discovered that on both machines, this works correctly:

> echo -e "\033[32mThis should be green\033[0m"

... while this fails (comes out in white):

> tput setaf 2; echo "This should be green"

B-not-green

I'm surprised to observe this behaviour on machine A, after so many years without noticing any trouble.

So, I'm facing two puzzles:

  • why is my prompt failing on machine B
  • why is that tput failing on both machines?

Should I just be avoiding tput here? I'm guessing I only did it because it looks cleaner than escape sequences.

1
  • 1
    What is $TERM set to on both machines? tput will interrogate terminfo according to the setting of $TERM to get the codes it needs to send, while sending escape codes ignores terminfo and just hopes it works. On the failing machine, try tput setaf 2 | od -h -t c, and see what tput is actually sending. Commented Sep 6, 2023 at 21:32

1 Answer 1

0

In the end I had to build upwards from scratch.

I won't try to explain this code; GP4 will do a better job!

There was a ${DIM} that was somehow getting RESET on the old system but not the new system. I don't get it.

All I can say is: now it works!

function rgb {
    let "sum = 16 + 36*$1 + 6*$2 + $3"
    echo -n "\[\e[38;5;${sum}m\]"
}

BOLD=$(tput bold)
DIM=$(tput dim)
RESET_RAW='\e[0m'
RESET='\[\e[0m\]'

BRIGHT=$(rgb 4 4 4)
BLUE=$(rgb 1 1 5)
YELLOW=$(rgb 4 4 1)

git_branch() {
  git rev-parse --abbrev-ref HEAD 2>/dev/null
}

PS1="\n"
PS1+="$(rgb 2 2 2)\u"
PS1+="$(rgb 1 1 1)@"
PS1+="$(rgb 2 3 2)\h "            # `sudo scutil --set HostName foo` to set hostname (\h)`
PS1+="${BLUE}\w "
PS1+="${YELLOW}\$(git_branch)"
PS1+="\n"
PS1+="$(rgb 3 3 3)> "
PS1+="${BRIGHT}${BOLD}"

PS2="${BOLD}> ${RESET}"

trap 'echo -ne "${RESET_RAW}" > $(tty)' DEBUG
1
  • You should accept the answer to indicate that the problem has been solved.
    – Barmar
    Commented Oct 7, 2023 at 22:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .