view bin/termcolors.sh @ 145:c3ca45204c4e

Documentation.
author Edho Arief <edho@myconan.net>
date Sun, 02 Oct 2011 04:28:49 +0700
parents 1783152a1fe6
children 73e90355bbd7
line wrap: on
line source

#!/bin/sh

# Usage:
#   _c [bold_|]<color> <text>
#
#   <color> can be one of:
#     black, blue, green, cyan, red, purple, brown, gray
#   Prefix the color with bold_ to use bold style/light color.
#
# Example:
#   To print word "Hello" in bold blue:
#     _c bold_blue "Hello"
#
#   To print text with only one word colored:
#     echo "Hello, $(c green world)."
#
# To do:
#   Support for background color, underline, etc.
#

_c() {
  color_name="${1}"
  shift
  text="${*}"
  esq="\033["
  r="${esq}0m" # r for reset
  case "${color_name}" in
    bold_*) color="1;";;
    *) color="0;";;
  esac
  case "${color_name}" in
    *black) color="${color}30";;
    *blue) color="${color}34";;
    *green) color="${color}32";;
    *cyan) color="${color}36";;
    *red) color="${color}31";;
    *purple) color="${color}35";;
    *brown) color="${color}33";;
    *gray) color="${color}37";;
  esac
  color="${esq}${color}m"
  printf "${color}%s${r}" "${text}"
}