Debian Codegraph @

app/Example.hs

raw
1 module Main
2 (
3 main
4 ) where
5
6 import Control.Concurrent (threadDelay)
7 import Control.Monad (forM_, replicateM_)
8 import System.IO (hFlush, stdout)
9 import Text.Printf(printf)
10
11 import Data.Colour.SRGB (sRGB24)
12
13 import System.Console.ANSI
14 import System.Environment ( getArgs )
15
16 examples :: [(String, IO ())]
17 examples =
18 [ ( "Cursor movement", cursorMovementExample )
19 , ( "Line change", lineChangeExample )
20 , ( "Set cursor position", setCursorPositionExample )
21 , ( "Save restore cursor", saveRestoreCursorExample )
22 , ( "Clear", clearExample )
23 , ( "Line wrap", lineWrapExample )
24 , ( "Scroll", scrollExample )
25 , ( "Screen buffers", screenBuffersExample )
26 , ( "SGR color", sgrColorExample )
27 , ( "SGR other", sgrOtherExample )
28 , ( "Cursor visibility", cursorVisibilityExample )
29 , ( "Hyperlink", hyperlinkExample )
30 , ( "Title", titleExample )
31 , ( "Get cursor position", getCursorPositionExample )
32 , ( "Get terminal size", getTerminalSizeExample )
33 , ( "Get layer color", getLayerColorExample )
34 ]
35
36 main :: IO ()
37 main = do
38 args <- getArgs
39 let numExamples = zip [1 ..] examples
40 if wantHelp args
41 then
42 listExamples numExamples
43 else do
44 let wantedNumExamples = filter (isExample args) numExamples
45 wantedExamples = map getExample wantedNumExamples
46 stdoutSupportsANSI <- hNowSupportsANSI stdout
47 if stdoutSupportsANSI
48 then
49 mapM_ (resetScreen >>) wantedExamples
50 else
51 putStrLn "Standard output does not support 'ANSI' escape codes."
52 where
53 wantHelp :: [String] -> Bool
54 wantHelp args = "-h" `elem` args || "--help" `elem` args
55
56 listExamples :: [(Int, (String, IO ()))] -> IO ()
57 listExamples = mapM_ (\(n, (d, _)) -> putStrLn $ show n ++ ": " ++ d)
58
59 isExample :: [String] -> (Int, (String, IO ())) -> Bool
60 isExample args (n, _) = show n `elem` args
61
62 getExample :: (Int, (String, IO ())) -> IO ()
63 getExample (_, (_, example)) = example
64
65 -- Annex D to Standard ECMA-48 (5th Ed, 1991) identifies that the representation
66 -- of an erased state is implementation-dependent. There may or may not be a
67 -- distinction between a character position in the erased state and one imaging
68 -- SPACE. Consequently, to reset the screen, the default graphic rendition must
69 -- be selected (setSGR [Reset]) before all character positions are put into the
70 -- erased state (clearScreen).
71 resetScreen :: IO ()
72 resetScreen = setSGR [Reset] >> clearScreen >> setCursorPosition 0 0
73
74 pause :: IO ()
75 pause = do
76 hFlush stdout
77 -- 1 second pause
78 threadDelay 1000000
79
80 cursorMovementExample :: IO ()
81 cursorMovementExample = do
82 putStrLn "Line One"
83 putStr "Line Two"
84 pause
85 -- Line One
86 -- Line Two
87
88 cursorUp 1
89 putStr " - Extras"
90 pause
91 -- Line One - Extras
92 -- Line Two
93
94 cursorBackward 2
95 putStr "zz"
96 pause
97 -- Line One - Extrzz
98 -- Line Two
99
100 cursorForward 2
101 putStr "- And More"
102 pause
103 -- Line One - Extrzz - And More
104 -- Line Two
105
106 cursorDown 1
107 putStr "Disconnected"
108 pause
109 -- Line One - Extrzz - And More
110 -- Line Two Disconnected
111
112 lineChangeExample :: IO ()
113 lineChangeExample = do
114 putStrLn "Line One"
115 putStr "Line Two"
116 pause
117 -- Line One
118 -- Line Two
119
120 cursorUpLine 1
121 putStr "New Line One"
122 pause
123 -- New Line One
124 -- Line Two
125
126 cursorDownLine 1
127 putStr "New Line Two"
128 pause
129 -- New Line One
130 -- New Line Two
131
132 setCursorPositionExample :: IO ()
133 setCursorPositionExample = do
134 putStrLn "Line One"
135 putStrLn "Line Two"
136 pause
137 -- Line One
138 -- Line Two
139
140 setCursorPosition 0 5
141 putStr "Foo"
142 pause
143 -- Line Foo
144 -- Line Two
145
146 setCursorPosition 1 5
147 putStr "Bar"
148 pause
149 -- Line Foo
150 -- Line Bar
151
152 setCursorColumn 1
153 putStr "oaf"
154 pause
155 -- Line Foo
156 -- Loaf Bar
157
158 saveRestoreCursorExample :: IO ()
159 saveRestoreCursorExample = do
160 putStr "Start sentence ..."
161 pause
162 -- Start sentence ...
163
164 saveCursor
165 setCursorPosition 2 3
166 putStr "SPLASH!"
167 pause
168 -- Start sentence ...
169 --
170 -- SPLASH!
171
172 restoreCursor
173 putStr " end sentence, uninterrupted."
174 pause
175 -- Start sentence ... end sentence, uninterrupted
176 --
177 -- SPLASH!
178
179 clearExample :: IO ()
180 clearExample = do
181 putStrLn "Line One"
182 putStrLn "Line Two"
183 pause
184 -- Line One
185 -- Line Two
186
187 setCursorPosition 0 4
188 clearFromCursorToScreenEnd
189 pause
190 -- Line
191
192 resetScreen
193 putStrLn "Line One"
194 putStrLn "Line Two"
195 pause
196 -- Line One
197 -- Line Two
198
199 setCursorPosition 1 4
200 clearFromCursorToScreenBeginning
201 pause
202 --
203 -- Two
204
205 resetScreen
206 putStrLn "Line One"
207 putStrLn "Line Two"
208 pause
209 -- Line One
210 -- Line Two
211
212 setCursorPosition 0 4
213 clearFromCursorToLineEnd
214 pause
215 -- Line
216 -- Line Two
217
218 setCursorPosition 1 4
219 clearFromCursorToLineBeginning
220 pause
221 -- Line
222 -- Two
223
224 clearLine
225 pause
226 -- Line
227
228 clearScreen
229 pause
230 --
231
232 lineWrapExample :: IO ()
233 lineWrapExample = do
234 putStrLn $ take 240 $ cycle "Default-line-wrap|"
235 pause
236 -- Default-line-wrap|Default-line-wrap|Default-line-wrap|Default-line-wrap|Def
237 -- ault-line-wrap|Default-line-wrap|Default-line-wrap|Default-line-wrap|Defaul
238 -- t-line-wrap|Default-line-wrap|Default-line-wrap|Default-line-wrap|Default-l
239 disableLineWrap
240 putStrLn $ take 240 $ cycle "Disable-line-wrap|"
241 pause
242 -- Disable-line-wrap|Disable-line-wrap|Disable-line-wrap|Disable-line-wrap|Dis
243 enableLineWrap
244 putStrLn $ take 240 $ cycle "Enable-line-wrap|"
245 pause
246 -- Enable-line-wrap|Enable-line-wrap|Enable-line-wrap|Enable-line-wrap|Enable-
247 -- line-wrap|Enable-line-wrap|Enable-line-wrap|Enable-line-wrap|Enable-line-wr
248 -- ap|Enable-line-wrap|Enable-line-wrap|Enable-line-wrap|Enable-line-wrap|Enab
249
250 scrollExample :: IO ()
251 scrollExample = do
252 putStrLn "Line One"
253 putStrLn "Line Two"
254 putStrLn "Line Three"
255 pause
256 -- Line One
257 -- Line Two
258 -- Line Three
259
260 scrollPageDown 2
261 pause
262 --
263 --
264 -- Line One
265 -- Line Two
266 -- Line Three
267
268 scrollPageUp 3
269 pause
270 -- Line Two
271 -- Line Three
272
273 screenBuffersExample :: IO ()
274 screenBuffersExample = do
275 replicateM_ 5 $ putStrLn "This message is on the Normal Screen Bufffer"
276 replicateM_ 5 pause
277 useAlternateScreenBuffer
278 replicateM_ 5 $ putStrLn "This message is on the Alternate Screen Bufffer"
279 replicateM_ 5 pause
280 useNormalScreenBuffer
281 replicateM_ 5 $ putStrLn "This message is continuing where we left off"
282 replicateM_ 5 pause
283
284 sgrColorExample :: IO ()
285 sgrColorExample = do
286 let colors = enumFromTo minBound maxBound :: [Color]
287 forM_ [Foreground, Background, Underlining] $ \layer -> do
288 forM_ [Dull, Vivid] $ \intensity -> do
289 resetScreen
290 forM_ colors $ \color -> do
291 setSGR [Reset]
292 setSGR [SetColor layer intensity color]
293 setSGR [SetUnderlining SingleUnderline]
294 print color
295 pause
296 -- The ANSI eight standard colors, 6 times in sequence (three layers and two
297 -- intensities)
298
299 resetScreen
300 putStrLn "True color (24 bit color depth)"
301 putStrLn "-------------------------------"
302 putStrLn ""
303 setSGR [SetRGBColor Foreground $ sRGB24 0 0 0]
304 forM_ [0 .. 23] $ \row -> do
305 forM_ [0 .. 47] $ \col -> do
306 let r = row * 11
307 g = 255 - r
308 b = col * 5
309 setSGR [SetRGBColor Background $ sRGB24 r g b]
310 putStr "-"
311 putStrLn ""
312 replicateM_ 5 pause
313 -- True colors, a swatch of 24 rows and 48 columns
314
315 resetScreen
316 putStrLn "A 256-color palette"
317 putStrLn "-------------------"
318 putStrLn ""
319
320 -- First 16 colors ('system' colors in xterm protocol), in a row
321 --
322 -- 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 A A B B C C D D E E F F
323 forM_ [Dull .. Vivid] $ \intensity -> do
324 forM_ [Black .. White] $ \color -> do
325 let i = fromEnum intensity * 8 + fromEnum color
326 eol = i == 15
327 setSGR [SetPaletteColor Background $ xtermSystem intensity color]
328 setSGR [SetPaletteColor Foreground $ xtermSystem Dull Black]
329 printf "%X " i
330 setSGR [SetPaletteColor Foreground $ xtermSystem Vivid White]
331 printf "%X" i
332 if eol
333 then putStrLn ""
334 else do
335 setSGR [Reset]
336 putStr " "
337 putStrLn ""
338
339 -- Next 216 colors (6 level RGB in xterm protocol), in 12 rows of 18
340 --
341 -- 000 001 002 003 004 005 010 011 012 013 014 015 020 021 022 023 024 025
342 -- 030 031 032 033 034 035 040 041 042 043 044 045 050 051 052 053 054 055
343 -- 100 101 102 103 104 105 110 111 112 113 114 115 120 121 122 123 124 125
344 -- ... and so on ...
345 forM_ [0 .. 5] $ \r -> do
346 forM_ [0 .. 5] $ \g -> do
347 forM_ [0 .. 5] $ \b -> do
348 let i = 16 + b + g * 6 + r * 36
349 eol = i `mod` 18 == 15
350 r' = (r + 3) `mod` 6
351 g' = (g + 3) `mod` 6
352 b' = (b + 3) `mod` 6
353 setSGR [SetPaletteColor Foreground $ xterm6LevelRGB r' g' b']
354 setSGR [SetPaletteColor Background $ xterm6LevelRGB r g b]
355 putStr $ show r ++ show g ++ show b
356 if eol
357 then putStrLn ""
358 else do
359 setSGR [Reset]
360 putStr " "
361 putStrLn ""
362
363 -- Final 24 colors (24 levels of gray in xterm protocol), in two rows
364 --
365 -- 0 1 2 3 4 5 6 7 8 9 10 11
366 -- 12 13 14 15 16 17 18 19 20 21 22 23
367 forM_ [0 .. 23] $ \y -> do
368 setSGR [SetPaletteColor Foreground $ xterm24LevelGray $ (y + 12) `mod` 24]
369 setSGR [SetPaletteColor Background $ xterm24LevelGray y]
370 printf "%3d" y
371 if y == 11
372 then putStrLn ""
373 else do
374 setSGR [Reset]
375 putStr " "
376 replicateM_ 5 pause
377
378 resetScreen
379 setSGR [Reset]
380 setSGR [SetUnderlining SingleUnderline]
381 forM_
382 (zip "Underlining: True color (24 bit color depth)" (cycle [0, 10 .. 255])) $
383 \(c, i) -> do
384 setSGR [SetRGBColor Underlining $ sRGB24 i 0 0]
385 putChar c
386 putChar '\n'
387 putChar '\n'
388 setSGR [Reset]
389 setSGR [SetUnderlining SingleUnderline]
390 forM_ (zip "Underlining: A 256-color palette" (cycle [0 .. 5])) $
391 \(c, i) -> do
392 setSGR [SetPaletteColor Underlining $ xterm6LevelRGB i 0 0]
393 putChar c
394 putChar '\n'
395 replicateM_ 5 pause
396
397 sgrOtherExample :: IO ()
398 sgrOtherExample = do
399 let named_styles = [ (SetConsoleIntensity BoldIntensity, "Bold")
400 , (SetConsoleIntensity FaintIntensity, "Faint")
401 , (SetConsoleIntensity NormalIntensity, "Normal")
402 , (SetItalicized True, "Italic")
403 , (SetItalicized False, "No Italics")
404 , (SetUnderlining SingleUnderline, "Single Underline")
405 , (SetUnderlining DoubleUnderline, "Double Underline")
406 , (SetUnderlining CurlyUnderline, "Curly Underline")
407 , (SetUnderlining DottedUnderline, "Dotted Underline")
408 , (SetUnderlining DashedUnderline, "Dashed Underline")
409 , (SetUnderlining NoUnderline, "No Underline")
410 , (SetBlinkSpeed SlowBlink, "Slow Blink")
411 , (SetBlinkSpeed RapidBlink, "Rapid Blink")
412 , (SetBlinkSpeed NoBlink, "No Blink")
413 , (SetVisible False, "Conceal")
414 , (SetVisible True, "Reveal")
415 ]
416 forM_ named_styles $ \(style, name) -> do
417 resetScreen
418 setSGR [style]
419 putStrLn name
420 pause
421 -- Text describing a style displayed in that style in sequence
422
423 setSGR [SetColor Foreground Vivid Red]
424 setSGR [SetColor Background Vivid Blue]
425
426 clearScreen >> setCursorPosition 0 0
427 setSGR [SetSwapForegroundBackground False]
428 putStr "Red-On-Blue"
429 pause
430 -- Red-On-Blue
431
432 clearScreen >> setCursorPosition 0 0
433 setSGR [SetSwapForegroundBackground True]
434 putStr "Blue-On-Red"
435 pause
436 -- Blue-On-Red
437
438 cursorVisibilityExample :: IO ()
439 cursorVisibilityExample = do
440 putStr "Cursor Demo"
441 pause
442 -- Cursor Demo|
443
444 hideCursor
445 pause
446 -- Cursor Demo
447
448 showCursor
449 pause
450 -- Cursor Demo|
451
452 hyperlinkExample :: IO ()
453 hyperlinkExample = do
454 putStr "Hyperlink demo: "
455 hyperlink "https://example.com" "Example hyperlink\n"
456 putStrLn ""
457 putStrLn "Linked hyperlinks demo:"
458 hyperlinkWithId "ref" "https://example.com" "Example linked hyperlink one\n"
459 hyperlinkWithId "ref" "https://example.com" "Example linked hyperlink two\n"
460
461 replicateM_ 5 pause
462 -- Hyperlink demo: Example hyperlink
463 --
464 -- Linked hyperlinks demo:
465 -- Example linked hyperlink one
466 -- Example linked hyperlink two
467
468 titleExample :: IO ()
469 titleExample = do
470 putStr "Title Demo"
471 pause
472 -- ~/foo/ - ansi-terminal-ex - 83x70
473 ------------------------------------
474 -- Title Demo
475
476 setTitle "Yup, I'm a new title!"
477 pause
478 -- Yup, I'm a new title! - ansi-terminal-ex - 83x70
479 ---------------------------------------------------
480 -- Title Demo
481
482 getCursorPositionExample :: IO ()
483 getCursorPositionExample = do
484 putStrLn " 11111111112222222222"
485 putStrLn "12345678901234567890123456789"
486 putStr "Report cursor position here:"
487 pause
488 -- 11111111112222222222
489 -- 12345678901234567890123456789
490 -- Report cursor position here:|
491 result <- getCursorPosition
492 putStrLn " (3rd row, 29th column) to stdin, as CSI 3 ; 29 R.\n"
493 case result of
494 Just (row, col) -> putStrLn $ "The cursor was at row number " ++
495 show (row + 1) ++ " and column number " ++ show (col + 1) ++ ".\n"
496 Nothing -> putStrLn "Error: unable to get the cursor position\n"
497 replicateM_ 3 pause
498 -- 11111111112222222222
499 -- 12345678901234567890123456789
500 -- Report cursor position here: (3rd row, 29th column) to stdin, as CSI 3 ; 29 R.
501 --
502 -- The cursor was at row number 3 and column number 29.
503
504 getTerminalSizeExample :: IO ()
505 getTerminalSizeExample = do
506 result <- getTerminalSize
507 case result of
508 Just (h, w) -> putStrLn $ "The size of the terminal is " ++ show h ++
509 " rows by " ++ show w ++ " columns.\n"
510 Nothing -> putStrLn "Error: unable to get the terminal size\n"
511 pause
512 -- The size of the terminal is 25 rows by 80 columns.
513
514 getLayerColorExample :: IO ()
515 getLayerColorExample = do
516 fgResult <- getLayerColor Foreground
517 case fgResult of
518 Just fgCol -> putStrLn $ "The reported foreground color is:\n" ++
519 show fgCol ++ "\n"
520 Nothing -> putStrLn "Error: unable to get the foreground color\n"
521 bgResult <- getLayerColor Background
522 case bgResult of
523 Just bgCol -> putStrLn $ "The reported background color is:\n" ++
524 show bgCol ++ "\n"
525 Nothing -> putStrLn "Error: unable to get the background color\n"