colours 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. # The arrays will be 1-based because this is ZSH default behaviour.
  19. # This can be changed with the `KSH_ARRAYS` option, but we do not.
  20. # First 16 are the ANSI Terminal colours and bright variants:
  21. # Black, Red, Green, Yellow, Blue, Magenta, Cyan, White
  22. for colour in {0..255}; do
  23. FG[$colour]=$cStart"38;5;"$colour$cEnd
  24. BG[$colour]=$cStart"48;5;"$colour$cEnd
  25. done
  26. # Create text effects.
  27. cReset=$cStart"0"$cEnd
  28. cBol=$cStart"1"$cEnd # bold
  29. cIta=$cStart"3"$cEnd # italic
  30. cUnd=$cStart"4"$cEnd # underline
  31. cBli=$cStart"5"$cEnd # blink
  32. cRev=$cStart"7"$cEnd # reverse
  33. cnBol=$cStart"22"$cEnd # no-bold
  34. cnIta=$cStart"23"$cEnd # no-italic
  35. cnUnd=$cStart"24"$cEnd # no-underline
  36. cnBli=$cStart"25"$cEnd # no-blink
  37. cnRev=$cStart"27"$cEnd # no-reverse
  38. SPECTRUM_TEXT=${SPECTRUM_TEXT:-Arma virumque cano Troiae qui primus ab oris}
  39. # Show all 256 colours with colour number
  40. function spectrum_ls() {
  41. local line
  42. for code in {0..255}; do
  43. line="$code: ${FG[$code]}$SPECTRUM_TEXT$cReset"
  44. [[ -n $(command -v print) ]] && print -P -- $line || printf "$line\n"
  45. done
  46. }
  47. # Show all 256 colours where the background is set to specific colour
  48. function spectrum_bls() {
  49. local line
  50. for code in {0..255}; do
  51. line="$code: ${BG[$code]}$SPECTRUM_TEXT$cReset"
  52. [[ -n $(command -v print) ]] && print -P -- $line || printf "$line\n"
  53. done
  54. }