From e17fa56246f857eea2b83d497ff740dbea2f5b6f Mon Sep 17 00:00:00 2001 From: defanor Date: Tue, 31 Oct 2017 22:50:16 +0300 Subject: Fix newline handling `fitWords` used to look for plain newlines, missing styled ones. --- Pancake.hs | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) (limited to 'Pancake.hs') diff --git a/Pancake.hs b/Pancake.hs index e78ab66..9647186 100644 --- a/Pancake.hs +++ b/Pancake.hs @@ -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 -- cgit v1.2.3