stringfmt
Safe HaskellSafe-Inferred
LanguageHaskell2010

Data.Fmt

Description

Type-safe string formatting and pretty-printing.

This module re-exports the most commonly used parts of the library. For specialized functionality, import the submodules directly:

Synopsis

Core Fmt type (from Data.Fmt.Type)

newtype Fmt m a b Source #

An indexed continuation formatter.

Fmt m a b = (m -> a) -> b — the monoid m accumulates formatted output, a is the result type, and b captures the arguments.

This is Costar ((->) m) from profunctors, giving Profunctor, Closed, Costrong, Cochoice, Category, and Arrow instances.

Constructors

Fmt 

Fields

Instances

Instances details
Monoid m => Category (Fmt m :: Type -> Type -> Type) Source # 
Instance details

Defined in Data.Fmt.Type

Methods

id :: forall (a :: k). Fmt m a a #

(.) :: forall (b :: k) (c :: k) (a :: k). Fmt m b c -> Fmt m a b -> Fmt m a c #

Monoid m => Arrow (Fmt m) Source # 
Instance details

Defined in Data.Fmt.Type

Methods

arr :: (b -> c) -> Fmt m b c #

first :: Fmt m b c -> Fmt m (b, d) (c, d) #

second :: Fmt m b c -> Fmt m (d, b) (d, c) #

(***) :: Fmt m b c -> Fmt m b' c' -> Fmt m (b, b') (c, c') #

(&&&) :: Fmt m b c -> Fmt m b c' -> Fmt m b (c, c') #

Cochoice (Fmt m) Source # 
Instance details

Defined in Data.Fmt.Type

Methods

unleft :: Fmt m (Either a d) (Either b d) -> Fmt m a b Source #

unright :: Fmt m (Either d a) (Either d b) -> Fmt m a b Source #

Closed (Fmt m) Source # 
Instance details

Defined in Data.Fmt.Type

Methods

closed :: Fmt m a b -> Fmt m (x -> a) (x -> b) Source #

Corepresentable (Fmt m) Source # 
Instance details

Defined in Data.Fmt.Type

Associated Types

type Corep (Fmt m) :: Type -> Type Source #

Methods

cotabulate :: (Corep (Fmt m) d -> c) -> Fmt m d c Source #

Costrong (Fmt m) Source # 
Instance details

Defined in Data.Fmt.Type

Methods

unfirst :: Fmt m (a, d) (b, d) -> Fmt m a b Source #

unsecond :: Fmt m (d, a) (d, b) -> Fmt m a b Source #

Monoid m => Strong (Fmt m) Source # 
Instance details

Defined in Data.Fmt.Type

Methods

first' :: Fmt m a b -> Fmt m (a, c) (b, c) Source #

second' :: Fmt m a b -> Fmt m (c, a) (c, b) Source #

Profunctor (Fmt m) Source # 
Instance details

Defined in Data.Fmt.Type

Methods

dimap :: (a -> b) -> (c -> d) -> Fmt m b c -> Fmt m a d Source #

lmap :: (a -> b) -> Fmt m b c -> Fmt m a c Source #

rmap :: (b -> c) -> Fmt m a b -> Fmt m a c Source #

(#.) :: forall a b c q. Coercible c b => q b c -> Fmt m a b -> Fmt m a c Source #

(.#) :: forall a b c q. Coercible b a => Fmt m b c -> q a b -> Fmt m a c Source #

Cosieve (Fmt m) ((->) m) Source # 
Instance details

Defined in Data.Fmt.Type

Methods

cosieve :: Fmt m a b -> (m -> a) -> b Source #

Applicative (Fmt m a) Source # 
Instance details

Defined in Data.Fmt.Type

Methods

pure :: a0 -> Fmt m a a0 #

(<*>) :: Fmt m a (a0 -> b) -> Fmt m a a0 -> Fmt m a b #

liftA2 :: (a0 -> b -> c) -> Fmt m a a0 -> Fmt m a b -> Fmt m a c #

(*>) :: Fmt m a a0 -> Fmt m a b -> Fmt m a b #

(<*) :: Fmt m a a0 -> Fmt m a b -> Fmt m a a0 #

Functor (Fmt m a) Source # 
Instance details

Defined in Data.Fmt.Type

Methods

fmap :: (a0 -> b) -> Fmt m a a0 -> Fmt m a b #

(<$) :: a0 -> Fmt m a b -> Fmt m a a0 #

Monad (Fmt m a) Source # 
Instance details

Defined in Data.Fmt.Type

Methods

(>>=) :: Fmt m a a0 -> (a0 -> Fmt m a b) -> Fmt m a b #

(>>) :: Fmt m a a0 -> Fmt m a b -> Fmt m a b #

return :: a0 -> Fmt m a a0 #

(IsString m, a ~ b) => IsString (Fmt m a b) Source # 
Instance details

Defined in Data.Fmt.Type

Methods

fromString :: String -> Fmt m a b #

Monoid m => Monoid (Fmt1 m s a) Source # 
Instance details

Defined in Data.Fmt.Type

Methods

mempty :: Fmt1 m s a #

mappend :: Fmt1 m s a -> Fmt1 m s a -> Fmt1 m s a #

mconcat :: [Fmt1 m s a] -> Fmt1 m s a #

Semigroup m => Semigroup (Fmt1 m s a) Source # 
Instance details

Defined in Data.Fmt.Type

Methods

(<>) :: Fmt1 m s a -> Fmt1 m s a -> Fmt1 m s a #

sconcat :: NonEmpty (Fmt1 m s a) -> Fmt1 m s a #

stimes :: Integral b => b -> Fmt1 m s a -> Fmt1 m s a #

type Corep (Fmt m) Source # 
Instance details

Defined in Data.Fmt.Type

type Corep (Fmt m) = (->) m

runFmt :: Fmt m m a -> a Source #

Run a Fmt.

type Fmt1 m s a = Fmt m s (a -> s) Source #

A unary formatter: Fmt1 m s a ~ (m -> s) -> a -> s

type Fmt2 m s a b = Fmt m s (a -> b -> s) Source #

A binary formatter: Fmt2 m s a b ~ (m -> s) -> a -> b -> s

type Fmt3 m s a b c = Fmt m s (a -> b -> c -> s) Source #

A ternary formatter.

fmt1 :: (a -> m) -> Fmt1 m s a Source #

Format a value using a function a -> m.

fmt2 :: (a -> b -> m) -> Fmt2 m s a b Source #

Format two values.

fmt1_ :: Fmt m a a -> Fmt1 m a b Source #

Ignore the input, use a constant formatter.

fmt2_ :: Fmt m a a -> Fmt2 m a b c Source #

Ignore two inputs.

(.%) :: Semigroup m => Fmt1 m s a -> Fmt1 m s a -> Fmt1 m s a infixr 6 Source #

Concatenate two formatters, applying both to the same input.

cat1 :: (Monoid m, Foldable f) => Fmt1 m m a -> Fmt1 m s (f a) Source #

Format each value and concatenate.

fmt :: m -> Fmt m a a Source #

Format a constant value.

(%) :: Semigroup m => Fmt m b c -> Fmt m a b -> Fmt m a c infixr 0 Source #

Concatenate two formatters.

apply :: Fmt1 m s m -> Fmt m s a -> Fmt m s a Source #

Apply a Fmt1 to a Fmt.

bind :: Fmt m a1 b -> (m -> Fmt m a2 a1) -> Fmt m a2 b Source #

Indexed bind.

refmt :: (m1 -> m2) -> Fmt m1 a b -> Fmt m2 a b Source #

Map over the formatting monoid.

prefix :: Semigroup m => m -> Fmt m a b -> Fmt m a b Source #

Add the given prefix.

suffix :: Semigroup m => m -> Fmt m a b -> Fmt m a b Source #

Add the given suffix.

enclose :: Semigroup m => Fmt m b2 c -> Fmt m a b1 -> Fmt m b1 b2 -> Fmt m a c Source #

Enclose with prefix and suffix.

tuple :: (Semigroup m, IsString m) => Fmt m b c -> Fmt m a b -> Fmt m a c Source #

Format a pair in parentheses.

quotes :: (Semigroup m, IsString m) => Fmt m a b -> Fmt m a b Source #

Double quotes.

quotes' :: (Semigroup m, IsString m) => Fmt m a b -> Fmt m a b Source #

Single quotes.

parens :: (Semigroup m, IsString m) => Fmt m a b -> Fmt m a b Source #

Parentheses.

braces :: (Semigroup m, IsString m) => Fmt m a b -> Fmt m a b Source #

Braces.

brackets :: (Semigroup m, IsString m) => Fmt m a b -> Fmt m a b Source #

Square brackets.

backticks :: (Semigroup m, IsString m) => Fmt m a b -> Fmt m a b Source #

Backticks.

left1 :: IsString m => Fmt1 m m a -> Fmt1 m s (Either a b) Source #

Format a Left, rendering Right as empty.

right1 :: IsString m => Fmt1 m m b -> Fmt1 m s (Either a b) Source #

Format a Right, rendering Left as empty.

either1 :: Fmt1 m m a -> Fmt1 m m b -> Fmt1 m s (Either a b) Source #

Format an Either.

maybe1 :: m -> Fmt1 m m a -> Fmt1 m s (Maybe a) Source #

Format a Maybe with a default.

Tree type (from Data.Fmt.Functor)

type Tree m ann = Mu (Doc m ann) Source #

A document tree: the initial algebra of Doc m ann.

Pretty-printing (from Data.Fmt.Tree)

Smart constructors

emptyDoc :: Tree m ann Source #

Empty document.

leaf :: Int -> m -> Tree m ann Source #

Literal content with display width.

hardline :: Tree m ann Source #

Hard line break. Cannot be flattened.

line :: IsString m => Tree m ann Source #

Line break, or space when flattened.

line' :: Tree m ann Source #

Line break, or empty when flattened.

softline :: IsString m => Tree m ann Source #

A line break that behaves like a space when flattened by group.

softline = group line

softline' :: Tree m ann Source #

A line break that vanishes when flattened by group.

softline' = group line'

flatAlt :: Tree m ann -> Tree m ann -> Tree m ann Source #

flatAlt default flat: use default normally, flat when flattened by group.

nest :: Int -> Tree m ann -> Tree m ann Source #

Increase nesting by i.

group :: Tree m ann -> Tree m ann Source #

Try to lay out the document on a single line. Falls back to the original if flattening fails.

group x = union (flatten x) x

annotate :: ann -> Tree m ann -> Tree m ann Source #

Attach an annotation.

column :: (Int -> Tree m ann) -> Tree m ann Source #

React to the current column position.

nesting :: (Int -> Tree m ann) -> Tree m ann Source #

React to the current nesting level.

Separators

(<+>) :: IsString m => Tree m ann -> Tree m ann -> Tree m ann infixr 6 Source #

Concatenate with a space in between.

hsep :: IsString m => [Tree m ann] -> Tree m ann Source #

Concatenate with spaces.

vsep :: IsString m => [Tree m ann] -> Tree m ann Source #

Concatenate with line separators.

sep :: IsString m => [Tree m ann] -> Tree m ann Source #

vsep that tries to fit on one line (group).

hcat :: [Tree m ann] -> Tree m ann Source #

Concatenate without separators.

vcat :: [Tree m ann] -> Tree m ann Source #

Concatenate with line' separators (line or empty).

cat :: [Tree m ann] -> Tree m ann Source #

vcat that tries to fit on one line (group).

fillSep :: IsString m => [Tree m ann] -> Tree m ann Source #

Concatenate with softline separators.

fillCat :: [Tree m ann] -> Tree m ann Source #

Concatenate with softline' separators.

Indentation

align :: Tree m ann -> Tree m ann Source #

Lay out relative to the current column rather than the current nesting level.

align d = column (\k -> nesting (\i -> nest (k - i) d))

hang :: Int -> Tree m ann -> Tree m ann Source #

hang i doc = align (nest i doc)

indent :: IsString m => Int -> Tree m ann -> Tree m ann Source #

indent i doc inserts i spaces then aligns.

Enclosure

list :: IsString m => [Tree m ann] -> Tree m ann Source #

list = encloseSep "[" "]" ", "

tupled :: IsString m => [Tree m ann] -> Tree m ann Source #

tupled = encloseSep "(" ")" ", "

encloseSep :: IsString m => Tree m ann -> Tree m ann -> Tree m ann -> [Tree m ann] -> Tree m ann Source #

Enclose a list with separators.

encloseSep lbrace rbrace comma [a, b, c] = lbrace <> a <> comma <> b <> comma <> c <> rbrace

When the content fits, renders on one line. Otherwise, each element gets its own line, aligned.

surround :: Tree m ann -> Tree m ann -> Tree m ann -> Tree m ann Source #

surround mid left right = left <> mid <> right

punctuate :: Tree m ann -> [Tree m ann] -> [Tree m ann] Source #

Append a separator to all but the last element.

Layout and rendering

newtype LayoutOptions Source #

Layout options.

Constructors

LayoutOptions 

Instances

Instances details
Show LayoutOptions Source # 
Instance details

Defined in Data.Fmt.Tree

Eq LayoutOptions Source # 
Instance details

Defined in Data.Fmt.Tree

data PageWidth Source #

Page width configuration.

Constructors

AvailablePerLine !Int !Double
AvailablePerLine maxColumns ribbonFraction
Unbounded 

Instances

Instances details
Show PageWidth Source # 
Instance details

Defined in Data.Fmt.Tree

Eq PageWidth Source # 
Instance details

Defined in Data.Fmt.Tree

defaultLayoutOptions :: LayoutOptions Source #

80 columns, ribbon fraction 1.0.

pretty :: (Monoid m, IsString m) => LayoutOptions -> Tree m ann -> m Source #

Lay out and render a document.

pretty opts = render . layoutPretty opts

Numeric encoders (from Data.Fmt.Code)

c :: IsString m => Fmt1 m s Char Source #

Format a character.

s :: (IsString m, Show a) => Fmt1 m s a Source #

Format a showable value.

d :: IsString m => Fmt1 m s Int Source #

Decimal encoding of an Int.

u :: IsString m => Fmt1 m s Word Source #

Decimal encoding of a Word.

x :: IsString m => Fmt1 m s Word Source #

Shortest hex of a Word, lower-case.

e :: (IsString m, RealFloat a) => Int -> Fmt1 m s a Source #

Scientific notation with prec digits of precision.

f :: (IsString m, RealFloat a) => Int -> Fmt1 m s a Source #

Fixed-point notation with prec digits after the decimal.

g :: (IsString m, RealFloat a) => Int -> Fmt1 m s a Source #

General notation (shorter of e and f).