Debian Codegraph @

src/Text/Tabular/Base.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Fancy display functions for List-based tables
5 --
6 -- The functions in this module assume some (unenforced) invariants.
7 -- If you cannot guarantee that your data respects these invariants,
8 -- you should instead use Text.Tabular.List.
9 ------------------------------------------------------------------------
10
11 {-# OPTIONS --safe --cubical-compatible #-}
12
13 module Text.Tabular.Base where
14
15 open import Data.Bool.Base using (if_then_else_)
16 open import Data.Char.Base using (Char)
17 open import Data.List.Base as List
18 using (List; []; _∷_; _?∷_; _++_; _∷ʳ?_; null; map; intersperse)
19 open import Data.Maybe.Base as Maybe using (Maybe; nothing; just; maybe)
20 open import Data.Nat.Base
21 open import Data.String.Base as String
22 using (String; fromChar; unlines; replicate; length)
23 open import Function.Base
24 open import Agda.Builtin.Equality
25
26 open String
27 using ( Alignment
28 ; Left
29 ; Center
30 ; Right
31 ) public
32
33 record TabularLine : Set where
34 field
35 left : Maybe String
36 cont : Maybe Char
37 sep : String
38 right : Maybe String
39 open TabularLine
40
41 record TabularConfig : Set where
42 field
43 top : Maybe TabularLine
44 sep : Maybe TabularLine
45 row : TabularLine
46 bot : Maybe TabularLine
47 open TabularConfig
48
49 unicode : TabularConfig
50 unicode .top = just λ where
51 .left → just "┌"
52 .cont → just '─'
53 .sep → "┬"
54 .right → just "┐"
55 unicode .sep = just λ where
56 .left → just "├"
57 .cont → just '─'
58 .sep → "┼"
59 .right → just "┤"
60 unicode .row = λ where
61 .left → just "│"
62 .cont → nothing
63 .sep → "│"
64 .right → just "│"
65 unicode .bot = just λ where
66 .left → just "└"
67 .cont → just '─'
68 .sep → "┴"
69 .right → just "┘"
70
71 ascii : TabularConfig
72 ascii .top = just λ where
73 .left → just "+"
74 .cont → just '-'
75 .sep → "-"
76 .right → just "+"
77 ascii .sep = just λ where
78 .left → just "|"
79 .cont → just '-'
80 .sep → "+"
81 .right → just "|"
82 ascii .row = λ where
83 .left → just "|"
84 .cont → nothing
85 .sep → "|"
86 .right → just "|"
87 ascii .bot = just λ where
88 .left → just "+"
89 .cont → just '-'
90 .sep → "-"
91 .right → just "+"
92
93 compact : TabularConfig → TabularConfig
94 compact c = record c { sep = nothing }
95
96 private
97 dropBorder : TabularLine → TabularLine
98 dropBorder l = record l { left = nothing; right = nothing }
99
100 noBorder : TabularConfig → TabularConfig
101 noBorder c .top = nothing
102 noBorder c .sep = Maybe.map dropBorder (c .sep)
103 noBorder c .row = dropBorder (c .row)
104 noBorder c .bot = nothing
105
106
107 private
108 space : TabularLine → TabularLine
109 space l = let pad = maybe fromChar " " (l .cont) in λ where
110 .left → Maybe.map (String._++ pad) (l .left)
111 .cont → l .cont
112 .sep → pad String.++ l .sep String.++ pad
113 .right → Maybe.map (pad String.++_) (l .right)
114
115 addSpace : TabularConfig → TabularConfig
116 addSpace c .top = Maybe.map space (c .top)
117 addSpace c .sep = Maybe.map space (c .sep)
118 addSpace c .row = space (c .row)
119 addSpace c .bot = Maybe.map space (c .bot)
120
121 whitespace : TabularConfig
122 whitespace .top = nothing
123 whitespace .sep = nothing
124 whitespace .row = λ where
125 .left → nothing
126 .cont → nothing
127 .sep → " "
128 .right → nothing
129 whitespace .bot = nothing
130
131 -- /!\ Invariants:
132 -- * the table is presented as a list of rows
133 -- * header has the same length as each one of the rows
134 -- i.e. we have a rectangular table
135 -- * all of the strings in a given column have the same length
136
137 unsafeDisplay : TabularConfig → List (List String) → List String
138 unsafeDisplay _ [] = []
139 unsafeDisplay c (header ∷ rows) =
140 map String.concat $ th ++ (trs ∷ʳ? lbot)
141 where
142
143 cellsOf : Maybe Char → List String → List String
144 cellsOf nothing = id
145 cellsOf (just c) = map (λ cell → replicate (length cell) c)
146
147 lineOf : TabularLine → List String → List String
148 lineOf l xs = l .left
149 ?∷ intersperse (l .sep) (cellsOf (l .cont) xs)
150 ∷ʳ? l .right
151
152 mlineOf : Maybe TabularLine → List String → Maybe (List String)
153 mlineOf l xs = Maybe.map (λ l → lineOf l xs) l
154
155 ltop : Maybe (List String)
156 lsep : Maybe (List String)
157 tr : List String → List String
158 lbot : Maybe (List String)
159
160 ltop = mlineOf (c. top) header
161 lsep = mlineOf (c. sep) header
162 tr = lineOf (c. row)
163 lbot = mlineOf (c. bot) header
164
165 th = ltop ?∷ tr header ∷ []
166 trs = if null rows then id else (maybe _∷_ id lsep)
167 $ maybe intersperse id lsep
168 $ map tr rows