Skip to content

Commit b945134

Browse files
committed
Treat emojis as two characters wide
Many terminals (e.g., kitty and alacritty) render emojis as two characters wide. This messed up the layout of all the boxes.
1 parent 73c7e06 commit b945134

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

src/Vatras/Show/Lines.agda

Lines changed: 23 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,26 @@ 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 : Line Line
57+
align width line = manipulate (fromAlignment (alignment line) (width ∸ (length line ∸ Data.String.length (content line)))) line
4258

4359
{-|
4460
Lines monad.
@@ -126,7 +142,7 @@ infix 1 >_
126142
infix 1 >∷_
127143

128144
phantom : String String
129-
phantom s = replicate (Data.String.length s) ' '
145+
phantom s = replicate (stringLength s) ' '
130146

131147
mantle : String String Lines Lines
132148
mantle prefix suffix = map (manipulate (λ s prefix ++ s ++ suffix))
@@ -188,7 +204,7 @@ boxed width title content =
188204
tr = '╮'
189205
br = '╯'
190206

191-
total-titlebar-len = width ∸ (Data.String.length title) ∸ 4 -- 2x whitespace + 2x corners
207+
total-titlebar-len = width ∸ (stringLength title) ∸ 4 -- 2x whitespace + 2x corners
192208
left-titlebar-len = ⌊ total-titlebar-len /2⌋
193209
right-titlebar-len = ⌈ total-titlebar-len /2⌉
194210

@@ -198,7 +214,7 @@ boxed width title content =
198214

199215
title-spacing = fromChar (if (title == "") then h else ' ')
200216
header = (replicate left-titlebar-len h) ++ title-spacing ++ title ++ title-spacing ++ (replicate right-titlebar-len h)
201-
footer = replicate (Data.String.length header) h
217+
footer = replicate (stringLength header) h
202218
in do
203219
-- print the header of the box
204220
> (fromChar tl) ++ header ++ (fromChar tr)

0 commit comments

Comments
 (0)