diff options
author | defanor <defanor@uberspace.net> | 2017-10-31 22:50:16 +0300 |
---|---|---|
committer | defanor <defanor@uberspace.net> | 2017-10-31 22:50:16 +0300 |
commit | e17fa56246f857eea2b83d497ff740dbea2f5b6f (patch) | |
tree | 408d4ff28ee49f17b8d083a01bf72545b2953add /Pancake.hs | |
parent | 097ac68877acfa6d35ec3987b80ee1ae8da4ff62 (diff) |
Fix newline handling
`fitWords` used to look for plain newlines, missing styled ones.
Diffstat (limited to 'Pancake.hs')
-rw-r--r-- | Pancake.hs | 33 |
1 files changed, 17 insertions, 16 deletions
@@ -277,22 +277,23 @@ fitLines :: Int fitLines maxLen inlineBits = map mconcat $ map reverse $ fitWords [] 0 inlineBits where - -- handle newline characters - fitWords curLine _ ("\n":ws) = curLine : fitWords [] 0 ws - -- a new line - fitWords _ 0 (w:ws) = fitWords [w] (length $ uncolored w) ws - -- add a word to a line - fitWords curLine curLen (w:ws) = let wLen = length (uncolored w) - spaceAhead = case ws of - (" " : _) -> True - _ -> False - in if curLen + wLen <= maxLen - then fitWords (w:curLine) (curLen + wLen) $ - -- if there's an unnecessary space ahead, skip it - if (curLen + wLen + 1 > maxLen && spaceAhead) - then tail ws - else ws - else curLine : fitWords [] 0 (w:ws) + fitWords curLine curLen (w:ws) + -- handle newline characters + | uncolored w == "\n" = curLine : fitWords [] 0 ws + -- a new line + | curLen == 0 = fitWords [w] (length $ uncolored w) ws + -- add a word to a line + | otherwise = let wLen = length (uncolored w) + spaceAhead = case ws of + (" " : _) -> True + _ -> False + in if curLen + wLen <= maxLen + then fitWords (w:curLine) (curLen + wLen) $ + -- if there's an unnecessary space ahead, skip it + if (curLen + wLen + 1 > maxLen && spaceAhead) + then tail ws + else ws + else curLine : fitWords [] 0 (w:ws) -- end, no words pending fitWords _ 0 [] = [] -- end, with words pending |