[CentralOH] 2014-09-29 會議 Scribbles 落書/惡文?: Prompt Colorized According to Success or Failure of Previous Command

jep200404 at columbus.rr.com jep200404 at columbus.rr.com
Thu Oct 9 20:45:41 CEST 2014


Jason, where's the code for your prompt?

On Thu, 9 Oct 2014 12:54:14 -0400, Ann Elliott <elliott.222 at osu.edu> wrote:

> X-Mailer: Apple Mail (2.1878.6)

> PS1='...’
          ^
          |
          |
          +---- ick! Is that '’' an Apple Mail artifact?

> PS1='\[\033[`if [ $? -gt 0 ]; then echo 31; else echo 32; fi`m\]\h:\w \u\$ \[\033[00m\]’

Just for Pythonic giggles, refactoring reductio ad absurdum: 

PS1='\[\033[`echo "print 32 if $? == 0 else 31" | python`m\]\h:\w \u\$ \[\033[00m\]'

or (say it with meaning)

ANSI_RED=31
ANSI_GREEN=32
PS1='\[\033[`echo "print $ANSI_GREEN if $? == 0 else $ANSI_RED" | python`m\]\h:\w \u\$ \[\033[00m\]'

or (say it with _more_ meaning)

ANSI_RED=31
ANSI_GREEN=32
SUCCESS_COLOR=$ANSI_GREEN
FAILURE_COLOR=$ANSI_RED
PS1='\[\033[`echo "print $SUCCESS_COLOR if $? == 0 else $FAILURE_COLOR" | python`m\]\h:\w \u\$ \[\033[00m\]'

or (now for the ad absurdum part)

ANSI_RED='\033[31m'
ANSI_GREEN='\033[32m'
SUCCESS_COLOR=$ANSI_GREEN
FAILURE_COLOR=$ANSI_RED
PS1='\[`echo "print '\''$SUCCESS_COLOR'\'' if $? == 0 else '\''$FAILURE_COLOR'\''" | python`\]\h:\w \u\$ \[\033[00m\]'


More information about the CentralOH mailing list