module Distribution.Text (
Text(..),
defaultStyle,
display,
flatStyle,
simpleParse,
stdParse,
) where
import Prelude ()
import Distribution.Compat.Prelude
import qualified Distribution.Compat.ReadP as Parse
import qualified Text.PrettyPrint as Disp
import Data.Version (Version(Version))
class Text a where
disp :: a -> Disp.Doc
parse :: Parse.ReadP r a
defaultStyle :: Disp.Style
defaultStyle = Disp.Style { Disp.mode = Disp.PageMode
, Disp.lineLength = 79
, Disp.ribbonsPerLine = 1.0
}
display :: Text a => a -> String
display = Disp.renderStyle defaultStyle . disp
flatStyle :: Disp.Style
flatStyle = Disp.Style { Disp.mode = Disp.LeftMode
, Disp.lineLength = err "lineLength"
, Disp.ribbonsPerLine = err "ribbonsPerLine"
}
where
err x = error ("flatStyle: tried to access " ++ x ++ " in LeftMode. " ++
"This should never happen and indicates a bug in Cabal.")
simpleParse :: Text a => String -> Maybe a
simpleParse str = case [ p | (p, s) <- Parse.readP_to_S parse str
, all isSpace s ] of
[] -> Nothing
(p:_) -> Just p
stdParse :: Text ver => (ver -> String -> res) -> Parse.ReadP r res
stdParse f = do
cs <- Parse.sepBy1 component (Parse.char '-')
_ <- Parse.char '-'
ver <- parse
let name = intercalate "-" cs
return $! f ver (lowercase name)
where
component = do
cs <- Parse.munch1 isAlphaNum
if all isDigit cs then Parse.pfail else return cs
lowercase :: String -> String
lowercase = map toLower
instance Text Bool where
disp = Disp.text . show
parse = Parse.choice [ (Parse.string "True" Parse.+++
Parse.string "true") >> return True
, (Parse.string "False" Parse.+++
Parse.string "false") >> return False ]
instance Text Int where
disp = Disp.text . show
parse = (fmap negate $ Parse.char '-' >> parseNat) Parse.+++ parseNat
parseNat :: Parse.ReadP r Int
parseNat = read `fmap` Parse.munch1 isDigit
instance Text Version where
disp (Version branch _tags)
= Disp.hcat (Disp.punctuate (Disp.char '.') (map Disp.int branch))
parse = do
branch <- Parse.sepBy1 parseNat (Parse.char '.')
_tags <- Parse.many (Parse.char '-' >> Parse.munch1 isAlphaNum)
return (Version branch [])