|
| 1 | +-- | ANSI escape sequences. |
| 2 | +-- |
| 3 | +-- This is a stripped-down version of the parts of the @ansi-terminal@ package |
| 4 | +-- we use. |
| 5 | +-- |
| 6 | +-- See: <https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797> |
| 7 | +module ANSI |
| 8 | + ( SGR (..) |
| 9 | + , setSGR |
| 10 | + ) where |
| 11 | + |
| 12 | +-- | Render a single numeric SGR sequence. |
| 13 | +rawSGR :: Int -> String |
| 14 | +rawSGR code = "\x1b[" <> show code <> "m" |
| 15 | + |
| 16 | +-- | Render a series of `SGR` escape sequences. |
| 17 | +setSGR :: [SGR] -> String |
| 18 | +setSGR = concat . map renderSGR |
| 19 | + |
| 20 | +-- | All of the SGR sequences we want to use. |
| 21 | +data SGR |
| 22 | + = Reset |
| 23 | + | Bold |
| 24 | + | Dim |
| 25 | + | Italic |
| 26 | + | Underline |
| 27 | + | Black |
| 28 | + | Red |
| 29 | + | Green |
| 30 | + | Yellow |
| 31 | + | Blue |
| 32 | + | Magenta |
| 33 | + | Cyan |
| 34 | + | White |
| 35 | + | Default |
| 36 | + | OnBlack |
| 37 | + | OnRed |
| 38 | + | OnGreen |
| 39 | + | OnYellow |
| 40 | + | OnBlue |
| 41 | + | OnMagenta |
| 42 | + | OnCyan |
| 43 | + | OnWhite |
| 44 | + | OnDefault |
| 45 | + | BrightBlack |
| 46 | + | BrightRed |
| 47 | + | BrightGreen |
| 48 | + | BrightYellow |
| 49 | + | BrightBlue |
| 50 | + | BrightMagenta |
| 51 | + | BrightCyan |
| 52 | + | BrightWhite |
| 53 | + | OnBrightBlack |
| 54 | + | OnBrightRed |
| 55 | + | OnBrightGreen |
| 56 | + | OnBrightYellow |
| 57 | + | OnBrightBlue |
| 58 | + | OnBrightMagenta |
| 59 | + | OnBrightCyan |
| 60 | + | OnBrightWhite |
| 61 | + deriving (Show) |
| 62 | + |
| 63 | +-- Render a single `SGR` sequence. |
| 64 | +renderSGR :: SGR -> String |
| 65 | +renderSGR code = |
| 66 | + case code of |
| 67 | + Reset -> rawSGR 0 |
| 68 | + Bold -> rawSGR 1 |
| 69 | + Dim -> rawSGR 2 |
| 70 | + Italic -> rawSGR 3 |
| 71 | + Underline -> rawSGR 4 |
| 72 | + Black -> rawSGR 30 |
| 73 | + Red -> rawSGR 31 |
| 74 | + Green -> rawSGR 32 |
| 75 | + Yellow -> rawSGR 33 |
| 76 | + Blue -> rawSGR 34 |
| 77 | + Magenta -> rawSGR 35 |
| 78 | + Cyan -> rawSGR 36 |
| 79 | + White -> rawSGR 37 |
| 80 | + Default -> rawSGR 39 |
| 81 | + OnBlack -> rawSGR 40 |
| 82 | + OnRed -> rawSGR 41 |
| 83 | + OnGreen -> rawSGR 42 |
| 84 | + OnYellow -> rawSGR 43 |
| 85 | + OnBlue -> rawSGR 44 |
| 86 | + OnMagenta -> rawSGR 45 |
| 87 | + OnCyan -> rawSGR 46 |
| 88 | + OnWhite -> rawSGR 47 |
| 89 | + OnDefault -> rawSGR 49 |
| 90 | + BrightBlack -> rawSGR 90 |
| 91 | + BrightRed -> rawSGR 91 |
| 92 | + BrightGreen -> rawSGR 92 |
| 93 | + BrightYellow -> rawSGR 93 |
| 94 | + BrightBlue -> rawSGR 94 |
| 95 | + BrightMagenta -> rawSGR 95 |
| 96 | + BrightCyan -> rawSGR 96 |
| 97 | + BrightWhite -> rawSGR 97 |
| 98 | + OnBrightBlack -> rawSGR 100 |
| 99 | + OnBrightRed -> rawSGR 101 |
| 100 | + OnBrightGreen -> rawSGR 102 |
| 101 | + OnBrightYellow -> rawSGR 103 |
| 102 | + OnBrightBlue -> rawSGR 104 |
| 103 | + OnBrightMagenta -> rawSGR 105 |
| 104 | + OnBrightCyan -> rawSGR 106 |
| 105 | + OnBrightWhite -> rawSGR 107 |
0 commit comments