word-wrap 0.3.2 → 0.3.3
raw patch · 4 files changed
+31/−21 lines, 4 filesdep ~base
Dependency ranges changed: base
Files
- CHANGELOG.md +7/−0
- src/Text/Wrap.hs +22/−19
- tests/Main.hs +1/−1
- word-wrap.cabal +1/−1
CHANGELOG.md view
@@ -1,4 +1,11 @@ +0.3.3+=====++Bug fixes:+ * Fixed accidental breaking of long tokens when they could be wrapped+ instead.+ 0.3.2 =====
src/Text/Wrap.hs view
@@ -73,20 +73,20 @@ -- ^ A single line of text. -> [T.Text] wrapLine settings limit t =- let go [] = [T.empty]- go [WS _] = [T.empty]- go ts =- let (firstLine, maybeRest) = breakTokens settings limit ts+ let go _ [] = [T.empty]+ go _ [WS _] = [T.empty]+ go lim ts =+ let (firstLine, maybeRest) = breakTokens settings lim ts firstLineText = T.stripEnd $ T.concat $ fmap tokenContent firstLine in case maybeRest of Nothing -> [firstLineText]- Just rest ->- let maybeIndent = if preserveIndentation settings- then ((WS indent) :)- else id- indent = T.takeWhile isSpace firstLineText- in firstLineText : go (maybeIndent rest)- in go (tokenize t)+ Just rest -> firstLineText : go lim rest+ (indent, modifiedText) = if preserveIndentation settings+ then let i = T.takeWhile isSpace t+ in (i, T.drop (T.length i) t)+ else (T.empty, t)+ result = go (limit - T.length indent) (tokenize modifiedText)+ in (indent <>) <$> result -- | Break a token sequence so that all tokens up to but not exceeding -- a length limit are included on the left, and if any remain on the@@ -98,7 +98,14 @@ breakTokens settings limit ts = -- Take enough tokens until we reach the point where taking more -- would exceed the line length.- let go _ [] = ([], [])+ let go _ [] = ([], [])+ go 0 (t@(NonWS _):toks) =+ if tokenLength t <= limit+ then ([t], toks)+ else if not $ breakLongWords settings+ then ([t], toks)+ else let (h, tl) = T.splitAt limit (tokenContent t)+ in ([NonWS h], NonWS tl : toks) go acc (tok:toks) = if tokenLength tok + acc <= limit then let (nextAllowed, nextDisallowed) = go (acc + tokenLength tok) toks@@ -106,13 +113,9 @@ else case tok of WS _ -> ([], toks) NonWS _ ->- if not $ breakLongWords settings- then if acc == 0- then ([tok], toks)- else ([], tok:toks)- else let remaining = max 0 (limit - acc)- (h, tl) = T.splitAt remaining (tokenContent tok)- in ([NonWS h], tokenize tl <> toks)+ if acc == 0+ then ([tok], toks)+ else ([], tok:toks) -- Allowed tokens are the ones we keep on this line. The rest go -- on the next line, to be wrapped again.
tests/Main.hs view
@@ -40,7 +40,7 @@ it "breaks long non-whitespace tokens" $ do let s = defaultWrapSettings { breakLongWords = True } wrapTextToLines s 7 "HelloCrazyWorld!\nReallyLong Token"- `shouldBe` ["HelloCr", "azyWorl", "d!", "ReallyL", "ong Tok", "en"]+ `shouldBe` ["HelloCr", "azyWorl", "d!", "ReallyL", "ong", "Token"] it "breaks long non-whitespace tokens and indents" $ do let s = defaultWrapSettings { breakLongWords = True
word-wrap.cabal view
@@ -1,5 +1,5 @@ name: word-wrap-version: 0.3.2+version: 0.3.3 synopsis: A library for word-wrapping description: A library for wrapping long lines of text. license: BSD3