colours 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # vim: ft=sh
  2. # A script to make using 256 colours less painful.
  3. #
  4. # Adapted from http://github.com/sykora/etc/blob/master/zsh/functions/spectrum/
  5. # P.C. Shyamshankar <sykora@lucentbeing.com>
  6. # Create foreground and background colour arrays for 256 colours.
  7. declare -a FG BG
  8. # Determine prompt colour escape strings.
  9. if [[ -n ${BASH_VERSION-} ]]; then
  10. cStart='\[\e['
  11. cEnd='m\]'
  12. elif [[ -n ${ZSH_VERSION-} ]]; then
  13. # ZSH specifics: A = associative array, H = hide value when echoed, g = global
  14. declare -AHg FG BG
  15. cStart='%{['
  16. cEnd='m%}'
  17. fi
  18. # Build the colour array.
  19. # First 16 are the ANSI Terminal colours and bright variants:
  20. # Black, Red, Green, Yellow, Blue, Magenta, Cyan, White
  21. for colour in {0..255}; do
  22. FG[$colour]=$cStart"38;5;"$colour$cEnd
  23. BG[$colour]=$cStart"48;5;"$colour$cEnd
  24. done
  25. # Create text effects.
  26. cReset=$cStart"0"$cEnd
  27. cBol=$cStart"1"$cEnd # bold
  28. cIta=$cStart"3"$cEnd # italic
  29. cUnd=$cStart"4"$cEnd # underline
  30. cBli=$cStart"5"$cEnd # blink
  31. cRev=$cStart"7"$cEnd # reverse
  32. cnBol=$cStart"22"$cEnd # no-bold
  33. cnIta=$cStart"23"$cEnd # no-italic
  34. cnUnd=$cStart"24"$cEnd # no-underline
  35. cnBli=$cStart"25"$cEnd # no-blink
  36. cnRev=$cStart"27"$cEnd # no-reverse
  37. SPECTRUM_TEXT=${SPECTRUM_TEXT:-Arma virumque cano Troiae qui primus ab oris}
  38. # Show all as foreground colours
  39. function spectrum_ls() {
  40. local line
  41. for code in {0..255}; do
  42. line="$code: ${FG[$code]}$SPECTRUM_TEXT$cReset"
  43. [[ -n $(command -v print) ]] && print -P -- $line || printf "$line\n"
  44. done
  45. }
  46. # Show all as background colours
  47. function spectrum_bls() {
  48. local line
  49. for code in {0..255}; do
  50. line="$code: ${BG[$code]}$SPECTRUM_TEXT$cReset"
  51. [[ -n $(command -v print) ]] && print -P -- $line || printf "$line\n"
  52. done
  53. }