packages feed

formatting 5.4 → 6.0.0

raw patch · 8 files changed

+124/−109 lines, 8 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

- Formatting.Holey: instance (IsString m, a ~ r) => IsString (Holey m r a)
+ Formatting: formatToString :: Format [Char] a -> a
+ Formatting.Holey: instance a ~ r => IsString (Format r a)
+ Formatting.Internal: formatToString :: Format [Char] a -> a
- Formatting: bprint :: Holey Builder Builder a -> a
+ Formatting: bprint :: Format Builder a -> a
- Formatting: format :: Holey Builder Text a -> a
+ Formatting: format :: Format Text a -> a
- Formatting: fprint :: Holey Builder (IO ()) a -> a
+ Formatting: fprint :: Format (IO ()) a -> a
- Formatting: hprint :: Handle -> Holey Builder (IO ()) a -> a
+ Formatting: hprint :: Handle -> Format (IO ()) a -> a
- Formatting: sformat :: Holey Builder Text a -> a
+ Formatting: sformat :: Format Text a -> a
- Formatting.Formatters: asInt :: Enum a => Format a
+ Formatting.Formatters: asInt :: Enum a => Format r (a -> r)
- Formatting.Formatters: base :: Integral a => Int -> Format a
+ Formatting.Formatters: base :: Integral a => Int -> Format r (a -> r)
- Formatting.Formatters: bin :: Integral a => Format a
+ Formatting.Formatters: bin :: Integral a => Format r (a -> r)
- Formatting.Formatters: build :: Buildable a => Format a
+ Formatting.Formatters: build :: Buildable a => Format r (a -> r)
- Formatting.Formatters: builder :: Format Builder
+ Formatting.Formatters: builder :: Format r (Builder -> r)
- Formatting.Formatters: center :: Buildable a => Int -> Char -> Format a
+ Formatting.Formatters: center :: Buildable a => Int -> Char -> Format r (a -> r)
- Formatting.Formatters: char :: Format Char
+ Formatting.Formatters: char :: Format r (Char -> r)
- Formatting.Formatters: commas :: (Buildable n, Integral n) => Format n
+ Formatting.Formatters: commas :: (Buildable n, Integral n) => Format r (n -> r)
- Formatting.Formatters: expt :: Real a => Int -> Format a
+ Formatting.Formatters: expt :: Real a => Int -> Format r (a -> r)
- Formatting.Formatters: fconst :: Builder -> Format a
+ Formatting.Formatters: fconst :: Builder -> Format r (a -> r)
- Formatting.Formatters: fitLeft :: Buildable a => Int -> Format a
+ Formatting.Formatters: fitLeft :: Buildable a => Int -> Format r (a -> r)
- Formatting.Formatters: fitRight :: Buildable a => Int -> Format a
+ Formatting.Formatters: fitRight :: Buildable a => Int -> Format r (a -> r)
- Formatting.Formatters: fixed :: Real a => Int -> Format a
+ Formatting.Formatters: fixed :: Real a => Int -> Format r (a -> r)
- Formatting.Formatters: float :: Real a => Format a
+ Formatting.Formatters: float :: Real a => Format r (a -> r)
- Formatting.Formatters: groupInt :: (Buildable n, Integral n) => Int -> Char -> Format n
+ Formatting.Formatters: groupInt :: (Buildable n, Integral n) => Int -> Char -> Format r (n -> r)
- Formatting.Formatters: hex :: Integral a => Format a
+ Formatting.Formatters: hex :: Integral a => Format r (a -> r)
- Formatting.Formatters: int :: Integral a => Format a
+ Formatting.Formatters: int :: Integral a => Format r (a -> r)
- Formatting.Formatters: left :: Buildable a => Int -> Char -> Format a
+ Formatting.Formatters: left :: Buildable a => Int -> Char -> Format r (a -> r)
- Formatting.Formatters: oct :: Integral a => Format a
+ Formatting.Formatters: oct :: Integral a => Format r (a -> r)
- Formatting.Formatters: ords :: Integral n => Format n
+ Formatting.Formatters: ords :: Integral n => Format r (n -> r)
- Formatting.Formatters: plural :: (Num a, Eq a) => Text -> Text -> Format a
+ Formatting.Formatters: plural :: (Num a, Eq a) => Text -> Text -> Format r (a -> r)
- Formatting.Formatters: prec :: Real a => Int -> Format a
+ Formatting.Formatters: prec :: Real a => Int -> Format r (a -> r)
- Formatting.Formatters: prefixBin :: Integral a => Format a
+ Formatting.Formatters: prefixBin :: Integral a => Format r (a -> r)
- Formatting.Formatters: prefixHex :: Integral a => Format a
+ Formatting.Formatters: prefixHex :: Integral a => Format r (a -> r)
- Formatting.Formatters: prefixOct :: Integral a => Format a
+ Formatting.Formatters: prefixOct :: Integral a => Format r (a -> r)
- Formatting.Formatters: right :: Buildable a => Int -> Char -> Format a
+ Formatting.Formatters: right :: Buildable a => Int -> Char -> Format r (a -> r)
- Formatting.Formatters: sci :: Format Scientific
+ Formatting.Formatters: sci :: Format r (Scientific -> r)
- Formatting.Formatters: scifmt :: FPFormat -> Maybe Int -> Format Scientific
+ Formatting.Formatters: scifmt :: FPFormat -> Maybe Int -> Format r (Scientific -> r)
- Formatting.Formatters: shortest :: Real a => Format a
+ Formatting.Formatters: shortest :: Real a => Format r (a -> r)
- Formatting.Formatters: shown :: Show a => Format a
+ Formatting.Formatters: shown :: Show a => Format r (a -> r)
- Formatting.Formatters: stext :: Format Text
+ Formatting.Formatters: stext :: Format r (Text -> r)
- Formatting.Formatters: string :: Format String
+ Formatting.Formatters: string :: Format r (String -> r)
- Formatting.Formatters: text :: Format Text
+ Formatting.Formatters: text :: Format r (Text -> r)
- Formatting.Holey: (%) :: Monoid n => Holey n b c -> Holey n b1 b -> Holey n b1 c
+ Formatting.Holey: (%) :: Format b c -> Format r b -> Format r c
- Formatting.Holey: (%.) :: Holey m r (a -> b) -> Holey a b c -> Holey m r c
+ Formatting.Holey: (%.) :: Format r (Builder -> b) -> Format b c -> Format r c
- Formatting.Holey: bind :: Holey m b c -> (m -> Holey n a b) -> Holey n a c
+ Formatting.Holey: bind :: Format b c -> (Builder -> Format a b) -> Format a c
- Formatting.Holey: later :: (a -> m) -> Holey m r (a -> r)
+ Formatting.Holey: later :: (a -> Builder) -> Format r (a -> r)
- Formatting.Holey: now :: m -> Holey m r r
+ Formatting.Holey: now :: Builder -> Format r r
- Formatting.Holey: type Format a = forall r. Holey Builder r (a -> r)
+ Formatting.Holey: type Format r x = Holey Builder r x
- Formatting.Internal: bprint :: Holey Builder Builder a -> a
+ Formatting.Internal: bprint :: Format Builder a -> a
- Formatting.Internal: format :: Holey Builder Text a -> a
+ Formatting.Internal: format :: Format Text a -> a
- Formatting.Internal: fprint :: Holey Builder (IO ()) a -> a
+ Formatting.Internal: fprint :: Format (IO ()) a -> a
- Formatting.Internal: hprint :: Handle -> Holey Builder (IO ()) a -> a
+ Formatting.Internal: hprint :: Handle -> Format (IO ()) a -> a
- Formatting.Internal: sformat :: Holey Builder Text a -> a
+ Formatting.Internal: sformat :: Format Text a -> a
- Formatting.ShortFormatters: b :: Integral a => Format a
+ Formatting.ShortFormatters: b :: Integral a => Format r (a -> r)
- Formatting.ShortFormatters: c :: Format Char
+ Formatting.ShortFormatters: c :: Format r (Char -> r)
- Formatting.ShortFormatters: d :: Integral a => Format a
+ Formatting.ShortFormatters: d :: Integral a => Format r (a -> r)
- Formatting.ShortFormatters: ef :: Real a => Int -> Format a
+ Formatting.ShortFormatters: ef :: Real a => Int -> Format r (a -> r)
- Formatting.ShortFormatters: f :: Real a => Int -> Format a
+ Formatting.ShortFormatters: f :: Real a => Int -> Format r (a -> r)
- Formatting.ShortFormatters: l :: Buildable a => Int -> Char -> Format a
+ Formatting.ShortFormatters: l :: Buildable a => Int -> Char -> Format r (a -> r)
- Formatting.ShortFormatters: o :: Integral a => Format a
+ Formatting.ShortFormatters: o :: Integral a => Format r (a -> r)
- Formatting.ShortFormatters: pf :: Real a => Int -> Format a
+ Formatting.ShortFormatters: pf :: Real a => Int -> Format r (a -> r)
- Formatting.ShortFormatters: r :: Buildable a => Int -> Char -> Format a
+ Formatting.ShortFormatters: r :: Buildable a => Int -> Char -> Format r (a -> r)
- Formatting.ShortFormatters: s :: Format String
+ Formatting.ShortFormatters: s :: Format r (String -> r)
- Formatting.ShortFormatters: sf :: Real a => Format a
+ Formatting.ShortFormatters: sf :: Real a => Format r (a -> r)
- Formatting.ShortFormatters: sh :: Show a => Format a
+ Formatting.ShortFormatters: sh :: Show a => Format r (a -> r)
- Formatting.ShortFormatters: st :: Format Text
+ Formatting.ShortFormatters: st :: Format r (Text -> r)
- Formatting.ShortFormatters: t :: Format Text
+ Formatting.ShortFormatters: t :: Format r (Text -> r)
- Formatting.ShortFormatters: x :: Integral a => Format a
+ Formatting.ShortFormatters: x :: Integral a => Format r (a -> r)
- Formatting.Time: century :: FormatTime a => Format a
+ Formatting.Time: century :: FormatTime a => Format r (a -> r)
- Formatting.Time: dateDash :: FormatTime a => Format a
+ Formatting.Time: dateDash :: FormatTime a => Format r (a -> r)
- Formatting.Time: dateSlash :: FormatTime a => Format a
+ Formatting.Time: dateSlash :: FormatTime a => Format r (a -> r)
- Formatting.Time: dateSlashL :: FormatTime a => Format a
+ Formatting.Time: dateSlashL :: FormatTime a => Format r (a -> r)
- Formatting.Time: datetime :: FormatTime a => Format a
+ Formatting.Time: datetime :: FormatTime a => Format r (a -> r)
- Formatting.Time: day :: FormatTime a => Format a
+ Formatting.Time: day :: FormatTime a => Format r (a -> r)
- Formatting.Time: dayHalf :: FormatTime a => Format a
+ Formatting.Time: dayHalf :: FormatTime a => Format r (a -> r)
- Formatting.Time: dayHalfU :: FormatTime a => Format a
+ Formatting.Time: dayHalfU :: FormatTime a => Format r (a -> r)
- Formatting.Time: dayName :: FormatTime a => Format a
+ Formatting.Time: dayName :: FormatTime a => Format r (a -> r)
- Formatting.Time: dayNameShort :: FormatTime a => Format a
+ Formatting.Time: dayNameShort :: FormatTime a => Format r (a -> r)
- Formatting.Time: dayOfMonth :: FormatTime a => Format a
+ Formatting.Time: dayOfMonth :: FormatTime a => Format r (a -> r)
- Formatting.Time: dayOfMonthOrd :: FormatTime a => Format a
+ Formatting.Time: dayOfMonthOrd :: FormatTime a => Format r (a -> r)
- Formatting.Time: dayOfMonthS :: FormatTime a => Format a
+ Formatting.Time: dayOfMonthS :: FormatTime a => Format r (a -> r)
- Formatting.Time: dayOfWeek :: FormatTime a => Format a
+ Formatting.Time: dayOfWeek :: FormatTime a => Format r (a -> r)
- Formatting.Time: dayOfWeekFromZero :: FormatTime a => Format a
+ Formatting.Time: dayOfWeekFromZero :: FormatTime a => Format r (a -> r)
- Formatting.Time: days :: RealFrac n => Int -> Format n
+ Formatting.Time: days :: RealFrac n => Int -> Format r (n -> r)
- Formatting.Time: decimals :: FormatTime a => Format a
+ Formatting.Time: decimals :: FormatTime a => Format r (a -> r)
- Formatting.Time: diff :: RealFrac n => Bool -> Format n
+ Formatting.Time: diff :: RealFrac n => Bool -> Format r (n -> r)
- Formatting.Time: epoch :: FormatTime a => Format a
+ Formatting.Time: epoch :: FormatTime a => Format r (a -> r)
- Formatting.Time: hm :: FormatTime a => Format a
+ Formatting.Time: hm :: FormatTime a => Format r (a -> r)
- Formatting.Time: hms :: FormatTime a => Format a
+ Formatting.Time: hms :: FormatTime a => Format r (a -> r)
- Formatting.Time: hmsL :: FormatTime a => Format a
+ Formatting.Time: hmsL :: FormatTime a => Format r (a -> r)
- Formatting.Time: hmsPL :: FormatTime a => Format a
+ Formatting.Time: hmsPL :: FormatTime a => Format r (a -> r)
- Formatting.Time: hour12 :: FormatTime a => Format a
+ Formatting.Time: hour12 :: FormatTime a => Format r (a -> r)
- Formatting.Time: hour12S :: FormatTime a => Format a
+ Formatting.Time: hour12S :: FormatTime a => Format r (a -> r)
- Formatting.Time: hour24 :: FormatTime a => Format a
+ Formatting.Time: hour24 :: FormatTime a => Format r (a -> r)
- Formatting.Time: hour24S :: FormatTime a => Format a
+ Formatting.Time: hour24S :: FormatTime a => Format r (a -> r)
- Formatting.Time: hours :: RealFrac n => Int -> Format n
+ Formatting.Time: hours :: RealFrac n => Int -> Format r (n -> r)
- Formatting.Time: minute :: FormatTime a => Format a
+ Formatting.Time: minute :: FormatTime a => Format r (a -> r)
- Formatting.Time: minutes :: RealFrac n => Int -> Format n
+ Formatting.Time: minutes :: RealFrac n => Int -> Format r (n -> r)
- Formatting.Time: month :: FormatTime a => Format a
+ Formatting.Time: month :: FormatTime a => Format r (a -> r)
- Formatting.Time: monthName :: FormatTime a => Format a
+ Formatting.Time: monthName :: FormatTime a => Format r (a -> r)
- Formatting.Time: monthNameShort :: FormatTime a => Format a
+ Formatting.Time: monthNameShort :: FormatTime a => Format r (a -> r)
- Formatting.Time: pico :: FormatTime a => Format a
+ Formatting.Time: pico :: FormatTime a => Format r (a -> r)
- Formatting.Time: second :: FormatTime a => Format a
+ Formatting.Time: second :: FormatTime a => Format r (a -> r)
- Formatting.Time: seconds :: RealFrac n => Int -> Format n
+ Formatting.Time: seconds :: RealFrac n => Int -> Format r (n -> r)
- Formatting.Time: tz :: FormatTime a => Format a
+ Formatting.Time: tz :: FormatTime a => Format r (a -> r)
- Formatting.Time: tzName :: FormatTime a => Format a
+ Formatting.Time: tzName :: FormatTime a => Format r (a -> r)
- Formatting.Time: week :: FormatTime a => Format a
+ Formatting.Time: week :: FormatTime a => Format r (a -> r)
- Formatting.Time: weekCentury :: FormatTime a => Format a
+ Formatting.Time: weekCentury :: FormatTime a => Format r (a -> r)
- Formatting.Time: weekFromZero :: FormatTime a => Format a
+ Formatting.Time: weekFromZero :: FormatTime a => Format r (a -> r)
- Formatting.Time: weekOfYearMon :: FormatTime a => Format a
+ Formatting.Time: weekOfYearMon :: FormatTime a => Format r (a -> r)
- Formatting.Time: weekYY :: FormatTime a => Format a
+ Formatting.Time: weekYY :: FormatTime a => Format r (a -> r)
- Formatting.Time: weekYear :: FormatTime a => Format a
+ Formatting.Time: weekYear :: FormatTime a => Format r (a -> r)
- Formatting.Time: year :: FormatTime a => Format a
+ Formatting.Time: year :: FormatTime a => Format r (a -> r)
- Formatting.Time: years :: RealFrac n => Int -> Format n
+ Formatting.Time: years :: RealFrac n => Int -> Format r (n -> r)
- Formatting.Time: yy :: FormatTime a => Format a
+ Formatting.Time: yy :: FormatTime a => Format r (a -> r)

Files

+ CHANGELOG.md view
@@ -0,0 +1,5 @@+6.0.0++* Changed the type of `Format`. Now you write `Format r (a -> r)` instead+  of `Format a`.+* Add `formatToString` function.
formatting.cabal view
@@ -1,5 +1,5 @@ name:                formatting-version:             5.4+version:             6.0.0 synopsis:            Combinator-based type-safe formatting (like printf() or FORMAT) description:         Combinator-based type-safe formatting (like printf() or FORMAT), modelled from the HoleyMonoids package. license:             BSD3@@ -10,6 +10,7 @@ category:            Text build-type:          Simple cabal-version:       >=1.8+extra-source-files:  CHANGELOG.md  library   exposed-modules:   Formatting,
src/Formatting.hs view
@@ -29,7 +29,9 @@   hprint,   -- * Formatting library   module Formatting.Holey,-  module Formatting.Formatters+  module Formatting.Formatters,+  -- * Other functions+  formatToString  ) where  import Formatting.Formatters
src/Formatting/Formatters.hs view
@@ -74,15 +74,15 @@ import           Data.Scientific  -- | Output a lazy text.-text :: Format Text+text :: Format r (Text -> r) text = later T.fromLazyText  -- | Output a strict text.-stext :: Format S.Text+stext :: Format r (S.Text -> r) stext = later T.fromText  -- | Output a string.-string :: Format String+string :: Format r (String -> r) string = later (T.fromText . T.pack)  -- | Output a showable value (instance of 'Show') by turning it into@@ -90,87 +90,87 @@ -- -- >>> format ("Value number " % shown % " is " % shown % ".") 42 False -- "Value number 42 is False."-shown :: Show a => Format a+shown :: Show a => Format r (a -> r) shown = later (T.fromText . T.pack . show)  -- | Output a character.-char :: Format Char+char :: Format r (Char -> r) char = later B.build  -- | Build a builder.-builder :: Format Builder+builder :: Format r (Builder -> r) builder = later id  -- | Like `const` but for formatters.-fconst :: Builder -> Format a+fconst :: Builder -> Format r (a -> r) fconst m = later (const m)  -- | Build anything that implements the "Buildable" class.-build :: Buildable a => Format a+build :: Buildable a => Format r (a -> r) build = later B.build  -- | Render an integral e.g. 123 -> \"123\", 0 -> \"0\".-int :: Integral a => Format a+int :: Integral a => Format r (a -> r) int = later T.shortest  -- | Render some floating point with the usual notation, e.g. 123.32 => \"123.32\"-float :: Real a => Format a+float :: Real a => Format r (a -> r) float = later (T.shortest)  -- | Render a floating point number using scientific/engineering -- notation (e.g. 2.3e123), with the given number of decimal places.-expt :: Real a => Int -> Format a+expt :: Real a => Int -> Format r (a -> r) expt i = later (T.expt i)  -- | Render a floating point number using normal notation, with the -- given number of decimal places.-fixed :: Real a => Int -> Format a+fixed :: Real a => Int -> Format r (a -> r) fixed i = later (T.fixed i)  -- | Render a floating point number, with the given number of digits -- of precision. Uses decimal notation for values between 0.1 and -- 9,999,999, and scientific notation otherwise.-prec :: Real a => Int -> Format a+prec :: Real a => Int -> Format r (a -> r) prec i = later (T.prec i)  -- | Render a floating point number using the smallest number of -- digits that correctly represent it.-shortest :: Real a => Format a+shortest :: Real a => Format r (a -> r) shortest = later T.shortest  -- | Render a scientific number.-sci :: Format Scientific+sci :: Format r (Scientific -> r) sci = later scientificBuilder  -- | Render a scientific number with options.-scifmt :: FPFormat -> Maybe Int -> Format Scientific+scifmt :: FPFormat -> Maybe Int -> Format r (Scientific -> r) scifmt f i = later (formatScientificBuilder f i)  -- | Shows the Int value of Enum instances using 'fromEnum'. -- -- >>> format ("Got: " % char % " (" % asInt % ")") 'a' 'a' -- "Got: a (97)"-asInt :: Enum a => Format a+asInt :: Enum a => Format r (a -> r) asInt = later (T.shortest . fromEnum)  -- | Pad the left hand side of a string until it reaches k characters -- wide, if necessary filling with character c.-left :: Buildable a => Int -> Char -> Format a+left :: Buildable a => Int -> Char -> Format r (a -> r) left i c = later (T.left i c)  -- | Pad the right hand side of a string until it reaches k characters -- wide, if necessary filling with character c.-right :: Buildable a => Int -> Char -> Format a+right :: Buildable a => Int -> Char -> Format r (a -> r) right i c = later (T.right i c)  -- | Pad the left & right hand side of a string until it reaches k characters -- wide, if necessary filling with character c.-center :: Buildable a => Int -> Char -> Format a+center :: Buildable a => Int -> Char -> Format r (a -> r) center i c = later centerT where   centerT = T.fromLazyText . LT.center (fromIntegral i) c . T.toLazyText . B.build  -- | Group integral numbers, e.g. groupInt 2 '.' on 123456 -> \"12.34.56\".-groupInt :: (Buildable n,Integral n) => Int -> Char -> Format n+groupInt :: (Buildable n,Integral n) => Int -> Char -> Format r (n -> r) groupInt 0 _ = later B.build groupInt i c = later (commaize)   where commaize =@@ -190,12 +190,12 @@         cycle' xs = xs <> cycle' xs  -- | Fit in the given length, truncating on the left.-fitLeft :: Buildable a => Int -> Format a+fitLeft :: Buildable a => Int -> Format r (a -> r) fitLeft size = later (fit (fromIntegral size)) where   fit i = T.fromLazyText . LT.take i . T.toLazyText . B.build  -- | Fit in the given length, truncating on the right.-fitRight :: Buildable a => Int -> Format a+fitRight :: Buildable a => Int -> Format r (a -> r) fitRight size = later (fit (fromIntegral size)) where   fit i = T.fromLazyText .           (\t -> LT.drop (LT.length t - i) t)@@ -203,11 +203,11 @@           . B.build  -- | Add commas to an integral, e.g 12000 -> \ "12,000".-commas :: (Buildable n,Integral n) => Format n+commas :: (Buildable n,Integral n) => Format r (n -> r) commas = groupInt 3 ','  -- | Add a suffix to an integral, e.g. 1st, 2nd, 3rd, 21st.-ords :: Integral n => Format n+ords :: Integral n => Format r (n -> r) ords = later go   where go n           | tens > 3 && tens < 21 = T.shortest n <> "th"@@ -221,43 +221,43 @@           where tens = n `mod` 100  -- | English plural suffix for an integral.-plural :: (Num a, Eq a) => Text -> Text -> Format a+plural :: (Num a, Eq a) => Text -> Text -> Format r (a -> r) plural s p = later (\i -> if i == 1 then B.build s else B.build p)  -- | Render an integral at base n.-base :: Integral a => Int -> Format a+base :: Integral a => Int -> Format r (a -> r) base numBase = later (B.build . atBase numBase)  -- | Render an integer using binary notation. (No leading 0b is -- added.) Defined as @bin = 'base' 2@.-bin :: Integral a => Format a+bin :: Integral a => Format r (a -> r) bin = base 2 {-# INLINE bin #-}  -- | Render an integer using octal notation. (No leading 0o is -- added.) Defined as @oct = 'base' 8@.-oct :: Integral a => Format a+oct :: Integral a => Format r (a -> r) oct = base 8 {-# INLINE oct #-}  -- | Render an integer using hexadecimal notation. (No leading 0x is -- added.) Has a specialized implementation.-hex :: Integral a => Format a+hex :: Integral a => Format r (a -> r) hex = later T.hex {-# INLINE hex #-}  -- | Render an integer using binary notation with a leading 0b.-prefixBin :: Integral a => Format a+prefixBin :: Integral a => Format r (a -> r) prefixBin = "0b" % bin {-# INLINE prefixBin #-}  -- | Render an integer using octal notation with a leading 0o.-prefixOct :: Integral a => Format a+prefixOct :: Integral a => Format r (a -> r) prefixOct = "0o" % oct {-# INLINE prefixOct #-}  -- | Render an integer using hexadecimal notation with a leading 0x.-prefixHex :: Integral a => Format a+prefixHex :: Integral a => Format r (a -> r) prefixHex = "0x" % hex {-# INLINE prefixHex #-} 
src/Formatting/Holey.hs view
@@ -1,3 +1,4 @@+{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE TypeSynonymInstances #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE GADTs #-}@@ -32,7 +33,7 @@ import Data.Text.Lazy.Builder (Builder)  -- | A formatter.-type Format a = forall r. Holey Builder r (a -> r)+type Format r x = Holey Builder r x  -- | The type of a monoid with holes. The underlying monoid is -- represented by type parameter @m@. The @r@ is the result type and@@ -50,28 +51,28 @@   fmap g m = Holey (\k -> runHM m (k . g))  -- | Very useful instance for writing format string.-instance (IsString m, a ~ r) => IsString (Holey m r a) where+instance (a ~ r) => IsString (Format r a) where   fromString = now . fromString  -- | Composition operator. The same as category composition.-(%) :: Monoid n => Holey n b c -> Holey n b1 b -> Holey n b1 c+(%) :: Format b c -> Format r b -> Format r c f % g = f `bind` \a -> g `bind` \b -> now (a `mappend` b)  -- | Function compose two holeys. Will feed the result of one holey -- into another.-(%.) :: Holey m r (a -> b) -> Holey a b c -> Holey m r c+(%.) :: Format r (Builder -> b) -> Format b c -> Format r c (%.) (Holey a) (Holey b) = Holey (b . a)  -- | Insert a constant monoidal value.-now :: m -> Holey m r r+now :: Builder -> Format r r now a = Holey ($ a)  -- | Monadic indexed bind for holey monoids.-bind :: Holey m b c -> (m -> Holey n a b) -> Holey n a c+bind :: Format b c -> (Builder -> Format a b) -> Format a c m `bind` f = Holey $ \k -> runHM m (\a -> runHM (f a) k)  -- | Insert a monoidal value that is not specified until the -- computation is 'run'. The argument that is expected later is -- converted to the monoid type using the given conversion function.-later :: (a -> m) -> Holey m r (a -> r)+later :: (a -> Builder) -> Format r (a -> r) later f = Holey (. f)
src/Formatting/Internal.hs view
@@ -4,6 +4,8 @@  import           Formatting.Holey +import qualified Data.Text.Lazy as TL+import qualified Data.Text.Lazy.Builder as TLB import qualified Data.Text as S (Text) import           Data.Text.Lazy (Text) import qualified Data.Text.Lazy as T@@ -13,21 +15,25 @@ import           System.IO  -- | Run the formatter and return a lazy 'Text' value.-format :: Holey Builder Text a -> a+format :: Format Text a -> a format m = runHM m T.toLazyText  -- | Run the formatter and return a strict 'S.Text' value.-sformat :: Holey Builder S.Text a -> a+sformat :: Format S.Text a -> a sformat m = runHM m (T.toStrict . T.toLazyText)  -- | Run the formatter and return a 'Builder' value.-bprint :: Holey Builder Builder a -> a+bprint :: Format Builder a -> a bprint m = runHM m id  -- | Run the formatter and print out the text to stdout.-fprint :: Holey Builder (IO ()) a -> a+fprint :: Format (IO ()) a -> a fprint m = runHM m (T.putStr . T.toLazyText)  -- | Run the formatter and put the output onto the given 'Handle'.-hprint :: Handle -> Holey Builder (IO ()) a -> a+hprint :: Handle -> Format (IO ()) a -> a hprint h m = runHM m (T.hPutStr h . T.toLazyText)++-- | Run the formatter and return a list of characters.+formatToString :: Format [Char] a -> a+formatToString m = runHM m (TL.unpack . TLB.toLazyText)
src/Formatting/ShortFormatters.hs view
@@ -27,71 +27,71 @@ import qualified Data.Text.Lazy.Builder as T  -- | Output a lazy text.-t :: Format Text+t :: Format r (Text -> r) t = later T.fromLazyText  -- | Render an integral e.g. 123 -> \"123\", 0 -> \"0\".-d :: Integral a => Format a+d :: Integral a => Format r (a -> r) d = int  -- | Render an integer using binary notation. (No leading 0b is -- added.)-b :: Integral a => Format a+b :: Integral a => Format r (a -> r) b = bin  -- | Render an integer using octal notation. (No leading 0o is added.)-o :: Integral a => Format a+o :: Integral a => Format r (a -> r) o = oct  -- | Render an integer using hexadecimal notation. (No leading 0x is -- added.)-x :: Integral a => Format a+x :: Integral a => Format r (a -> r) x = later T.hex  -- | Output a strict text.-st :: Format S.Text+st :: Format r (S.Text -> r) st = later T.fromText  -- | Output a string.-s :: Format String+s :: Format r (String -> r) s = later (T.fromText . T.pack)  -- | Output a showable value (instance of 'Show') by turning it into -- 'Text'.-sh :: Show a => Format a+sh :: Show a => Format r (a -> r) sh = later (T.fromText . T.pack . show)  -- | Output a character.-c :: Format Char+c :: Format r (Char -> r) c = later B.build  -- | Render a floating point number using scientific/engineering -- notation (e.g. 2.3e123), with the given number of decimal places.-ef :: Real a => Int -> Format a+ef :: Real a => Int -> Format r (a -> r) ef i = later (T.expt i)  -- | Render a floating point number using normal notation, with the -- given number of decimal places.-f :: Real a => Int -> Format a+f :: Real a => Int -> Format r (a -> r) f i = later (T.fixed i)  -- | Render a floating point number, with the given number of digits -- of precision. Uses decimal notation for values between 0.1 and -- 9,999,999, and scientific notation otherwise.-pf :: Real a => Int -> Format a+pf :: Real a => Int -> Format r (a -> r) pf i = later (T.prec i)  -- | Render a floating point number using the smallest number of -- digits that correctly represent it.-sf :: Real a => Format a+sf :: Real a => Format r (a -> r) sf = later T.shortest  -- | Pad the left hand side of a string until it reaches @k@ characters -- wide, if necessary filling with character @ch@.-l :: Buildable a => Int -> Char -> Format a+l :: Buildable a => Int -> Char -> Format r (a -> r) l i ch = later (T.left i ch)  -- | Pad the right hand side of a string until it reaches @k@ characters -- wide, if necessary filling with character @ch@.-r :: Buildable a => Int -> Char -> Format a+r :: Buildable a => Int -> Char -> Format r (a -> r) r i ch = later (T.right i ch)
src/Formatting/Time.hs view
@@ -20,77 +20,77 @@ -- * For 'TimeZone' (and 'ZonedTime' and 'UTCTime'):  -- | Timezone offset on the format @-HHMM@.-tz :: FormatTime a => Format a+tz :: FormatTime a => Format r (a -> r) tz = later (build . fmt "%z")  -- | Timezone name.-tzName :: FormatTime a => Format a+tzName :: FormatTime a => Format r (a -> r) tzName = later (build . fmt "%Z")  -- | As 'dateTimeFmt' @locale@ (e.g. @%a %b %e %H:%M:%S %Z %Y@).-datetime :: FormatTime a => Format a+datetime :: FormatTime a => Format r (a -> r) datetime = later (build . fmt "%c")  -- * For 'TimeOfDay' (and 'LocalTime' and 'ZonedTime' and 'UTCTime'):  -- | Same as @%H:%M@.-hm :: FormatTime a => Format a+hm :: FormatTime a => Format r (a -> r) hm = later (build . fmt "%R")  -- | Same as @%H:%M:%S@.-hms :: FormatTime a => Format a+hms :: FormatTime a => Format r (a -> r) hms = later (build . fmt "%T")  -- | As 'timeFmt' @locale@ (e.g. @%H:%M:%S@).-hmsL :: FormatTime a => Format a+hmsL :: FormatTime a => Format r (a -> r) hmsL = later (build . fmt "%X")  -- | As 'time12Fmt' @locale@ (e.g. @%I:%M:%S %p@).-hmsPL :: FormatTime a => Format a+hmsPL :: FormatTime a => Format r (a -> r) hmsPL = later (build . fmt "%r")  -- | Day half from ('amPm' @locale@), converted to lowercase, @am@, -- @pm@.-dayHalf :: FormatTime a => Format a+dayHalf :: FormatTime a => Format r (a -> r) dayHalf = later (build . fmt "%P")  -- | Day half from ('amPm' @locale@), @AM@, @PM@.-dayHalfU :: FormatTime a => Format a+dayHalfU :: FormatTime a => Format r (a -> r) dayHalfU = later (build . fmt "%p")  -- | Hour, 24-hour, leading 0 as needed, @00@ - @23@.-hour24 :: FormatTime a => Format a+hour24 :: FormatTime a => Format r (a -> r) hour24 = later (build . fmt "%H")  -- | Hour, 12-hour, leading 0 as needed, @01@ - @12@.-hour12 :: FormatTime a => Format a+hour12 :: FormatTime a => Format r (a -> r) hour12 = later (build . fmt "%I")  -- | Hour, 24-hour, leading space as needed, @ 0@ - @23@.-hour24S :: FormatTime a => Format a+hour24S :: FormatTime a => Format r (a -> r) hour24S = later (build . fmt "%k")  -- | Hour, 12-hour, leading space as needed, @ 1@ - @12@.-hour12S :: FormatTime a => Format a+hour12S :: FormatTime a => Format r (a -> r) hour12S = later (build . fmt "%l")  -- | Minute, @00@ - @59@.-minute :: FormatTime a => Format a+minute :: FormatTime a => Format r (a -> r) minute = later (build . fmt "%M")  -- | Second, without decimal part, @00@ - @60@.-second :: FormatTime a => Format a+second :: FormatTime a => Format r (a -> r) second = later (build . fmt "%S")  -- | Picosecond, including trailing zeros, @000000000000@ - -- @999999999999@.-pico :: FormatTime a => Format a+pico :: FormatTime a => Format r (a -> r) pico = later (build . fmt "%q")  -- | Decimal point and up to 12 second decimals, without trailing -- zeros. For a whole number of seconds, this produces the empty -- string.-decimals :: FormatTime a => Format a+decimals :: FormatTime a => Format r (a -> r) decimals = later (build . fmt "%Q")  -- * For 'UTCTime' and 'ZonedTime'@@ -99,110 +99,110 @@ -- the Unix epoch, this is a negative number. Note that in @%s.%q@ and @%s%Q@ -- the decimals are positive, not negative. For example, 0.9 seconds -- before the Unix epoch is formatted as @-1.1@ with @%s%Q@.-epoch :: FormatTime a => Format a+epoch :: FormatTime a => Format r (a -> r) epoch = later (build . fmt "%s")  -- * For 'Day' (and 'LocalTime' and 'ZonedTime' and 'UTCTime'):  -- | Same as @%m\/%d\/%y@.-dateSlash :: FormatTime a => Format a+dateSlash :: FormatTime a => Format r (a -> r) dateSlash = later (build . fmt "%D")  -- | Same as @%Y-%m-%d@.-dateDash :: FormatTime a => Format a+dateDash :: FormatTime a => Format r (a -> r) dateDash = later (build . fmt "%F")  -- | As 'dateFmt' @locale@ (e.g. @%m\/%d\/%y@).-dateSlashL :: FormatTime a => Format a+dateSlashL :: FormatTime a => Format r (a -> r) dateSlashL = later (build . fmt "%x")  -- | Year.-year :: FormatTime a => Format a+year :: FormatTime a => Format r (a -> r) year = later (build . fmt "%Y")  -- | Last two digits of year, @00@ - @99@.-yy :: FormatTime a => Format a+yy :: FormatTime a => Format r (a -> r) yy = later (build . fmt "%y")  -- | Century (being the first two digits of the year), @00@ - @99@.-century :: FormatTime a => Format a+century :: FormatTime a => Format r (a -> r) century = later (build . fmt "%C")  -- | Month name, long form ('fst' from 'months' @locale@), @January@ - -- @December@.-monthName :: FormatTime a => Format a+monthName :: FormatTime a => Format r (a -> r) monthName = later (build . fmt "%B")  -- | @ %H] month name, short form ('snd' from 'months' @locale@), -- @Jan@ - @Dec@.-monthNameShort :: FormatTime a => Format a+monthNameShort :: FormatTime a => Format r (a -> r) monthNameShort = later (build . fmt "%b")  -- | Month of year, leading 0 as needed, @01@ - @12@.-month :: FormatTime a => Format a+month :: FormatTime a => Format r (a -> r) month = later (build . fmt "%m")  -- | Day of month, leading 0 as needed, @01@ - @31@.-dayOfMonth :: FormatTime a => Format a+dayOfMonth :: FormatTime a => Format r (a -> r) dayOfMonth = later (build . fmt "%d")  -- | Day of month, @1st@, @2nd@, @25th@, etc.-dayOfMonthOrd :: FormatTime a => Format a+dayOfMonthOrd :: FormatTime a => Format r (a -> r) dayOfMonthOrd = later (bprint ords . toInt)   where toInt :: FormatTime a => a -> Int         toInt = read . formatTime defaultTimeLocale "%d"  -- | Day of month, leading space as needed, @ 1@ - @31@.-dayOfMonthS :: FormatTime a => Format a+dayOfMonthS :: FormatTime a => Format r (a -> r) dayOfMonthS = later (build . fmt "%e")  -- | Day of year for Ordinal Date format, @001@ - @366@.-day :: FormatTime a => Format a+day :: FormatTime a => Format r (a -> r) day = later (build . fmt "%j")  -- | Year for Week Date format e.g. @2013@.-weekYear :: FormatTime a => Format a+weekYear :: FormatTime a => Format r (a -> r) weekYear = later (build . fmt "%G")  -- | Last two digits of year for Week Date format, @00@ - @99@.-weekYY :: FormatTime a => Format a+weekYY :: FormatTime a => Format r (a -> r) weekYY = later (build . fmt "%g")  -- | Century (first two digits of year) for Week Date format, @00@ - -- @99@.-weekCentury :: FormatTime a => Format a+weekCentury :: FormatTime a => Format r (a -> r) weekCentury = later (build . fmt "%f")  -- | Week for Week Date format, @01@ - @53@.-week :: FormatTime a => Format a+week :: FormatTime a => Format r (a -> r) week = later (build . fmt "%V")  -- | Day for Week Date format, @1@ - @7@.-dayOfWeek :: FormatTime a => Format a+dayOfWeek :: FormatTime a => Format r (a -> r) dayOfWeek = later (build . fmt "%u")  -- | Day of week, short form ('snd' from 'wDays' @locale@), @Sun@ - -- @Sat@.-dayNameShort :: FormatTime a => Format a+dayNameShort :: FormatTime a => Format r (a -> r) dayNameShort = later (build . fmt "%a")  -- | Day of week, long form ('fst' from 'wDays' @locale@), @Sunday@ - -- @Saturday@.-dayName :: FormatTime a => Format a+dayName :: FormatTime a => Format r (a -> r) dayName = later (build . fmt "%A")  -- | Week number of year, where weeks start on Sunday (as -- 'sundayStartWeek'), @00@ - @53@.-weekFromZero :: FormatTime a => Format a+weekFromZero :: FormatTime a => Format r (a -> r) weekFromZero = later (build . fmt "%U")  -- | Day of week number, @0@ (= Sunday) - @6@ (= Saturday).-dayOfWeekFromZero :: FormatTime a => Format a+dayOfWeekFromZero :: FormatTime a => Format r (a -> r) dayOfWeekFromZero = later (build . fmt "%w")  -- | Week number of year, where weeks start on Monday (as -- 'mondayStartWeek'), @00@ - @53@.-weekOfYearMon :: FormatTime a => Format a+weekOfYearMon :: FormatTime a => Format r (a -> r) weekOfYearMon = later (build . fmt "%W")  -- * Time spans, diffs, 'NominalDiffTime', 'DiffTime', etc.@@ -212,7 +212,7 @@ -- 'DiffTime'. diff :: (RealFrac n)      => Bool     -- ^ Display 'in/ago'?-     -> Format n -- ^ Example: '3 seconds ago', 'in three days'.+     -> Format r (n -> r) -- ^ Example: '3 seconds ago', 'in three days'.) diff fix =   later diffed   where@@ -252,35 +252,35 @@ -- | Display the absolute value time span in years. years :: (RealFrac n)       => Int -- ^ Decimal places.-      -> Format n+      -> Format r (n -> r) years n = later (bprint (fixed n) . abs . count)   where count n = n / 365 / 24 / 60 / 60  -- | Display the absolute value time span in days. days :: (RealFrac n)       => Int -- ^ Decimal places.-      -> Format n+      -> Format r (n -> r) days n = later (bprint (fixed n) . abs . count)   where count n = n / 24 / 60 / 60  -- | Display the absolute value time span in hours. hours :: (RealFrac n)       => Int -- ^ Decimal places.-      -> Format n+      -> Format r (n -> r) hours n = later (bprint (fixed n) . abs . count)   where count n = n / 60 / 60  -- | Display the absolute value time span in minutes. minutes :: (RealFrac n)       => Int -- ^ Decimal places.-      -> Format n+      -> Format r (n -> r) minutes n = later (bprint (fixed n) . abs . count)   where count n = n / 60  -- | Display the absolute value time span in seconds. seconds :: (RealFrac n)       => Int -- ^ Decimal places.-      -> Format n+      -> Format r (n -> r) seconds n = later (bprint (fixed n) . abs . count)   where count n = n