text-printer 0.4 → 0.5
raw patch · 5 files changed
+169/−32 lines, 5 filesdep ~basedep ~bytestringdep ~semigroups
Dependency ranges changed: base, bytestring, semigroups
Files
- README.md +14/−0
- src/Text/Printer.hs +96/−21
- src/Text/Printer/Fractional.hs +12/−1
- src/Text/Printer/Integral.hs +37/−6
- text-printer.cabal +10/−4
+ README.md view
@@ -0,0 +1,14 @@+Text-Printer+============++[](https://travis-ci.org/mvv/text-printer) [](http://hackage.haskell.org/package/text-printer)++This package provides an interface for injecting text into monoids such as+builders and printers.++Installation+------------+The usual:++ $ cabal install+
src/Text/Printer.hs view
@@ -2,6 +2,9 @@ {-# LANGUAGE UnicodeSyntax #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE DeriveDataTypeable #-}+#if __GLASGOW_HASKELL__ >= 706+{-# LANGUAGE DeriveGeneric #-}+#endif {-# LANGUAGE TypeSynonymInstances #-} {-# LANGUAGE FlexibleInstances #-} @@ -21,6 +24,8 @@ , Utf8Builder(..) , buildUtf8 , buildLazyUtf8+ , PrettyPrinter(..)+ , renderPretty -- * Combinators , (<>) , hcat@@ -49,10 +54,17 @@ ) where import Prelude hiding (foldr, foldr1, print, lines)+#if __GLASGOW_HASKELL__ >= 706+import GHC.Generics (Generic)+#endif import Data.Typeable (Typeable) import Data.String (IsString(..))+import Data.Semigroup (Semigroup) import qualified Data.Semigroup as S-import Data.Monoid (Monoid(..), (<>))+import Data.Monoid (Monoid(..))+#if MIN_VERSION_base(4,5,0)+import Data.Monoid ((<>))+#endif import Data.Foldable (Foldable(..), toList) import Data.Traversable (Traversable, mapAccumL, mapAccumR) import qualified Data.Text as TS@@ -73,7 +85,7 @@ -- Other operations must be monoid homomorphisms that are eqiuvalent (but -- possibly faster) to the composition of 'string' and the corresponding -- embedding, e.g. @'text' = 'string' . 'TS.unpack'@.-class (IsString p, Monoid p) ⇒ Printer p where+class (IsString p, Semigroup p, Monoid p) ⇒ Printer p where -- | Print a character. @'char' /c/@ must be equivalent to -- @'string' [/c/]@, but hopefully is faster. char ∷ Char → p@@ -120,16 +132,17 @@ -- | A simple string builder as used by 'Show'. newtype StringBuilder = StringBuilder { stringBuilder ∷ String → String }- deriving (Typeable, Monoid)+ deriving ( Typeable+#if __GLASGOW_HASKELL__ >= 706+ , Generic+#endif+ , Semigroup+ , Monoid) instance IsString StringBuilder where fromString s = StringBuilder (s ++) {-# INLINE fromString #-} -instance S.Semigroup StringBuilder where- (<>) = mappend- {-# INLINE (<>) #-}- instance Printer StringBuilder where char c = StringBuilder (c :) {-# INLINE char #-}@@ -157,15 +170,21 @@ -- | Use this builder when you are sure that only ASCII characters -- will get printed to it. newtype AsciiBuilder = AsciiBuilder { asciiBuilder ∷ BB.Builder }- deriving (Typeable, Monoid)+ deriving ( Typeable+#if __GLASGOW_HASKELL__ >= 706+ , Generic+#endif+ , Monoid) instance IsString AsciiBuilder where fromString = AsciiBuilder . BB.string7 {-# INLINE fromString #-} -instance S.Semigroup AsciiBuilder where- (<>) = mappend+instance Semigroup AsciiBuilder where+ b₁ <> b₂ = AsciiBuilder $ asciiBuilder b₁ <> asciiBuilder b₂ {-# INLINE (<>) #-}+ stimes = S.stimesMonoid+ {-# INLINE stimes #-} instance Printer AsciiBuilder where char = AsciiBuilder . BB.char7@@ -189,15 +208,21 @@ -- | UTF-8 lazy 'BL.ByteString' builder. newtype Utf8Builder = Utf8Builder { utf8Builder ∷ BB.Builder }- deriving (Typeable, Monoid)+ deriving ( Typeable+#if __GLASGOW_HASKELL__ >= 706+ , Generic+#endif+ , Monoid) instance IsString Utf8Builder where fromString = Utf8Builder . BB.stringUtf8 {-# INLINE fromString #-} -instance S.Semigroup Utf8Builder where- (<>) = mappend+instance Semigroup Utf8Builder where+ b₁ <> b₂ = Utf8Builder $ utf8Builder b₁ <> utf8Builder b₂ {-# INLINE (<>) #-}+ stimes = S.stimesMonoid+ {-# INLINE stimes #-} instance Printer Utf8Builder where char = Utf8Builder . BB.charUtf8@@ -227,10 +252,51 @@ buildLazyUtf8 = BB.toLazyByteString . utf8Builder {-# INLINE buildLazyUtf8 #-} -instance Printer PP.Doc where- char = PP.char+newtype PrettyPrinter = PrettyPrinter { prettyPrinter ∷ PP.Doc }+ deriving ( Typeable+#if __GLASGOW_HASKELL__ >= 706+ , Generic+#endif+#if MIN_VERSION_pretty(1,1,0)+ , IsString+# if MIN_VERSION_base(4,9,0)+ , Semigroup+# endif+ , Monoid+#endif+ )++#if !MIN_VERSION_pretty(1,1,0)+instance IsString PrettyPrinter where+ fromString = PrettyPrinter . PP.text+ {-# INLINE fromString #-}+#endif++#if !MIN_VERSION_base(4,9,0) || !MIN_VERSION_pretty(1,1,0)+instance Semigroup PrettyPrinter where+ p₁ <> p₂ = PrettyPrinter+ $ (PP.<>) (prettyPrinter p₁) (prettyPrinter p₂)+ {-# INLINE (<>) #-}+ stimes = S.stimesMonoid+ {-# INLINE stimes #-}+#endif++#if !MIN_VERSION_pretty(1,1,0)+instance Monoid PrettyPrinter where+ mempty = PP.empty+ {-# INLINE mempty #-}+ mappend = (S.<>)+ {-# INLINE mappend #-}+#endif++instance Printer PrettyPrinter where+ char = PrettyPrinter . PP.char {-# INLINE char #-} +-- | An alias for @'PP.render' . 'prettyPrinter'@+renderPretty ∷ PrettyPrinter → String+renderPretty = PP.render . prettyPrinter+ #if !MIN_VERSION_base(4,5,0) -- | An infix synonym for 'mappend'. (<>) ∷ Monoid m ⇒ m → m → m@@ -341,8 +407,9 @@ -- /x/ '<->' (/y/ '<->' /z/) = (/x/ '<->' /y/) '<->' /z/. (<->) ∷ p → p → p -instance MultilinePrinter PP.Doc where- (<->) = (PP.$+$)+instance MultilinePrinter PrettyPrinter where+ p₁ <-> p₂ = PrettyPrinter + $ (PP.$+$) (prettyPrinter p₁) (prettyPrinter p₂) {-# INLINE (<->) #-} -- | Combine the items of a 'Foldable' data structure with '<->'.@@ -362,17 +429,26 @@ -- | A multiline printer that combines lines with the provided function. newtype LinePrinter p = LinePrinter { linePrinter ∷ (p → p → p) → p }- deriving Typeable+ deriving ( Typeable+#if __GLASGOW_HASKELL__ >= 706+ , Generic+#endif+ ) instance IsString p ⇒ IsString (LinePrinter p) where fromString = LinePrinter . const . fromString {-# INLINE fromString #-} +instance Semigroup p ⇒ Semigroup (LinePrinter p) where+ x <> y = LinePrinter $ \l → linePrinter x l S.<> linePrinter y l+ {-# INLINE (<>) #-}+ stimes n x = LinePrinter $ S.stimes n . linePrinter x+ {-# INLINE stimes #-}+ instance Monoid p ⇒ Monoid (LinePrinter p) where mempty = LinePrinter $ const mempty {-# INLINE mempty #-}- mappend x y = LinePrinter $ \l →- mappend (linePrinter x l) (linePrinter y l)+ mappend x y = LinePrinter $ \l → mappend (linePrinter x l) (linePrinter y l) {-# INLINE mappend #-} mconcat xs = LinePrinter $ \l → mconcat (map (\x → linePrinter x l) xs) {-# INLINE mconcat #-}@@ -412,4 +488,3 @@ crlfPrinter ∷ Printer p ⇒ LinePrinter p → p crlfPrinter p = linePrinter p (separate crlf) {-# INLINE crlfPrinter #-}-
src/Text/Printer/Fractional.hs view
@@ -1,5 +1,9 @@+{-# LANGUAGE CPP #-} {-# LANGUAGE UnicodeSyntax #-} {-# LANGUAGE DeriveDataTypeable #-}+#if __GLASGOW_HASKELL__ >= 706+{-# LANGUAGE DeriveGeneric #-}+#endif -- | Print fractions. module Text.Printer.Fractional@@ -22,6 +26,9 @@ , fraction ) where +#if __GLASGOW_HASKELL__ >= 706+import GHC.Generics (Generic)+#endif import Data.Typeable (Typeable) import Data.Ix (Ix) import Data.Monoid (mempty)@@ -32,7 +39,11 @@ -- | Optionality characteristic. data Optional = Optional | Required- deriving (Typeable, Show, Read, Eq, Ord, Enum, Bounded, Ix)+ deriving ( Typeable+#if __GLASGOW_HASKELL__ >= 706+ , Generic+#endif+ , Show, Read, Eq, Ord, Enum, Bounded, Ix) -- | True if the supplied value is 'Optional' and false otherwise. isOptional ∷ Optional → Bool
src/Text/Printer/Integral.hs view
@@ -1,5 +1,9 @@+{-# LANGUAGE CPP #-} {-# LANGUAGE UnicodeSyntax #-} {-# LANGUAGE DeriveDataTypeable #-}+#if __GLASGOW_HASKELL__ >= 706+{-# LANGUAGE DeriveGeneric #-}+#endif {-# LANGUAGE BangPatterns #-} -- | Print integral numbers in common positional numeral systems. @@ -61,6 +65,9 @@ , upHexBits ) where +#if __GLASGOW_HASKELL__ >= 706+import GHC.Generics (Generic)+#endif import Data.Typeable (Typeable) import Data.Char (chr, ord) import Data.Int@@ -110,7 +117,11 @@ lastDigitIn ∷ Bits α ⇒ s → α → Int -- | The binary numeral system.-data Binary = Binary deriving (Typeable, Eq, Ord, Show, Read)+data Binary = Binary deriving ( Typeable+#if __GLASGOW_HASKELL__ >= 706+ , Generic+#endif+ , Eq, Ord, Show, Read ) instance PositionalSystem Binary where systemName _ = "binary"@@ -141,7 +152,11 @@ {-# INLINE lastDigitIn #-} -- | The octal numeral system.-data Octal = Octal deriving (Typeable, Eq, Ord, Show, Read)+data Octal = Octal deriving ( Typeable+#if __GLASGOW_HASKELL__ >= 706+ , Generic+#endif+ , Eq, Ord, Show, Read ) instance PositionalSystem Octal where systemName _ = "octal"@@ -174,7 +189,11 @@ {-# INLINE lastDigitIn #-} -- | The decimal numeral system.-data Decimal = Decimal deriving (Typeable, Eq, Ord, Show, Read)+data Decimal = Decimal deriving ( Typeable+#if __GLASGOW_HASKELL__ >= 706+ , Generic+#endif+ , Eq, Ord, Show, Read ) instance PositionalSystem Decimal where systemName _ = "decimal"@@ -197,7 +216,11 @@ {-# INLINE printZeroIn #-} -- | The hexadecimal numeral system.-data Hexadecimal = Hexadecimal+data Hexadecimal = Hexadecimal deriving ( Typeable+#if __GLASGOW_HASKELL__ >= 706+ , Generic+#endif+ , Eq, Ord, Show, Read ) instance PositionalSystem Hexadecimal where systemName _ = "hexadecimal"@@ -232,7 +255,11 @@ {-# INLINABLE lastDigitIn #-} -- | The hexadecimal numeral system, using lower case digits.-data LowHex = LowHex+data LowHex = LowHex deriving ( Typeable+#if __GLASGOW_HASKELL__ >= 706+ , Generic+#endif+ , Eq, Ord, Show, Read ) instance PositionalSystem LowHex where systemName _ = "lower case hexadecimal"@@ -267,7 +294,11 @@ {-# INLINABLE lastDigitIn #-} -- | The hexadecimal numeral system, using upper case digits.-data UpHex = UpHex+data UpHex = UpHex deriving ( Typeable+#if __GLASGOW_HASKELL__ >= 706+ , Generic+#endif+ , Eq, Ord, Show, Read ) instance PositionalSystem UpHex where systemName _ = "upper case hexadecimal"
text-printer.cabal view
@@ -1,5 +1,5 @@ Name: text-printer-Version: 0.4+Version: 0.5 Category: Text Stability: experimental Synopsis: Abstract interface for text builders/printers.@@ -16,6 +16,12 @@ License: BSD3 License-File: LICENSE +Extra-Source-Files:+ README.md++Tested-With: GHC==7.2.2, GHC==7.4.2, GHC==7.6.3, GHC==7.8.4,+ GHC==7.10.3, GHC==8.0.1+ Cabal-Version: >= 1.10.0 Build-Type: Simple @@ -26,12 +32,12 @@ Library Default-Language: Haskell2010 Build-Depends:- base >= 4 && < 5,+ base >= 4.4 && < 5, bytestring >= 0.10, text, pretty, text-latin1 >= 0.3,- semigroups >= 0.8+ semigroups >= 0.18.2 Hs-Source-Dirs: src GHC-Options: -Wall Exposed-Modules:@@ -43,7 +49,7 @@ Default-Language: Haskell2010 Type: exitcode-stdio-1.0 Build-Depends:- base >= 4 && < 5,+ base >= 4.4 && < 5, test-framework >= 0.5, test-framework-quickcheck2 >= 0.2, QuickCheck >= 2.4,