Skip to content

Commit

Permalink
make strWidth aware of multi-line strings (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmichael committed Oct 10, 2015
1 parent 1d944ac commit 155722d
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions hledger-lib/Hledger/Utils/String.hs
Expand Up @@ -336,14 +336,17 @@ takeWidth w (c:cs) | cw <= w = c:takeWidth (w-cw) cs
-- from Pandoc (copyright John MacFarlane, GPL)
-- see also http://unicode.org/reports/tr11/#Description

-- | Get real length of string, taking into account combining and
-- double-width characters.
-- | Calculate the designated render width of a string, taking into
-- account wide characters and line breaks (the longest line within a
-- multi-line string determines the width ).
strWidth :: String -> Int
strWidth = foldr (\a b -> charWidth a + b) 0
strWidth "" = 0
strWidth s = maximum $ map (foldr (\a b -> charWidth a + b) 0) $ lines s

-- | Returns the width of a character in a monospace font: 0 for a
-- combining character, 1 for a regular character, 2 for an East Asian
-- wide character.
-- | Get the designated render width of a character: 0 for a combining
-- character, 1 for a regular character, 2 for a wide character.
-- (Wide characters are rendered as exactly double width in apps and
-- fonts that support it.)
charWidth :: Char -> Int
charWidth c =
case c of
Expand Down

0 comments on commit 155722d

Please sign in to comment.