1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
------------------------------------------------------------------------------
--- Library for formatted output on terminals
---
--- Information on ANSI Codes can be found at
--- http://en.wikipedia.org/wiki/ANSI_escape_code
---
--- @author Sebastian Fischer, Bjoern Peemoeller
--- @version December 2018
------------------------------------------------------------------------------

module System.Console.ANSI.Codes
  ( -- cursor movement
    cursorPos
  , cursorHome
  , cursorUp
  , cursorDown
  , cursorFwd
  , cursorBack
  , saveCursor
  , restoreCursor

  -- graphics control
  , clear
  , eraseLine

  -- formatting output
  , normal
  , bold
  , faint
  , italic
  , underline
  , blinkSlow
  , blinkRapid
  , inverse
  , concealed
  , crossedout

  -- foreground color
  , black
  , red
  , green
  , yellow
  , blue
  , cyan
  , magenta
  , white
  , fgDefault

  -- background color
  , bgBlack
  , bgRed
  , bgGreen
  , bgYellow
  , bgBlue
  , bgCyan
  , bgMagenta
  , bgWhite
  , bgDefault
  ) where

import List (isSuffixOf)

-- -----------------------------------------------------------------------------
-- Cursor movement
-- -----------------------------------------------------------------------------

--- move cursor to position
cursorPos :: Int -> Int -> String
cursorPos r c = cmd (show r ++ ";" ++ show c ++ "H")

--- move cursor to home position
cursorHome :: String
cursorHome = cmd "H"

--- move cursor n lines up
cursorUp :: Int -> String
cursorUp = moveCursor "A"

--- move cursor n lines down
cursorDown :: Int -> String
cursorDown = moveCursor "B"

--- move cursor n columns forward
cursorFwd :: Int -> String
cursorFwd = moveCursor "C"

--- move cursor n columns backward
cursorBack :: Int -> String
cursorBack = moveCursor "D"

--- save cursor position
saveCursor :: String
saveCursor = cmd "s"

--- restore saved cursor position
restoreCursor :: String
restoreCursor = cmd "u"

-- -----------------------------------------------------------------------------
-- Graphics control
-- -----------------------------------------------------------------------------

--- clear screen
clear :: String
clear = cmd "2J"

--- erase line
eraseLine :: String
eraseLine = cmd "K"

-- -----------------------------------------------------------------------------
-- Text formatting
-- -----------------------------------------------------------------------------

--- Reset formatting to normal formatting
normal :: String -> String
normal = mode 0

--- Bold text
bold :: String -> String
bold = mode 1

--- Faint text
faint :: String -> String
faint = mode 2

--- Italic text
italic :: String -> String
italic = mode 3

--- Underlined text
underline :: String -> String
underline = mode 4

--- Slowly blinking text
blinkSlow :: String -> String
blinkSlow = mode 5

--- rapidly blinking text
blinkRapid :: String -> String
blinkRapid = mode 6

--- Inverse colors
inverse :: String -> String
inverse = mode 7

--- Concealed (invisible) text
concealed :: String -> String
concealed = mode 8

--- Crossed out text
crossedout :: String -> String
crossedout = mode 9

-- -----------------------------------------------------------------------------
-- Foreground color
-- -----------------------------------------------------------------------------

--- Black foreground color
black :: String -> String
black = mode 30

--- Red foreground color
red :: String -> String
red = mode 31

--- Green foreground color
green :: String -> String
green = mode 32

--- Yellow foreground color
yellow :: String -> String
yellow = mode 33

--- Blue foreground color
blue :: String -> String
blue = mode 34

--- Magenta foreground color
magenta :: String -> String
magenta = mode 35

--- Cyan foreground color
cyan :: String -> String
cyan = mode 36

--- White foreground color
white :: String -> String
white = mode 37

--- Default foreground color
fgDefault :: String -> String
fgDefault = mode 39

-- -----------------------------------------------------------------------------
-- Background color
-- -----------------------------------------------------------------------------

--- Black background color
bgBlack :: String -> String
bgBlack = mode 40

--- Red background color
bgRed :: String -> String
bgRed = mode 41

--- Green background color
bgGreen :: String -> String
bgGreen = mode 42

--- Yellow background color
bgYellow :: String -> String
bgYellow = mode 43

--- Blue background color
bgBlue :: String -> String
bgBlue = mode 44

--- Magenta background color
bgMagenta :: String -> String
bgMagenta = mode 45

--- Cyan background color
bgCyan :: String -> String
bgCyan = mode 46

--- White background color
bgWhite :: String -> String
bgWhite = mode 47

--- Default background color
bgDefault :: String -> String
bgDefault = mode 49

-- -----------------------------------------------------------------------------
-- Helper functions
-- -----------------------------------------------------------------------------

--- Cursor movements
moveCursor :: String -> Int -> String
moveCursor s n = cmd (show n ++ s)

--- Text mode
mode :: Int -> String -> String
mode n s = cmd (show n ++ "m") ++ s ++ if end `isSuffixOf` s then "" else end
  where end = cmd "0m"

--- Create a command using the CSI (control sequence introducer) "\ESC["
cmd :: String -> String
cmd s = '\ESC' : '[' : s