Debian Codegraph @

doc/README/Text/Tabular.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Examples of printing list and vec-based tables
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --safe --cubical-compatible #-}
8
9 module README.Text.Tabular where
10
11 open import Function.Base
12 open import Relation.Binary.PropositionalEquality
13
14 open import Data.List.Base
15 open import Data.String.Base
16 open import Data.Vec.Base
17
18 open import Text.Tabular.Base
19 import Text.Tabular.List as Tabularˡ
20 import Text.Tabular.Vec as Tabularᵛ
21
22 ------------------------------------------------------------------------
23 -- VEC
24 --
25 -- If you have a matrix of strings, you simply need to:
26 -- * pick a configuration (see below)
27 -- * pick an alignment for each column
28 -- * pass the matrix
29 --
30 -- The display function will then pad each string on the left, right,
31 -- or both to respect the alignment constraints.
32 -- It will return a list of strings corresponding to each line in the
33 -- table. You may then:
34 --- * use Data.String.Base's unlines to produce a String
35 -- * use Text.Pretty's text and vcat to produce a Doc (i.e. indentable!)
36 ------------------------------------------------------------------------
37
38 _ : unlines (Tabularᵛ.display unicode
39 (Right ∷ Left ∷ Center ∷ [])
40 ( ("foo" ∷ "bar" ∷ "baz" ∷ [])
41 ∷ ("1" ∷ "2" ∷ "3" ∷ [])
42 ∷ ("6" ∷ "5" ∷ "4" ∷ [])
43 ∷ []))
44 ≡ "┌───┬───┬───┐
45 \ \│foo│bar│baz│
46 \ \├───┼───┼───┤
47 \ \│ 1│2 │ 3 │
48 \ \├───┼───┼───┤
49 \ \│ 6│5 │ 4 │
50 \ \└───┴───┴───┘"
51 _ = refl
52
53 ------------------------------------------------------------------------
54 -- CONFIG
55 --
56 -- Configurations allow you to change the way the table is displayed.
57 ------------------------------------------------------------------------
58
59 -- We will use the same example throughout
60
61 foobar : Vec (Vec String 2) 3
62 foobar = ("foo" ∷ "bar" ∷ [])
63 ∷ ("1" ∷ "2" ∷ [])
64 ∷ ("4" ∷ "3" ∷ [])
65 ∷ []
66
67 ------------------------------------------------------------------------
68 -- Basic configurations: unicode, ascii, whitespace
69
70 -- unicode
71
72 _ : unlines (Tabularᵛ.display unicode
73 (Right ∷ Left ∷ [])
74 foobar)
75 ≡ "┌───┬───┐
76 \ \│foo│bar│
77 \ \├───┼───┤
78 \ \│ 1│2 │
79 \ \├───┼───┤
80 \ \│ 4│3 │
81 \ \└───┴───┘"
82 _ = refl
83
84 -- ascii
85
86 _ : unlines (Tabularᵛ.display ascii
87 (Right ∷ Left ∷ [])
88 foobar)
89 ≡ "+-------+
90 \ \|foo|bar|
91 \ \|---+---|
92 \ \| 1|2 |
93 \ \|---+---|
94 \ \| 4|3 |
95 \ \+-------+"
96 _ = refl
97
98 -- whitespace
99
100 _ : unlines (Tabularᵛ.display whitespace
101 (Right ∷ Left ∷ [])
102 foobar)
103 ≡ "foo bar
104 \ \ 1 2
105 \ \ 4 3 "
106 _ = refl
107
108 ------------------------------------------------------------------------
109 -- Modifiers: altering existing configurations
110
111 -- In these examples we will be using unicode as the base configuration.
112 -- However these modifiers apply to all configurations (and can even be
113 -- combined)
114
115 -- compact: drop the horizontal line between each row
116
117 _ : unlines (Tabularᵛ.display (compact unicode)
118 (Right ∷ Left ∷ [])
119 foobar)
120 ≡ "┌───┬───┐
121 \ \│foo│bar│
122 \ \│ 1│2 │
123 \ \│ 4│3 │
124 \ \└───┴───┘"
125 _ = refl
126
127 -- noBorder: drop the outside borders
128
129 _ : unlines (Tabularᵛ.display (noBorder unicode)
130 (Right ∷ Left ∷ [])
131 foobar)
132 ≡ "foo│bar
133 \ \───┼───
134 \ \ 1│2
135 \ \───┼───
136 \ \ 4│3 "
137 _ = refl
138
139 -- addSpace : add whitespace space inside cells
140
141 _ : unlines (Tabularᵛ.display (addSpace unicode)
142 (Right ∷ Left ∷ [])
143 foobar)
144 ≡ "┌─────┬─────┐
145 \ \│ foo │ bar │
146 \ \├─────┼─────┤
147 \ \│ 1 │ 2 │
148 \ \├─────┼─────┤
149 \ \│ 4 │ 3 │
150 \ \└─────┴─────┘"
151 _ = refl
152
153 -- compact together with addSpace
154
155 _ : unlines (Tabularᵛ.display (compact (addSpace unicode))
156 (Right ∷ Left ∷ [])
157 foobar)
158 ≡ "┌─────┬─────┐
159 \ \│ foo │ bar │
160 \ \│ 1 │ 2 │
161 \ \│ 4 │ 3 │
162 \ \└─────┴─────┘"
163 _ = refl
164
165
166 ------------------------------------------------------------------------
167 -- LIST
168 --
169 -- Same thing as for vectors except that if the list of lists is not
170 -- rectangular, it is padded with empty strings to make it so. If there
171 -- are not enough alignment directives, we arbitrarily pick Left.
172 ------------------------------------------------------------------------
173
174 _ : unlines (Tabularˡ.display unicode
175 (Center ∷ Right ∷ [])
176 ( ("foo" ∷ "bar" ∷ [])
177 ∷ ("partial" ∷ "rows" ∷ "are" ∷ "ok" ∷ [])
178 ∷ ("3" ∷ "2" ∷ "1" ∷ "..." ∷ "surprise!" ∷ [])
179 ∷ []))
180 ≡ "┌───────┬────┬───┬───┬─────────┐
181 \ \│ foo │ bar│ │ │ │
182 \ \├───────┼────┼───┼───┼─────────┤
183 \ \│partial│rows│are│ok │ │
184 \ \├───────┼────┼───┼───┼─────────┤
185 \ \│ 3 │ 2│1 │...│surprise!│
186 \ \└───────┴────┴───┴───┴─────────┘"
187 _ = refl
188
189 ------------------------------------------------------------------------
190 -- LIST (UNSAFE)
191 --
192 -- If you know *for sure* that your data is already perfectly rectangular
193 -- i.e. all the rows of the list of lists have the same length
194 -- in each column, all the strings have the same width
195 -- then you can use the unsafeDisplay function defined Text.Tabular.Base.
196 --
197 -- This is what gets used internally by `Text.Tabular.Vec` and
198 -- `Text.Tabular.List` once the potentially unsafe data has been
199 -- processed.
200 ------------------------------------------------------------------------
201
202 _ : unlines (unsafeDisplay (compact unicode)
203 ( ("foo" ∷ "bar" ∷ [])
204 ∷ (" 1" ∷ " 2" ∷ [])
205 ∷ (" 4" ∷ " 3" ∷ [])
206 ∷ []))
207 ≡ "┌───┬───┐
208 \ \│foo│bar│
209 \ \│ 1│ 2│
210 \ \│ 4│ 3│
211 \ \└───┴───┘"
212 _ = refl