prompt-colors.sh 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/env bash
  2. # prompt-colors.sh
  3. #
  4. # source this file to get color definitions
  5. # are also printed to STDERR.
  6. define_color_names() {
  7. ColorNames=( Black Red Green Yellow Blue Magenta Cyan White )
  8. FgColors=( 30 31 32 33 34 35 36 37 )
  9. BgColors=( 40 41 42 43 44 45 46 47 )
  10. local AttrNorm=0
  11. local AttrBright=1
  12. local AttrDim=2
  13. local AttrUnder=4
  14. local AttrBlink=5
  15. local AttrRev=7
  16. local AttrHide=8
  17. # define "BoldCOLOR", "BrightCOLOR", and "DimCOLOR" names
  18. # _map_colors ATTRNAME ATTRVALUE
  19. #
  20. # Defines three names for every color, attribute combintaion:
  21. # {ATTRNAME}{COLORNAME}
  22. # {ATTRNAME}{COLORNAME}Fg
  23. # {ATTRNAME}{COLORNAME}Bg
  24. #
  25. # Example: BoldRed, BoldRedFg, BoldRedBg
  26. _map_colors() {
  27. local x=0
  28. local attrname="${1}"
  29. local attrcode="${2}"
  30. while (( x < 8 )) ; do
  31. local colorname="${ColorNames[x]}"
  32. local fgcolorcode="${FgColors[x]}"
  33. local bgcolorcode="${BgColors[x]}"
  34. longcolorname="${attrname}${colorname}"
  35. _def_color "${longcolorname}" "${attrcode}" "${fgcolorcode}"
  36. _def_color "${longcolorname}Fg" "${attrcode}" "${fgcolorcode}"
  37. _def_color "${longcolorname}Bg" "${attrcode}" "${bgcolorcode}"
  38. (( x++ ))
  39. done
  40. }
  41. # _term_color [ N | N M ]
  42. _term_color() {
  43. local cv
  44. if [[ "${#}" -gt 1 ]]; then
  45. cv="${1};${2}"
  46. else
  47. cv="${1}"
  48. fi
  49. echo "\[\033[${cv}m\]"
  50. }
  51. # def_color NAME ATTRCODE COLORCODE
  52. _def_color() {
  53. local def="${1}=\"\`_term_color ${2} ${3}\`\""
  54. eval "${def}"
  55. }
  56. _map_colors Bold ${AttrBright}
  57. _map_colors Bright ${AttrBright}
  58. _map_colors Dim ${AttrDim}
  59. _map_colors '' ${AttrNorm}
  60. _def_color IntenseBlack 0 90
  61. _def_color ResetColor 0 0
  62. }
  63. # do the color definitions only once
  64. if [[ -z "${ColorNames+x}" || "${#ColorNames[*]}" = 0 || -z "${IntenseBlack:+x}" || -z "${ResetColor:+x}" ]]; then
  65. define_color_names
  66. fi