word-wrap 0.2 → 0.3
raw patch · 4 files changed
+27/−6 lines, 4 files
Files
- CHANGELOG.md +8/−0
- src/Text/Wrap.hs +11/−3
- tests/Main.hs +7/−2
- word-wrap.cabal +1/−1
CHANGELOG.md view
@@ -1,4 +1,12 @@ +0.3+===++API changes:+ * Added the breakLongWords setting to WrapSettings. This setting makes+ it possible to cause words to get broken up over multiple lines if+ their lengths exceed the wrapping width.+ 0.2 ===
src/Text/Wrap.hs view
@@ -15,12 +15,16 @@ WrapSettings { preserveIndentation :: Bool -- ^ Whether to indent new lines created by wrapping -- when their original line was indented.+ , breakLongWords :: Bool+ -- ^ Whether to break in the middle of individual words+ -- when not doing so would result in a long line. } deriving (Eq, Show, Read) defaultWrapSettings :: WrapSettings defaultWrapSettings = WrapSettings { preserveIndentation = False+ , breakLongWords = False } -- | Wrap text at the specified width. Newlines and whitespace in the@@ -41,8 +45,7 @@ deriving (Show) tokenLength :: Token -> Int-tokenLength (WS t) = T.length t-tokenLength (NonWS t) = T.length t+tokenLength = T.length . tokenContent tokenContent :: Token -> T.Text tokenContent (WS t) = t@@ -71,7 +74,12 @@ wrapLine settings limit t = let go [] = [T.empty] go [WS _] = [T.empty]- go [tok] = [tokenContent tok]+ go [tok] = if breakLongWords settings+ then let (h, tl) = T.splitAt limit $ tokenContent tok+ in if T.null tl+ then [h]+ else h : go [NonWS tl]+ else [tokenContent tok] go ts = let (firstLine, maybeRest) = breakTokens limit ts firstLineText = T.stripEnd $ T.concat $ fmap tokenContent firstLine
tests/Main.hs view
@@ -28,11 +28,16 @@ `shouldBe` ["Hello,", "World!"] it "preserves indentation" $ do- let s = WrapSettings { preserveIndentation = True }+ let s = defaultWrapSettings { preserveIndentation = True } wrapTextToLines s 10 " Hello, World!" `shouldBe` [" Hello,", " World!"] it "preserves indentation (2)" $ do- let s = WrapSettings { preserveIndentation = True }+ let s = defaultWrapSettings { preserveIndentation = True } wrapTextToLines s 10 " Hello, World!\n Things And Stuff" `shouldBe` [" Hello,", " World!", " Things", " And", " Stuff"]++ it "breaks long non-whitespace tokens" $ do+ let s = defaultWrapSettings { breakLongWords = True }+ wrapTextToLines s 7 "HelloCrazyWorld!"+ `shouldBe` ["HelloCr", "azyWorl", "d!"]
word-wrap.cabal view
@@ -1,5 +1,5 @@ name: word-wrap-version: 0.2+version: 0.3 synopsis: A library for word-wrapping description: A library for wrapping long lines of text. license: BSD3