Hacker News new | ask | show | jobs
by Tyr42 4973 days ago
One option that is missing that I really thing is useful is

    wincolour=$yellow
    errorcolour=$red
    # To provide a coloured version of $?, wrapped in ().  Used in PS1
    pretty_exit_code(){
      LAST_COMMAND_STATUS=$?
      if [ $LAST_COMMAND_STATUS = 0 ];
      then echo -e "$wincolour($LAST_COMMAND_STATUS)$black";
      else echo -e "$errorcolour($LAST_COMMAND_STATUS)$black";
      fi
    }
Which just provides the exit code of the last command, but coloured differently for failure or success.
2 comments

Interesting. I've been using $PIPESTATUS at the start of my prompt for a while now.
I use this, which allows a similar effect but is enabled for PIPESTATUS, and also does some good things for eterm and screen.

   PIPESTATUS_REGEX="^ +$"

   # check to see if any of the pipe statuses don't start with 0
   # ${_CMD_PIPESTATUS[@]#0} results in null if they all do
   _PIPESTATUS="
       _CMD_PIPESTATUS=(\${PIPESTATUS[@]})
       if [[ \"\${_CMD_PIPESTATUS[*]#0}\" && ! \"\${_CMD_PIPESTATUS[*]#0}\" =~ \$PIPESTATUS_REGEX ]]
       then
           _RES_STR=\" [\${_CMD_PIPESTATUS[*]}]\";
       else
           _RES_STR=''
       fi"

    export PROMPT_COMMAND="$_PIPESTATUS; $PROMPT_COMMAND"

    if [[ $TERM = screen || $_TERM = screen ]]; then
        _RESET_PROMPT="\[\033k\033\\\\\]"
    else
        _RESET_PROMPT=""
    fi

    if [ $TERM = "eterm-color" ]; then
        # XXX: this is going to break things when you start
        # use $INSIDE_EMACS to test whether we should use \u?
        _ETERM_PROMPT="\[\033AnSiTu \u\012\033AnSiTc \w\012\033AnSiTh \H\012\]"
    else
        _ETERM_PROMPT=""
    fi

    export PS1="\n$_ETERM_PROMPT\[\033[32m\]\u@\h \[\033[33m\]\w\[\033[35m\]\$_RES_STR\[\033[0m\]\n${_RESET_PROMPT}\\$ "
    unset _RESET_PROMPT
It's cool to also see the name of the signal that caused exit status > 128. Sadly this only fits in a tweet if it can assume the possible signal names are listed contiguously, which isn't so on 64-bit Linux these days.

  S=(`trap -l`);e(){ for N in ${PIPESTATUS[@]};do ((N>128))&&echo ${S[$((2*N-257))]}||echo $N;done;};trap $'echo "\e[41m Exit "`e`" \e[m"' ERR
That's a great idea. I've rewritten my own version to use these signals instead of numbers where possible.
Just added exit status to the page. Thanks for the suggestion.