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
|
------------------------------------------------------------------------
-- The Agda standard library
--
-- Examples of printing list and vec-based tables
------------------------------------------------------------------------
{-# OPTIONS --safe --cubical-compatible #-}
module README.Text.Tabular where
open import Function.Base
open import Relation.Binary.PropositionalEquality
open import Data.List.Base
open import Data.String.Base
open import Data.Vec.Base
open import Text.Tabular.Base
import Text.Tabular.List as Tabularˡ
import Text.Tabular.Vec as Tabularᵛ
------------------------------------------------------------------------
-- VEC
--
-- If you have a matrix of strings, you simply need to:
-- * pick a configuration (see below)
-- * pick an alignment for each column
-- * pass the matrix
--
-- The display function will then pad each string on the left, right,
-- or both to respect the alignment constraints.
-- It will return a list of strings corresponding to each line in the
-- table. You may then:
--- * use Data.String.Base's unlines to produce a String
-- * use Text.Pretty's text and vcat to produce a Doc (i.e. indentable!)
------------------------------------------------------------------------
_ : unlines (Tabularᵛ.display unicode
(Right ∷ Left ∷ Center ∷ [])
( ("foo" ∷ "bar" ∷ "baz" ∷ [])
∷ ("1" ∷ "2" ∷ "3" ∷ [])
∷ ("6" ∷ "5" ∷ "4" ∷ [])
∷ []))
≡ "┌───┬───┬───┐
\ \│foo│bar│baz│
\ \├───┼───┼───┤
\ \│ 1│2 │ 3 │
\ \├───┼───┼───┤
\ \│ 6│5 │ 4 │
\ \└───┴───┴───┘"
_ = refl
------------------------------------------------------------------------
-- CONFIG
--
-- Configurations allow you to change the way the table is displayed.
------------------------------------------------------------------------
-- We will use the same example throughout
foobar : Vec (Vec String 2) 3
foobar = ("foo" ∷ "bar" ∷ [])
∷ ("1" ∷ "2" ∷ [])
∷ ("4" ∷ "3" ∷ [])
∷ []
------------------------------------------------------------------------
-- Basic configurations: unicode, ascii, whitespace
-- unicode
_ : unlines (Tabularᵛ.display unicode
(Right ∷ Left ∷ [])
foobar)
≡ "┌───┬───┐
\ \│foo│bar│
\ \├───┼───┤
\ \│ 1│2 │
\ \├───┼───┤
\ \│ 4│3 │
\ \└───┴───┘"
_ = refl
-- ascii
_ : unlines (Tabularᵛ.display ascii
(Right ∷ Left ∷ [])
foobar)
≡ "+-------+
\ \|foo|bar|
\ \|---+---|
\ \| 1|2 |
\ \|---+---|
\ \| 4|3 |
\ \+-------+"
_ = refl
-- whitespace
_ : unlines (Tabularᵛ.display whitespace
(Right ∷ Left ∷ [])
foobar)
≡ "foo bar
\ \ 1 2
\ \ 4 3 "
_ = refl
------------------------------------------------------------------------
-- Modifiers: altering existing configurations
-- In these examples we will be using unicode as the base configuration.
-- However these modifiers apply to all configurations (and can even be
-- combined)
-- compact: drop the horizontal line between each row
_ : unlines (Tabularᵛ.display (compact unicode)
(Right ∷ Left ∷ [])
foobar)
≡ "┌───┬───┐
\ \│foo│bar│
\ \│ 1│2 │
\ \│ 4│3 │
\ \└───┴───┘"
_ = refl
-- noBorder: drop the outside borders
_ : unlines (Tabularᵛ.display (noBorder unicode)
(Right ∷ Left ∷ [])
foobar)
≡ "foo│bar
\ \───┼───
\ \ 1│2
\ \───┼───
\ \ 4│3 "
_ = refl
-- addSpace : add whitespace space inside cells
_ : unlines (Tabularᵛ.display (addSpace unicode)
(Right ∷ Left ∷ [])
foobar)
≡ "┌─────┬─────┐
\ \│ foo │ bar │
\ \├─────┼─────┤
\ \│ 1 │ 2 │
\ \├─────┼─────┤
\ \│ 4 │ 3 │
\ \└─────┴─────┘"
_ = refl
-- compact together with addSpace
_ : unlines (Tabularᵛ.display (compact (addSpace unicode))
(Right ∷ Left ∷ [])
foobar)
≡ "┌─────┬─────┐
\ \│ foo │ bar │
\ \│ 1 │ 2 │
\ \│ 4 │ 3 │
\ \└─────┴─────┘"
_ = refl
------------------------------------------------------------------------
-- LIST
--
-- Same thing as for vectors except that if the list of lists is not
-- rectangular, it is padded with empty strings to make it so. If there
-- are not enough alignment directives, we arbitrarily pick Left.
------------------------------------------------------------------------
_ : unlines (Tabularˡ.display unicode
(Center ∷ Right ∷ [])
( ("foo" ∷ "bar" ∷ [])
∷ ("partial" ∷ "rows" ∷ "are" ∷ "ok" ∷ [])
∷ ("3" ∷ "2" ∷ "1" ∷ "..." ∷ "surprise!" ∷ [])
∷ []))
≡ "┌───────┬────┬───┬───┬─────────┐
\ \│ foo │ bar│ │ │ │
\ \├───────┼────┼───┼───┼─────────┤
\ \│partial│rows│are│ok │ │
\ \├───────┼────┼───┼───┼─────────┤
\ \│ 3 │ 2│1 │...│surprise!│
\ \└───────┴────┴───┴───┴─────────┘"
_ = refl
------------------------------------------------------------------------
-- LIST (UNSAFE)
--
-- If you know *for sure* that your data is already perfectly rectangular
-- i.e. all the rows of the list of lists have the same length
-- in each column, all the strings have the same width
-- then you can use the unsafeDisplay function defined Text.Tabular.Base.
--
-- This is what gets used internally by `Text.Tabular.Vec` and
-- `Text.Tabular.List` once the potentially unsafe data has been
-- processed.
------------------------------------------------------------------------
_ : unlines (unsafeDisplay (compact unicode)
( ("foo" ∷ "bar" ∷ [])
∷ (" 1" ∷ " 2" ∷ [])
∷ (" 4" ∷ " 3" ∷ [])
∷ []))
≡ "┌───┬───┐
\ \│foo│bar│
\ \│ 1│ 2│
\ \│ 4│ 3│
\ \└───┴───┘"
_ = refl
|