module Darcs.Util.Text
(
sentence
, formatText
, formatParas
, formatPara
, chompTrailingNewline
, breakCommand
, quote
, pathlist
) where
import Prelude ()
import Darcs.Prelude
import Control.Arrow ( first )
import Data.List ( intercalate )
import Darcs.Util.Printer ( Doc, (<>), renderString, RenderMode(..), quoted, hsep )
sentence :: Doc -> Doc
sentence = (<> ".")
formatText :: Int -> [String] -> String
formatText linelen = unlines . formatParas linelen
formatParas :: Int -> [String] -> [String]
formatParas linelen = intercalate [""] .
map (map unwords . formatPara linelen . words)
formatPara :: Int -> [[a]] -> [[[a]]]
formatPara w = para'
where para' [] = []
para' xs = uncurry (:) $ para'' w xs
para'' r (x:xs) | w == r || length x < r = first (x:) $ para'' (r length x 1) xs
para'' _ xs = ([], para' xs)
breakCommand :: String -> (String, [String])
breakCommand s = case words s of
(arg0:args) -> (arg0,args)
[] -> (s,[])
chompTrailingNewline :: String -> String
chompTrailingNewline "" = ""
chompTrailingNewline s = if last s == '\n' then init s else s
quote :: String -> String
quote = renderString Encode . quoted
pathlist :: [FilePath] -> Doc
pathlist paths = hsep (map quoted paths)