git-aware-prompt icon indicating copy to clipboard operation
git-aware-prompt copied to clipboard

Use printf instead of echo in colors.sh

Open lethosor opened this issue 10 years ago • 7 comments

echo (both the built-in command and /bin/echo) doesn't recognize the "\e" escape sequence on OS X. (This may not be an issue, since $TERM is typically only unset in non-interactive environments, so these could theoretically also be replaced with an empty echo or printf.)

lethosor avatar Aug 10 '15 20:08 lethosor

This fixed my prompt on OS X. Before it was printing "(master)\e[0m$" from the txtrst escape sequence.

davidsnyder avatar Nov 30 '18 19:11 davidsnyder

Was master printing in color?

lethosor avatar Nov 30 '18 21:11 lethosor

Yes: https://imgur.com/a/99n9jkG (using echo)

Here it is with this patch: https://imgur.com/a/PEhL91H

davidsnyder avatar Nov 30 '18 22:11 davidsnyder

Weird, I'm guessing your tput doesn't recognize sgr 0 somehow. What version of OS X are you using?

Changing sgr 0 to sgr0 might also work.

lethosor avatar Nov 30 '18 22:11 lethosor

Darwin Kernel Version 17.7.0

Like I said, switching to printf fixes the issue.

davidsnyder avatar Nov 30 '18 22:11 davidsnyder

Right, but the only reason echo or printf are called is because tput fails. So in this case, it's because "tput sgr 0" fails. That's weird, though, because I'm using Darwin 17.6.0 (aka macOS 10.13.5) and it appears to work fine for me. Maybe it has something to do with your terminal, then.

At any rate, this patch would be good to merge for portability reasons in cases like yours.

lethosor avatar Nov 30 '18 22:11 lethosor

Ah you're right. I wasn't thinking about the logic. Are you sure that your tput sgr 0 2 isn't exiting 1? You might be hitting the echo/printf as I did.

davidsnyder@loki tictactoe-react (master)*$ tput sgr 0 2
davidsnyder@loki tictactoe-react (master)*$ echo $?
1

tput sgr0 seems to be the correct command? I read that sgr 0 0 is equivalent to sgr0 but sgr 0 0 exits 1 for some reason.

txtrst="$(tput sgr0 2>/dev/null || printf '\e[0m')" # Text Reset works for me.

davidsnyder avatar Dec 01 '18 16:12 davidsnyder