Skip to content

Commit 2100a96

Browse files
authored
Merge pull request #56 from VariantSync/emojis
emojis for roundtrip sandwich
2 parents 3ef8441 + ec16e50 commit 2100a96

2 files changed

Lines changed: 45 additions & 15 deletions

File tree

src/Vatras/Show/Lines.agda

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ This module introduces our pretty printing monad.
33
-}
44
module Vatras.Show.Lines where
55

6-
open import Data.Bool using (true; false; if_then_else_)
6+
open import Data.Bool using (true; false; if_then_else_; _∧_)
7+
open import Data.Char as Char using (Char)
78
open import Data.Nat using (ℕ; _*_; _∸_; ⌊_/2⌋; ⌈_/2⌉; _≤ᵇ_)
89
open import Data.List as List using (List; _∷_; [_]; concat; splitAt; _∷ʳ_)
910
open import Data.Maybe using (nothing; just)
@@ -34,11 +35,40 @@ open Line public
3435
manipulate : (String String) Line Line
3536
manipulate f l = record l { content = f (content l) }
3637

37-
align : Line Line
38-
align width line = manipulate (fromAlignment (alignment line) width) line
38+
-- Rough approximation for how monospaced fonts are typically rendered.
39+
-- Only currently used characters are included so this will need to be extended
40+
-- if more/different symbols/emojis are used.
41+
charWidth : Char
42+
charWidth c =
43+
-- All the symbols starting at the Emoticons block.
44+
if (0x1f300 ≤ᵇ codePoint) ∧ (codePoint ≤ᵇ 0x1fbff)
45+
then 2
46+
else 1
47+
where
48+
codePoint = Char.toℕ c
49+
50+
stringLength : String
51+
stringLength line = List.sum (List.map charWidth (Data.String.toList line))
3952

4053
length : Line
41-
length line = Data.String.length (content line)
54+
length line = stringLength (content line)
55+
56+
-- Align the given line to have the given width.
57+
-- This will add spaces before and/or after the line depending on the line's alignment.
58+
-- also see: Data.String.Base.fromAlignment
59+
align : Line Line
60+
align width line =
61+
manipulate
62+
(fromAlignment
63+
(alignment line)
64+
-- We use the fromAlignment function of the standard library,
65+
-- which considers all characters to have width 1.
66+
-- If the line contains characters with width > 1 (e.g., emojis),
67+
-- then fromAlignment would add too much padding. So we have to
68+
-- decrease the padding value accordingly.
69+
(width ∸ (length line ∸ Data.String.length (content line)))
70+
)
71+
line
4272

4373
{-|
4474
Lines monad.
@@ -126,7 +156,7 @@ infix 1 >_
126156
infix 1 >∷_
127157

128158
phantom : String String
129-
phantom s = replicate (Data.String.length s) ' '
159+
phantom s = replicate (stringLength s) ' '
130160

131161
mantle : String String Lines Lines
132162
mantle prefix suffix = map (manipulate (λ s prefix ++ s ++ suffix))
@@ -188,7 +218,7 @@ boxed width title content =
188218
tr = '╮'
189219
br = '╯'
190220

191-
total-titlebar-len = width ∸ (Data.String.length title) ∸ 4 -- 2x whitespace + 2x corners
221+
total-titlebar-len = width ∸ (stringLength title) ∸ 4 -- 2x whitespace + 2x corners
192222
left-titlebar-len = ⌊ total-titlebar-len /2⌋
193223
right-titlebar-len = ⌈ total-titlebar-len /2⌉
194224

@@ -198,7 +228,7 @@ boxed width title content =
198228

199229
title-spacing = fromChar (if (title == "") then h else ' ')
200230
header = (replicate left-titlebar-len h) ++ title-spacing ++ title ++ title-spacing ++ (replicate right-titlebar-len h)
201-
footer = replicate (Data.String.length header) h
231+
footer = replicate (stringLength header) h
202232
in do
203233
-- print the header of the box
204234
> (fromChar tl) ++ header ++ (fromChar tr)

src/Vatras/Test/Experiments/RoundTrip.agda

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,23 +98,23 @@ ex-trivial = "trivial" ≔ "D" ⟨ "l" -< [] >- ∷ "r" -< [] >- ∷ [] ⟩
9898

9999
ex-sandwich : Example (CCC Feature ∞ Artifact)
100100
ex-sandwich = "Sandwich Recipe"
101-
"Bread"
101+
"🍞"
102102
-< "Salad?"
103-
"salad" -< [] >-
103+
"🥗" -< [] >-
104104
"ε" -< [] >-
105105
∷ []
106106
107-
"cheese" -< [] >-
107+
"🧀" -< [] >-
108108
"Patty?"
109-
"meat" -< [] >-
110-
"tofu" -< [] >-
109+
"🍖" -< [] >-
110+
"🧆" -< [] >-
111111
∷ []
112112
113113
"Sauce?"
114114
"ε" -< [] >-
115-
"mayo" -< [] >-
116-
"ketchup" -< [] >-
117-
"mayo+ketchup" -< [] >-
115+
"🥚" -< [] >-
116+
"🍅" -< [] >-
117+
"🍅🥚" -< [] >-
118118
∷ []
119119
120120
∷ []

0 commit comments

Comments
 (0)