module Distribution.Pretty (
Pretty (..),
prettyShow,
defaultStyle,
flatStyle,
showFilePath,
showToken,
showFreeText,
showFreeTextV3,
Separator,
) where
import Data.Functor.Identity (Identity (..))
import Distribution.CabalSpecVersion
import Distribution.Compat.Prelude
import Prelude ()
import qualified Text.PrettyPrint as PP
class Pretty a where
pretty :: a -> PP.Doc
prettyVersioned :: CabalSpecVersion -> a -> PP.Doc
prettyVersioned CabalSpecVersion
_ = a -> Doc
forall a. Pretty a => a -> Doc
pretty
instance Pretty Bool where
pretty :: Bool -> Doc
pretty = String -> Doc
PP.text (String -> Doc) -> (Bool -> String) -> Bool -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> String
forall a. Show a => a -> String
show
instance Pretty Int where
pretty :: Int -> Doc
pretty = String -> Doc
PP.text (String -> Doc) -> (Int -> String) -> Int -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> String
forall a. Show a => a -> String
show
instance Pretty a => Pretty (Identity a) where
pretty :: Identity a -> Doc
pretty = a -> Doc
forall a. Pretty a => a -> Doc
pretty (a -> Doc) -> (Identity a -> a) -> Identity a -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Identity a -> a
forall a. Identity a -> a
runIdentity
prettyShow :: Pretty a => a -> String
prettyShow :: a -> String
prettyShow = Style -> Doc -> String
PP.renderStyle Style
defaultStyle (Doc -> String) -> (a -> Doc) -> a -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Doc
forall a. Pretty a => a -> Doc
pretty
defaultStyle :: PP.Style
defaultStyle :: Style
defaultStyle = Style :: Mode -> Int -> Float -> Style
PP.Style { mode :: Mode
PP.mode = Mode
PP.PageMode
, lineLength :: Int
PP.lineLength = Int
79
, ribbonsPerLine :: Float
PP.ribbonsPerLine = Float
1.0
}
flatStyle :: PP.Style
flatStyle :: Style
flatStyle = Style :: Mode -> Int -> Float -> Style
PP.Style { mode :: Mode
PP.mode = Mode
PP.LeftMode
, lineLength :: Int
PP.lineLength = String -> Int
forall a. String -> a
err String
"lineLength"
, ribbonsPerLine :: Float
PP.ribbonsPerLine = String -> Float
forall a. String -> a
err String
"ribbonsPerLine"
}
where
err :: String -> a
err String
x = String -> a
forall a. HasCallStack => String -> a
error (String
"flatStyle: tried to access " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
x String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" in LeftMode. " String -> String -> String
forall a. [a] -> [a] -> [a]
++
String
"This should never happen and indicates a bug in Cabal.")
type Separator = [PP.Doc] -> PP.Doc
showFilePath :: FilePath -> PP.Doc
showFilePath :: String -> Doc
showFilePath = String -> Doc
showToken
showToken :: String -> PP.Doc
showToken :: String -> Doc
showToken String
str
| String
"--" String -> String -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isPrefixOf` String
str = String -> Doc
PP.text (String -> String
forall a. Show a => a -> String
show String
str)
| String
":" String -> String -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isSuffixOf` String
str = String -> Doc
PP.text (String -> String
forall a. Show a => a -> String
show String
str)
| Bool -> Bool
not ((Char -> Bool) -> String -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any Char -> Bool
dodgy String
str) Bool -> Bool -> Bool
&& Bool -> Bool
not (String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
str) = String -> Doc
PP.text String
str
| Bool
otherwise = String -> Doc
PP.text (String -> String
forall a. Show a => a -> String
show String
str)
where
dodgy :: Char -> Bool
dodgy Char
c = Char -> Bool
isSpace Char
c Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
','
showFreeText :: String -> PP.Doc
showFreeText :: String -> Doc
showFreeText String
"" = Doc
forall a. Monoid a => a
mempty
showFreeText String
s = [Doc] -> Doc
PP.vcat [ String -> Doc
PP.text (if String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
l then String
"." else String
l) | String
l <- String -> [String]
lines_ String
s ]
showFreeTextV3 :: String -> PP.Doc
showFreeTextV3 :: String -> Doc
showFreeTextV3 String
"" = Doc
forall a. Monoid a => a
mempty
showFreeTextV3 String
s = [Doc] -> Doc
PP.vcat [ String -> Doc
PP.text String
l | String
l <- String -> [String]
lines_ String
s ]
lines_ :: String -> [String]
lines_ :: String -> [String]
lines_ [] = [String
""]
lines_ String
s =
let (String
l, String
s') = (Char -> Bool) -> String -> (String, String)
forall a. (a -> Bool) -> [a] -> ([a], [a])
break (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\n') String
s
in String
l String -> [String] -> [String]
forall a. a -> [a] -> [a]
: case String
s' of
[] -> []
(Char
_:String
s'') -> String -> [String]
lines_ String
s''