0 | module Text.ANSI.SGR
  1 |
  2 | import Data.List
  3 |
  4 | %default total
  5 |
  6 | public export
  7 | data Color
  8 |   = Black
  9 |   | Red
 10 |   | Green
 11 |   | Yellow
 12 |   | Blue
 13 |   | Magenta
 14 |   | Cyan
 15 |   | White
 16 |   | BrightBlack
 17 |   | BrightRed
 18 |   | BrightGreen
 19 |   | BrightYellow
 20 |   | BrightBlue
 21 |   | BrightMagenta
 22 |   | BrightCyan
 23 |   | BrightWhite
 24 |   | Color256 Bits8
 25 |   | RGB Bits8 Bits8 Bits8
 26 |
 27 | Cast Color String where
 28 |   cast Black = "0"
 29 |   cast Red = "1"
 30 |   cast Green = "2"
 31 |   cast Yellow = "3"
 32 |   cast Blue = "4"
 33 |   cast Magenta = "5"
 34 |   cast Cyan = "6"
 35 |   cast White = "7"
 36 |   cast BrightBlack = "8"
 37 |   cast BrightRed = "9"
 38 |   cast BrightGreen = "10"
 39 |   cast BrightYellow = "11"
 40 |   cast BrightBlue = "12"
 41 |   cast BrightMagenta = "13"
 42 |   cast BrightCyan = "14"
 43 |   cast BrightWhite = "15"
 44 |   cast (Color256 c) = show c
 45 |   cast (RGB r g b) = "\{show r};\{show g};\{show b}"
 46 |
 47 | public export
 48 | data Style
 49 |   = Bold
 50 |   | Faint
 51 |   | NotBoldOrFaint
 52 |   | Italic
 53 |   | NotItalic
 54 |   | SingleUnderline
 55 |   | DoubleUnderline
 56 |   | NoUnderline
 57 |   | Striked
 58 |   | NotStriked
 59 |
 60 | public export %inline
 61 | Dimmed : Style
 62 | Dimmed = Faint
 63 |
 64 | public export %inline
 65 | NormalIntensity : Style
 66 | NormalIntensity = NotBoldOrFaint
 67 |
 68 | Cast Style String where
 69 |   cast Bold = "1"
 70 |   cast Faint = "2"
 71 |   cast NotBoldOrFaint = "22"
 72 |   cast Italic = "3"
 73 |   cast NotItalic = "23"
 74 |   cast SingleUnderline = "4"
 75 |   cast DoubleUnderline = "21"
 76 |   cast NoUnderline = "24"
 77 |   cast Striked = "9"
 78 |   cast NotStriked = "29"
 79 |
 80 | public export
 81 | data Blink = Slow | Rapid | NoBlink
 82 |
 83 | Cast Blink String where
 84 |   cast Slow = "5"
 85 |   cast Rapid = "6"
 86 |   cast NoBlink = "25"
 87 |
 88 | public export
 89 | data SGR
 90 |   = Reset
 91 |   | SetForeground Color
 92 |   | SetBackground Color
 93 |   | SetStyle Style
 94 |   | SetBlink Blink
 95 |   | SetReversed Bool
 96 |
 97 | ||| Returns the ANSI escape code equivalent to the list of operations provided.
 98 | export
 99 | escapeSGR : List SGR -> String
100 | escapeSGR xs = "\x1B[\{params}m"
101 |   where
102 |     toCode : SGR -> String
103 |     toCode Reset = "0"
104 |     toCode (SetForeground c@(RGB {})) = "38;2;" ++ cast c
105 |     toCode (SetBackground c@(RGB {})) = "48;2;" ++ cast c
106 |     toCode (SetForeground c) = "38;5;" ++ cast c
107 |     toCode (SetBackground c) = "48;5;" ++ cast c
108 |     toCode (SetStyle s) = cast s
109 |     toCode (SetBlink b) = cast b
110 |     toCode (SetReversed True) = "7"
111 |     toCode (SetReversed False) = "27"
112 |
113 |     params : String
114 |     params = fastConcat $ intersperse ";" $ toCode <$> xs
115 |