You are not logged in.

#1 2010-01-27 18:36:41

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,965
Website

Some terminal color functions, including directory-based prompt colors

All of this code is taken from my ~/.bashrc. Note that some variables are outside of functions. It should be easy to adapt everything below to your own preferences.


This is my PS1 prompt. Note how the color variables are used.

COLOR_1="\[\e[1;37m\]"
COLOR_2="\[\e[0;37m\]"
COLOR_3="\[\e[1;34m\]"
COLOR_4="\[\e[1;30m\]"
function set_ps1()
{
  PS1="\n${COLOR_3}┌─[${COLOR_2}\u${COLOR_3}@${COLOR_4}\h ${COLOR_2}\w${COLOR_3}]\n${COLOR_3}└─> ${COLOR_2}"
}
set_ps1

A while ago I wrote this function to change the color of "decorations" in the prompt. It works by changing one of the color variables. If invoked with a number it will set the corresponding color, otherwise it will choose a random color from the available palette.

_palette=$(tput colors)
function color()
{
  NUMBER=$1
  if [ -z "$NUMBER" ]; then
    NUMBER=$[ ( $RANDOM % $_palette )  + 1 ]
    echo "number $NUMBER"
  fi
  COLOR_3="\[\e[0;38;5;${NUMBER}m\]"
  set_ps1
}

Recently, when I was playing with colors, I also wrote this function to display the 256-color palette so I could easily choose a color to pass to the function above. If you pass it an argument, it will display the palette in a wide format.

function colors()
{
  for NUMBER in $(seq 0 15); do
    printf "\e[0;38;5;${NUMBER}m%4d" $NUMBER
  done
  echo ""
  
  if [ -z "$1" ]; then
  
    for _I in $(seq 0 5); do
      for _J in $(seq 0 5); do
        for _K in $(seq 0 5); do
          NUMBER=$((16 + $_I + 6 * $_J + 36 * $_K ))
          printf "\e[0;38;5;${NUMBER}m%4d" $NUMBER
        done
        echo -n " "
        for _K in $(seq 0 5); do
          NUMBER=$((16 + 36 * $_I + $_J + 6 * $_K))
          printf "\e[0;38;5;${NUMBER}m%4d" $NUMBER
        done
        echo -n " "
        for _K in $(seq 0 5); do
          NUMBER=$((16 + 6 * $_I + 36 * $_J + $_K))
          printf "\e[0;38;5;${NUMBER}m%4d" $NUMBER
        done
        echo ""
      done
      echo ""
    done
  
  else
  
    for _I in $(seq 0 5); do
    for _J in $(seq 0 5); do
    for _K in $(seq 0 5); do
      NUMBER=$((16 + 6 * $_I + $_J + 36 * $_K))
      printf "\e[0;38;5;${NUMBER}m%4d" $NUMBER
      #echo -n " $_I$_J$_K"
    done
    echo -n " "
    done
    echo ""
    done
    echo ""
  
    for _I in $(seq 0 5); do
    for _J in $(seq 0 5); do
    for _K in $(seq 0 5); do
      NUMBER=$((16 + $_I + 36 * $_J + 6 * $_K))
      printf "\e[0;38;5;${NUMBER}m%4d" $NUMBER
      #echo -n " $_I$_J$_K"
    done
    echo -n " "
    done
    echo ""
    done
    echo ""
  
    for _I in $(seq 0 5); do
    for _J in $(seq 0 5); do
    for _K in $(seq 0 5); do
      NUMBER=$((16 + 36 * $_I + 6 * $_J + $_K))
      printf "\e[0;38;5;${NUMBER}m%4d" $NUMBER
      #echo -n " $_I$_J$_K"
    done
    echo -n " "
    done
    echo ""
    done
    echo ""
  
  fi
  
  for NUMBER in $(seq 232 255); do
    printf "\e[0;38;5;${NUMBER}m%4d" $NUMBER
  done
}

Even more recently, I've been using certain colors to provide visual cues about directory location that let me quickly scan open terminals to find the one I want, so I've written this function, which will change the PS1 prompt based on directory. Just add in cases as you need them and change the colors as you like (the colors below are just an example). You can use it synonymously with "cd".

function ccd()
{
  cd $@
  _dir=$(pwd)/
  case $_dir in
    ~/projects/*)
      color 202
    ;;
    /tmp/*)
      color 49
    ;;
    /usr/*)
      color 190
    ;;
    *)
      color 12
    ;;
  esac
}

Use this thread to post your own terminal color functions or variations of others that you've found here, along with feedback, discussion, etc.


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#2 2010-01-27 18:43:31

rson451
Member
From: Annapolis, MD USA
Registered: 2007-04-15
Posts: 1,233
Website

Re: Some terminal color functions, including directory-based prompt colors

Thanks for the colors() function.  The rest won't be useful to me, but this one will make it much easier to modify vim colorschemes.


archlinux - please read this and this — twice — then ask questions.
--
http://rsontech.net | http://github.com/rson

Offline

#3 2010-01-27 19:09:03

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,965
Website

Re: Some terminal color functions, including directory-based prompt colors

I expect this to become something of a bargain bin for terminal color functions. A bit of rummaging might turn up a few gems, but most of it will be worthless crap to most. tongue


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

Board footer

Powered by FluxBB