Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Here's what I'm currently rocking:

    0 0:00:00
    ~/Code/company/app$
First number is exit code of previous command, second is how long it took. Credit to https://unix.stackexchange.com/questions/252229/ps1-prompt-t... for the last part. It's a bit ratchet:

- I actually don't care about the exit code unless it was non-zero

- It calculates the delta three times

but I've been to busy to optimise it.

    prompt_command() {
      _PS1_now=$(printf '%(%s)T')
      PS1=$( printf "\n\$? \[\e%02d:%02d:%02d \n\[\033[01;34m\]\w\[\033[00m\]\$ " \
              $((  ( _PS1_now - _PS1_lastcmd ) / 3600))         \
              $(( (( _PS1_now - _PS1_lastcmd ) % 3600) / 60 )) \
              $((  ( _PS1_now - _PS1_lastcmd ) % 60))           \
          )
      _PS1_lastcmd=$_PS1_now
    }
    PROMPT_COMMAND='prompt_command'
    _PS1_lastcmd=$(printf '%(%s)T')


Here's what I'm currently rolling:

  $
A colorful prompt appears, only when 'user or hostname or path' changes, or after certain commands:

  - username@hostname:pwd
  $
Somewhere in .bash_aliases ...

  ### PROMPT ###

  ### long prompt (in color) only when 'user or hostname or path' changes, or after certain commands
  prompt_command () 
  { 
    local -a a;
    local last_cmd;
    IFS=' ' read -r -d '' -a a < <( history 1 );
    last_cmd="$(printf "%s" "${a[1]}"|xargs -0)";
    if [ "$USER_HOSTNAME_PWD" != "$USER@$HOSTNAME:$PWD" ] || [[ " cd src doc ssh sudo su login $(sed -e '/^#.*$/d' -e '/^$/d' -e 's@^.*/@@' /etc/shells|uniq|xargs) " == *" ${last_cmd} "* ]]; then
        USER_HOSTNAME_PWD="$USER@$HOSTNAME:$PWD";
        echo -e "\\e7\\e(B\\e[m- \\e[3$((($(id -u) != 0) + 1))m${USER}\\e(B\\e[m@\\e[35m${HOSTNAME%%.*}\\e(B\\e[m:\\e[36m${PWD/#$HOME/\~}\\e8";
    fi
  }
  -() { unset USER_HOSTNAME_PWD
  }
  PROMPT_COMMAND=prompt_command
  PS1='$ ';[ "$(id -u)" -eq 0 ] && PS1='# '




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: