jimeh / git-aware-prompt

Display current Git branch name in your terminal prompt when in a Git working directory.
Creative Commons Zero v1.0 Universal
2.16k stars 340 forks source link

Use printf instead of echo in colors.sh #23

Open lethosor opened 9 years ago

lethosor commented 9 years ago

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.)

davidsnyder commented 5 years ago

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

lethosor commented 5 years ago

Was master printing in color?

davidsnyder commented 5 years ago

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

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

lethosor commented 5 years ago

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.

davidsnyder commented 5 years ago

Darwin Kernel Version 17.7.0

Like I said, switching to printf fixes the issue.

lethosor commented 5 years ago

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.

davidsnyder commented 5 years ago

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.