module Darcs.Util.Text
(
sentence
, formatText
, formatParas
, formatPara
, chompTrailingNewline
, breakCommand
, quote
, pathlist
, showCommandLine
) where
import Prelude ()
import Darcs.Prelude
import Control.Arrow ( first )
import Data.List ( intercalate )
import Darcs.Util.Printer ( Doc, (<>), renderString, 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 . quoted
pathlist :: [FilePath] -> Doc
pathlist paths = hsep (map quoted paths)
showCommandLine :: [String] -> String
showCommandLine strings = showCommandLine' ['"'] strings
where showCommandLine' x xs =
x ++ intercalate (x ++ " " ++ x) xs ++ x