Color shell prompts (was Re: FreeBSD 4.8-RELEASE mutt & color (color))

Joshua Oremzn oremanj at webserver.get-linux.org
Mon Jul 14 10:24:06 PDT 2003


On Mon, Jul 14, 2003 at 12:51:43PM -0400 or thereabouts, Kevin Berrien wrote:
> Speaking of color.  Does anyone have a good tutorial/script to add color 
> in bash as one finds in the newer linux distributions?
> 

"The Bash-Prompt HOWTO" @ tldp.org
is a good one. Just to give you an idea about how crazy this all can
get, take a look at this prompt (for zsh). Note that ^[ means the
real ESC character, not caret + left bracket. Sorry about the long lines:

PS1='%{^[[1;34m%}--[ %{^[[1;36m%}$(pwd), $(ls -1 | wc -l | tr -d " ") files / ${SIZE}%{^[[1;34m%} ]-- --[ %{^[[1;36m%}$(date "+%I:%M %p")%{^[[1;34m%} ]-- --[ %{^[[1;36m%}%n @ %M[$(jobs | wc -l | tr -d " ")] / $(tty | sed "s:/dev/pts/:ttyp:g;s:/dev/::g")%{^[[1;34m%} ]--
--[ %{^[[1;36m%}$(ps ax | wc -l | tr -d " ") processes%{^[[1;34m%} ]-- --[ %{^[[1;36m%}$(who | wc -l | tr -d " ") users%{^[[1;34m%} ]--
--[ %{^[[1;36m%}%# %{^[[1;34m%}]-- %{^[[0m%}'
PS2='%{^[[1;34m%}--[ %{^[[1;36m%}%_ > %{^[[1;34m%}]-- %{^[[0m%}'

function precmd () {
    let TotalBytes=0

    for Bytes in $(command ls -l | grep "^-" | awk '{ print $5 }')
    do
        let TotalBytes=$TotalBytes+$Bytes
    done

    if [ $TotalBytes -lt 1024 ]; then
        TotalSize=$(echo -e "scale=0 \n$TotalBytes \nquit" | bc | tr -d '\n')
        suffix=" bytes"
    elif [ $TotalBytes -lt 1048576 ]; then
        TotalSize=$(echo -e "scale=0 \n$TotalBytes/1024 \nquit" | bc | tr -d '\n')
        suffix="K"
    elif [ $TotalBytes -lt 1073741824 ]; then
        TotalSize=$(echo -e "scale=0 \n$TotalBytes/1048576 \nquit" | bc | tr -d '\n')
        suffix="M"
    else
        TotalSize=$(echo -e "scale=0 \n$TotalBytes/1073741824 \nquit" | bc | tr -d '\n')
        suffix="G"
    fi

    SIZE="${TotalSize}${suffix}"

    UPTIME="`uptime | sed -e 's/.* \(.* days,\)\? \(.*:..,\) .*/\1 \2/' \
                          -e 's/,//g' -e 's/ up //'`"
}

It makes a prompt like this, but in color:
--[ /, 33 files / 10M ]-- --[ 10:23 AM ]-- --[ oremanj @ webserver.get-linux.org[0] / ttyv1 ]--
--[ 136 processes ]-- --[ 4 users ]--
--[ % ]-- <command-goes-here>

-- Josh

P.S. Of course I don't use that prompt!

> 
> _______________________________________________
> freebsd-questions at freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to "freebsd-questions-unsubscribe at freebsd.org"


More information about the freebsd-questions mailing list