diff options
author | Ben Sima <ben@bsima.me> | 2020-12-23 23:34:05 -0500 |
---|---|---|
committer | Ben Sima <ben@bsima.me> | 2020-12-23 23:34:16 -0500 |
commit | bfb4911bbddc5512a931aa90cac59168501cafef (patch) | |
tree | c8f0a7a19b4a3901df532c051df12454dbcd0ca5 | |
parent | 1927461c3a649a7696fc1749fb319ff31efab241 (diff) |
Add left-compose and text wrap utils
-rw-r--r-- | Alpha.hs | 15 |
1 files changed, 15 insertions, 0 deletions
@@ -25,6 +25,7 @@ module Alpha -- * Composing compose, (.>), + (<.), -- * Applying (<|), @@ -43,6 +44,7 @@ module Alpha lchomp, joinWith, CanSnakeCase (snake), + wrap, -- * String capitalize, @@ -148,3 +150,16 @@ lowercase str = [Char.toLower c | c <- str] require :: Text -> Maybe a -> a require _ (Just x) = x require s Nothing = panic <| s <> " not found" + +-- | Wrap text at the given limit. +wrap :: Int -> Text -> Text +wrap lim = Text.unwords . wrap_ 0 . Text.words + where + wrap_ :: Int -> [Text] -> [Text] + wrap_ _ [] = [] + wrap_ pos (w:ws) + | pos == 0 = w : (wrap_ (pos + lw) ws) + | pos + lw + 1 > lim = wrap_ 0 (Text.cons '\n' w : ws) + | otherwise = [w] ++ wrap_ (pos + lw + 1) ws + where + lw = Text.length w |