Difference between revisions of "Talk:Bash/Prompt customization"

From ArchWiki
Jump to: navigation, search
(fixing the bugs in the return value visualization)
 
(Examples: new section)
Line 24: Line 24:
 
which displays prompts following successful commands in dark red, and prompts
 
which displays prompts following successful commands in dark red, and prompts
 
following failing commands in bright green with the exit code shown.
 
following failing commands in bright green with the exit code shown.
 +
 +
== Examples ==
 +
 +
This article cries out for examples.  It is tough to tell if one would want to try any of these until one sees an example of what the outcome might be, should it be tried.  I'll bet a lot of people are a little hesitant to try them. - [[User:KitchM|KitchM]] 13:40, 12 April 2010 (EDT)

Revision as of 17:40, 12 April 2010

The return value visualization stuff didn't quite work right for me. The command history prints out funny (bits of old command prefixes stay displayed), and also long lines behaved oddly (wrapped back to start etc.).

I think the trouble maybe coming from echo'ing out color commands with one echo then putting out the characters to reset the font elsewhere. I asked on the bash list and ended up doing this:

   # Make the prompt dark red when the previous command has succeeded, or
   # bright green when it has failed (in keeping with the slightly backwards
   # way exit codes work :).
   red=$(tput setaf 1)
   bright_green=$(tput bold; tput setaf 2)
   reset=$(tput sgr0)
   # Namespace'ed last user command exit status.  We need this I think because
   # the tput commands in the subshells end up resetting $?.
   PROMPT_COMMAND='MYPROMPT_LUCES=$?'   
   colors=("$red" "$bright_green")
   PS1='\[${colors[$? != 0]}\]$(if [ $MYPROMPT_LUCES != 0 ]; then \
                                    echo "$MYPROMPT_LUCES "; \
                                fi)\$\[$reset\] '

which displays prompts following successful commands in dark red, and prompts following failing commands in bright green with the exit code shown.

Examples

This article cries out for examples. It is tough to tell if one would want to try any of these until one sees an example of what the outcome might be, should it be tried. I'll bet a lot of people are a little hesitant to try them. - KitchM 13:40, 12 April 2010 (EDT)