sgr.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package ansi
  2. import "strconv"
  3. // Select Graphic Rendition (SGR) is a command that sets display attributes.
  4. //
  5. // Default is 0.
  6. //
  7. // CSI Ps ; Ps ... m
  8. //
  9. // See: https://vt100.net/docs/vt510-rm/SGR.html
  10. func SelectGraphicRendition(ps ...Attr) string {
  11. if len(ps) == 0 {
  12. return ResetStyle
  13. }
  14. var s Style
  15. for _, p := range ps {
  16. attr, ok := attrStrings[p]
  17. if ok {
  18. s = append(s, attr)
  19. } else {
  20. if p < 0 {
  21. p = 0
  22. }
  23. s = append(s, strconv.Itoa(p))
  24. }
  25. }
  26. return s.String()
  27. }
  28. // SGR is an alias for [SelectGraphicRendition].
  29. func SGR(ps ...Attr) string {
  30. return SelectGraphicRendition(ps...)
  31. }
  32. var attrStrings = map[int]string{
  33. ResetAttr: "0",
  34. BoldAttr: "1",
  35. FaintAttr: "2",
  36. ItalicAttr: "3",
  37. UnderlineAttr: "4",
  38. SlowBlinkAttr: "5",
  39. RapidBlinkAttr: "6",
  40. ReverseAttr: "7",
  41. ConcealAttr: "8",
  42. StrikethroughAttr: "9",
  43. NoBoldAttr: "21",
  44. NormalIntensityAttr: "22",
  45. NoItalicAttr: "23",
  46. NoUnderlineAttr: "24",
  47. NoBlinkAttr: "25",
  48. NoReverseAttr: "27",
  49. NoConcealAttr: "28",
  50. NoStrikethroughAttr: "29",
  51. BlackForegroundColorAttr: "30",
  52. RedForegroundColorAttr: "31",
  53. GreenForegroundColorAttr: "32",
  54. YellowForegroundColorAttr: "33",
  55. BlueForegroundColorAttr: "34",
  56. MagentaForegroundColorAttr: "35",
  57. CyanForegroundColorAttr: "36",
  58. WhiteForegroundColorAttr: "37",
  59. ExtendedForegroundColorAttr: "38",
  60. DefaultForegroundColorAttr: "39",
  61. BlackBackgroundColorAttr: "40",
  62. RedBackgroundColorAttr: "41",
  63. GreenBackgroundColorAttr: "42",
  64. YellowBackgroundColorAttr: "43",
  65. BlueBackgroundColorAttr: "44",
  66. MagentaBackgroundColorAttr: "45",
  67. CyanBackgroundColorAttr: "46",
  68. WhiteBackgroundColorAttr: "47",
  69. ExtendedBackgroundColorAttr: "48",
  70. DefaultBackgroundColorAttr: "49",
  71. ExtendedUnderlineColorAttr: "58",
  72. DefaultUnderlineColorAttr: "59",
  73. BrightBlackForegroundColorAttr: "90",
  74. BrightRedForegroundColorAttr: "91",
  75. BrightGreenForegroundColorAttr: "92",
  76. BrightYellowForegroundColorAttr: "93",
  77. BrightBlueForegroundColorAttr: "94",
  78. BrightMagentaForegroundColorAttr: "95",
  79. BrightCyanForegroundColorAttr: "96",
  80. BrightWhiteForegroundColorAttr: "97",
  81. BrightBlackBackgroundColorAttr: "100",
  82. BrightRedBackgroundColorAttr: "101",
  83. BrightGreenBackgroundColorAttr: "102",
  84. BrightYellowBackgroundColorAttr: "103",
  85. BrightBlueBackgroundColorAttr: "104",
  86. BrightMagentaBackgroundColorAttr: "105",
  87. BrightCyanBackgroundColorAttr: "106",
  88. BrightWhiteBackgroundColorAttr: "107",
  89. }