pretty-types 0.2.0.0 → 0.2.1.0
raw patch · 4 files changed
+151/−2 lines, 4 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- ChangeLog.md +6/−0
- pretty-types.cabal +1/−1
- spec/PrettyTypesSpec.hs +42/−0
- src/Data/Type/Pretty.hs +102/−1
ChangeLog.md view
@@ -1,3 +1,9 @@+## Version 0.2.1.0++* Added ToPretty instances for type level strings and numbers+* Added ToPretty instances for types of kind Maybe and Bool+* Added ToPretty instances for some standard number types (Word*, Int*)+ ## Version 0.2.0.0 * Added indentation support
pretty-types.cabal view
@@ -1,5 +1,5 @@ name: pretty-types-version: 0.2.0.0+version: 0.2.1.0 synopsis: A small pretty printing DSL for complex types. description: Please see README.md homepage: https://github.com/sheyll/pretty-types#readme
spec/PrettyTypesSpec.hs view
@@ -6,6 +6,8 @@ import GHC.TypeLits import Data.Kind import Data.Proxy+import Data.Word+import Data.Int main :: IO () main = hspec spec@@ -141,6 +143,46 @@ `shouldBe` "foo\n bar1\n bar2\n bar3\n bar4" describe "ToPretty" $ do+ it "renders a type level symbol like PutStr" $+ showPretty (PX :: PX (ToPretty "foo")) `shouldBe` showPretty (PX :: PX (PutStr "foo"))+ it "renders a type level natural like PutNat" $+ showPretty (PX :: PX (ToPretty 123)) `shouldBe` showPretty (PX :: PX (PutNat 123))+ it "renders a type level symbol like PutStr" $+ showPretty (PX :: PX (ToPretty "foo")) `shouldBe` showPretty (PX :: PX (PutStr "foo"))+ it "renders 'Nothing to empty document" $+ showPretty (PX :: PX (ToPretty 'Nothing)) `shouldBe` ""+ it "renders 'Just x to ToPretty x" $+ showPretty (PX :: PX (ToPretty ('Just "test"))) `shouldBe` "test"+ it "renders Word8 as PutStr \"Word8\"" $+ showPretty (PX :: PX (ToPretty Word8)) `shouldBe` "Word8"+ it "renders Word16 as PutStr \"Word16\"" $+ showPretty (PX :: PX (ToPretty Word16)) `shouldBe` "Word16"+ it "renders Word32 as PutStr \"Word32\"" $+ showPretty (PX :: PX (ToPretty Word32)) `shouldBe` "Word32"+ it "renders Word64 as PutStr \"Word64\"" $+ showPretty (PX :: PX (ToPretty Word64)) `shouldBe` "Word64"+ it "renders Int8 as PutStr \"Int8\"" $+ showPretty (PX :: PX (ToPretty Int8)) `shouldBe` "Int8"+ it "renders Int16 as PutStr \"Int16\"" $+ showPretty (PX :: PX (ToPretty Int16)) `shouldBe` "Int16"+ it "renders Int32 as PutStr \"Int32\"" $+ showPretty (PX :: PX (ToPretty Int32)) `shouldBe` "Int32"+ it "renders Int64 as PutStr \"Int64\"" $+ showPretty (PX :: PX (ToPretty Int64)) `shouldBe` "Int64"+ it "renders Int as PutStr \"Int\"" $+ showPretty (PX :: PX (ToPretty Int)) `shouldBe` "Int"+ it "renders Integer as PutStr \"Integer\"" $+ showPretty (PX :: PX (ToPretty Integer)) `shouldBe` "Integer"+ it "renders Bool as PutStr \"Bool\"" $+ showPretty (PX :: PX (ToPretty Bool)) `shouldBe` "Bool"+ it "renders Float as PutStr \"Float\"" $+ showPretty (PX :: PX (ToPretty Float)) `shouldBe` "Float"+ it "renders Double as PutStr \"Double\"" $+ showPretty (PX :: PX (ToPretty Double)) `shouldBe` "Double"+ it "renders 'True as PutStr \"'True\"" $+ showPretty (PX :: PX (ToPretty 'True)) `shouldBe` "'True"+ it "renders 'False as PutStr \"'False\"" $+ showPretty (PX :: PX (ToPretty 'False)) `shouldBe` "'False" it "renders a custom type" $ do putStrLn prettyTestTable showPretty (PX :: PX TestTable) `shouldBe` "+-------+-----+------------+\n| col 1|col 2| col 3|\n+-------+-----+------------+\n| 2423| 451| 21234|\n| 242322| 42| n/a|\n| 0| 4351| 623562|\n| 4351| n/a| n/a|\n| 0| 4351| 623562|\n+-------+-----+------------+"
src/Data/Type/Pretty.hs view
@@ -109,6 +109,8 @@ import GHC.TypeLits import Data.Proxy import Text.Printf+import Data.Word+import Data.Int -- * Pretty Printing Types @@ -131,6 +133,88 @@ -- | A type of kind 'PrettyType' has a trivial @id@-like'ToPretty' instance. type instance ToPretty (t :: PrettyType) = t +-- ** ToPretty instances for uninhabited types++-- | A type of kind 'Symbol' is translated to 'PutStr'.+-- @since 0.2.1.0+type instance ToPretty (t :: Symbol) = PutStr t++-- | A type of kind 'Nat' is translated to 'PutNat'.+-- @since 0.2.1.0+type instance ToPretty (t :: Nat) = PutNat t++-- | Render 'True' as @'PutStr' "'True"@+-- @since 0.2.1.0+type instance ToPretty 'True = PutStr "'True"++-- | Render 'False' as @'PutStr' "'False"@+-- @since 0.2.1.0+type instance ToPretty 'False = PutStr "'False"++-- | Render a type of kind 'Maybe'.+-- @since 0.2.1.0+type instance ToPretty (t :: Maybe x) = ToPrettyMaybe t++-- | Render a type of kind 'Maybe'.+-- @since 0.2.1.0+type family ToPrettyMaybe (t :: Maybe x) :: PrettyType where+ ToPrettyMaybe 'Nothing = 'PrettyEmpty+ ToPrettyMaybe ('Just x) = ToPretty x++-- ** ToPretty instances for inhabited types++-- | Render 'Word8' as @'PutStr' "Word8"@+-- @since 0.2.1.0+type instance ToPretty Word8 = PutStr "Word8"++-- | Render 'Word16' as @'PutStr' "Word16"@+-- @since 0.2.1.0+type instance ToPretty Word16 = PutStr "Word16"++-- | Render 'Word32' as @'PutStr' "Word32"@+-- @since 0.2.1.0+type instance ToPretty Word32 = PutStr "Word32"++-- | Render 'Word64' as @'PutStr' "Word64"@+-- @since 0.2.1.0+type instance ToPretty Word64 = PutStr "Word64"++-- | Render 'Int8' as @'PutStr' "Int8"@+-- @since 0.2.1.0+type instance ToPretty Int8 = PutStr "Int8"++-- | Render 'Int16' as @'PutStr' "Int16"@+-- @since 0.2.1.0+type instance ToPretty Int16 = PutStr "Int16"++-- | Render 'Int32' as @'PutStr' "Int32"@+-- @since 0.2.1.0+type instance ToPretty Int32 = PutStr "Int32"++-- | Render 'Int64' as @'PutStr' "Int64"@+-- @since 0.2.1.0+type instance ToPretty Int64 = PutStr "Int64"++-- | Render 'Int' as @'PutStr' "Int"@+-- @since 0.2.1.0+type instance ToPretty Int = PutStr "Int"++-- | Render 'Integer' as @'PutStr' "Integer"@+-- @since 0.2.1.0+type instance ToPretty Integer = PutStr "Integer"++-- | Render 'Bool' as @'PutStr' "Bool"@+-- @since 0.2.1.0+type instance ToPretty Bool = PutStr "Bool"++-- | Render 'Float' as @'PutStr' "Float"@+-- @since 0.2.1.0+type instance ToPretty Float = PutStr "Float"++-- | Render 'Double' as @'PutStr' "Double"@+-- @since 0.2.1.0+type instance ToPretty Double = PutStr "Double"+ -- * Pretty Printing -- ** Pretty Printing Strings ('Symbol')@@ -213,6 +297,7 @@ -- ** Composing Pretty Printers -- | A label followed by a colon and space @: @ followed by another element.+-- @since 0.2.0.0 -- -- >>> showPretty (Proxy :: Proxy ("foo" <:> PutStr "bar")) -- @@@ -221,6 +306,7 @@ type (<:>) label body = 'PrettySuffix (PutStr ":") (PutStr label) <+> body infixl 5 <:> -- | Like '<:>' but begin the body on a new line.+-- @since 0.2.0.0 -- -- >>> showPretty (Proxy :: Proxy (PutStr "foo" <:$$> PutStr "bar")) -- @@@ -231,6 +317,7 @@ infixl 5 <:$$> -- | Like '<:$$__>' but indent the body with two spaces.+-- @since 0.2.0.0 -- -- >>> showPretty (Proxy :: Proxy (PutStr "foo" <:$$--> PutStr "bar")) -- @@@ -249,6 +336,7 @@ infixl 5 <+> -- | Choose the first non-empty from two 'PrettyType's.+-- @since 0.2.0.0 type (<||>) l r = 'PrettyAlternative l r infixl 5 <||> @@ -257,6 +345,7 @@ infixl 4 <$$> -- | Concatenate two 'PrettyType' using a 'PrettyNewline' and indent the second.+-- @since 0.2.0.0 type (<$$-->) l r = 'PrettyInfix 'PrettyNewline l ('PrettyIndent 2 r) infixl 3 <$$--> @@ -314,14 +403,18 @@ PrettySymbol :: PrettyPadded -> PrettyPrecision -> Symbol -> PrettyType PrettyNat :: PrettyPadded -> PrettyPrecision -> PrettyNatFormat -> Nat -> PrettyType -- | Prefix the second with the first argument, but only if it (the second) has content.+ -- @since 0.2.0.0 PrettyPrefix :: PrettyType -> PrettyType -> PrettyType -- | Combine the last to arguments with the first in between them, but only if both have content. PrettyInfix :: PrettyType -> PrettyType -> PrettyType -> PrettyType -- | Add a the first argument as suffix to the second argument, but only if the second has content.+ -- @since 0.2.0.0 PrettySuffix :: PrettyType -> PrettyType -> PrettyType -- | Indentation. Prefix any line using the given number of 'PrettySpace'.+ -- @since 0.2.0.0 PrettyIndent :: Nat -> PrettyType -> PrettyType -- | Alternative rendering, if the first document ist empty the second will be rendered.+ -- @since 0.2.0.0 PrettyAlternative :: PrettyType -> PrettyType -> PrettyType -- | Padding for 'PrettyType's 'PrettySymbol' and 'PrettyNat'.@@ -375,13 +468,16 @@ -- String. ptShow :: proxy p -> PTM () -- | Return 'True' if contents would be writting to the output of rendered via 'ptShow'+ -- @since 0.2.0.0 ptHasContent :: proxy p -> PTM Bool ptHasContent _ = return True --- | Internal monad used by 'ptShow', the state is a 'Bool' indicating+-- | The monad used by 'ptShow' to keep track of indentation.+-- @since 0.2.0.0 type PTM a = RWS Indentation String PTRenderState a -- | Internal; write a possibly indented string, and update the 'PTRenderState' accordingly.+-- @since 0.2.0.0 writeIndented :: String -> PTM () writeIndented s = do st <- get@@ -394,9 +490,11 @@ Control.Monad.RWS.tell s -- | Internal type of the indentation used by 'ptShow' in 'PTM'+-- @since 0.2.0.0 type Indentation = Int -- | Internal state used by 'ptShow' in 'PTM'+-- @since 0.2.0.0 data PTRenderState = AtBeginningOfLine | AlreadyIndented -- | Print nothing.@@ -462,6 +560,7 @@ ptHasContent _ = ptHasContent (Proxy :: Proxy x) -- | Add a 'PrettyType' suffix to x, but only if 'ptHasContent' holds.+-- @since 0.2.0.0 instance forall x sep . (PrettyTypeShow sep, PrettyTypeShow x) => PrettyTypeShow ('PrettySuffix sep x) where ptShow _ = do@@ -473,6 +572,7 @@ ptHasContent _ = ptHasContent (Proxy :: Proxy x) -- | Render the first document, and if it is empty, the second+-- @since 0.2.0.0 instance forall l r . (PrettyTypeShow l, PrettyTypeShow r) => PrettyTypeShow ('PrettyAlternative l r) where ptShow _ = do@@ -487,6 +587,7 @@ -- | Render an indented, nested type.+-- @since 0.2.0.0 instance forall n r . (PrettyTypeShow r, KnownNat n) => PrettyTypeShow ('PrettyIndent n r) where