Debian Codegraph @

src/System/Console/ANSI.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- ANSI escape codes
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module System.Console.ANSI where
10
11 open import Data.List.Base as List using (List; []; _∷_)
12 open import Data.Nat.Base using (ℕ; _+_)
13 import Data.Nat.Show as ℕ using (show)
14 open import Data.String.Base using (String; concat; intersperse)
15 open import Function.Base using (_$_; case_of_)
16
17 data Layer : Set where
18 foreground background : Layer
19
20 data Colour : Set where
21 black red green yellow blue magenta cyan white : Colour
22
23 data Intensity : Set where
24 classic bright : Intensity
25
26 data Weight : Set where
27 bold faint normal : Weight
28
29 data Underlining : Set where
30 single double : Underlining
31
32 data Style : Set where
33 italic straight : Style
34
35 data Blinking : Set where
36 slow rapid : Blinking
37
38 data Command : Set where
39 reset : Command
40 setColour : Layer → Intensity → Colour → Command
41 setWeight : Weight → Command
42 setStyle : Style → Command
43 setUnderline : Underlining → Command
44 setBlinking : Blinking → Command
45
46 private
47
48 escape : String → String
49 escape txt = concat $ "\x1b[" ∷ txt ∷ "m" ∷ []
50
51 encode : Command → String
52 encode reset = "0"
53 encode (setColour l i c) = ℕ.show (layer l + colour c + intensity i)
54
55 where
56
57 layer : Layer → ℕ
58 layer foreground = 30
59 layer background = 40
60
61 colour : Colour → ℕ
62 colour black = 0
63 colour red = 1
64 colour green = 2
65 colour yellow = 3
66 colour blue = 4
67 colour magenta = 5
68 colour cyan = 6
69 colour white = 7
70
71 intensity : Intensity → ℕ
72 intensity classic = 0
73 intensity bright = 60
74
75 encode (setWeight w) = case w of λ where
76 bold → "1"
77 faint → "2"
78 normal → "22"
79
80 encode (setStyle s) = case s of λ where
81 italic → "3"
82 straight → "23"
83
84 encode (setUnderline i) = case i of λ where
85 single → "4"
86 double → "21"
87
88 encode (setBlinking b) = case b of λ where
89 slow → "5"
90 rapid → "6"
91
92 command : Command → String
93 command c = escape (encode c)
94
95 withCommand : Command → String → String
96 withCommand c txt = concat (command c ∷ txt ∷ command reset ∷ [])
97
98 commands : List Command → String
99 commands [] = command reset
100 commands cs = escape (intersperse ";" $ List.map encode cs)
101
102 withCommands : List Command → String → String
103 withCommands cs txt = concat (commands cs ∷ txt ∷ command reset ∷ [])