fmt-0.6.3.0: A new formatting library
Safe HaskellNone
LanguageHaskell2010

Fmt.Internal.Formatters

Synopsis

Documentation

>>> import Fmt

indentF :: Int -> Builder -> Builder Source #

Indent a block of text.

>>> fmt $ "This is a list:\n" <> indentF 4 (blockListF [1,2,3])
This is a list:
    - 1
    - 2
    - 3

The output will always end with a newline, even when the input doesn't.

indentF' :: Int -> Text -> Builder -> Builder Source #

Add a prefix to the first line, and indent all lines but the first one.

The output will always end with a newline, even when the input doesn't.

nameF :: Builder -> Builder -> Builder Source #

Attach a name to anything:

>>> fmt $ nameF "clients" $ blockListF ["Alice", "Bob", "Zalgo"]
clients:
  - Alice
  - Bob
  - Zalgo

unwordsF :: (Foldable f, Buildable a) => f a -> Builder Source #

Put spaces between elements.

>>> fmt $ unwordsF ["hello", "world"]
hello world

Of course, it works on anything Buildable:

>>> fmt $ unwordsF [1, 2]
1 2

unlinesF :: (Foldable f, Buildable a) => f a -> Builder Source #

Arrange elements on separate lines.

>>> fmt $ unlinesF ["hello", "world"]
hello
world

listF :: (Foldable f, Buildable a) => f a -> Builder Source #

A simple comma-separated list formatter.

>>> listF ["hello", "world"]
"[hello, world]"

For multiline output, use jsonListF.

listF' :: Foldable f => (a -> Builder) -> f a -> Builder Source #

A version of listF that lets you supply your own building function for list elements.

For instance, to format a list of numbers as hex:

>>> listF' hexF [1234, 5678]
"[4d2, 162e]"

blockListF :: forall f a. (Foldable f, Buildable a) => f a -> Builder Source #

A multiline formatter for lists.

>>> fmt $ blockListF [1,2,3]
- 1
- 2
- 3

Multi-line elements are indented correctly:

>>> fmt $ blockListF ["hello\nworld", "foo\nbar\nquix"]
- hello
  world
- foo
  bar
  quix

blockListF' Source #

Arguments

:: forall f a. Foldable f 
=> Text

Bullet

-> (a -> Builder)

Builder for elements

-> f a

Structure with elements

-> Builder 

A version of blockListF that lets you supply your own building function for list elements (instead of build) and choose the bullet character (instead of "-").

jsonListF :: forall f a. (Foldable f, Buildable a) => f a -> Builder Source #

A JSON-style formatter for lists.

>>> fmt $ jsonListF [1,2,3]
[
  1
, 2
, 3
]

Like blockListF, it handles multiline elements well:

>>> fmt $ jsonListF ["hello\nworld", "foo\nbar\nquix"]
[
  hello
  world
, foo
  bar
  quix
]

jsonListF' :: forall f a. Foldable f => (a -> Builder) -> f a -> Builder Source #

A version of jsonListF that lets you supply your own building function for list elements.

mapF :: (IsList t, Item t ~ (k, v), Buildable k, Buildable v) => t -> Builder Source #

A simple JSON-like map formatter; works for Map, HashMap, etc, as well as ordinary lists of pairs.

>>> mapF [("a", 1), ("b", 4)]
"{a: 1, b: 4}"

For multiline output, use jsonMapF.

mapF' :: (IsList t, Item t ~ (k, v)) => (k -> Builder) -> (v -> Builder) -> t -> Builder Source #

A version of mapF that lets you supply your own building function for keys and values.

blockMapF :: (IsList t, Item t ~ (k, v), Buildable k, Buildable v) => t -> Builder Source #

A YAML-like map formatter:

>>> fmt $ blockMapF [("Odds", blockListF [1,3]), ("Evens", blockListF [2,4])]
Odds:
  - 1
  - 3
Evens:
  - 2
  - 4

blockMapF' :: (IsList t, Item t ~ (k, v)) => (k -> Builder) -> (v -> Builder) -> t -> Builder Source #

A version of blockMapF that lets you supply your own building function for keys and values.

jsonMapF :: (IsList t, Item t ~ (k, v), Buildable k, Buildable v) => t -> Builder Source #

A JSON-like map formatter (unlike mapF, always multiline):

>>> fmt $ jsonMapF [("Odds", jsonListF [1,3]), ("Evens", jsonListF [2,4])]
{
  Odds:
    [
      1
    , 3
    ]
, Evens:
    [
      2
    , 4
    ]
}

jsonMapF' :: forall t k v. (IsList t, Item t ~ (k, v)) => (k -> Builder) -> (v -> Builder) -> t -> Builder Source #

A version of jsonMapF that lets you supply your own building function for keys and values.

maybeF :: Buildable a => Maybe a -> Builder Source #

Like build for Maybe, but displays Nothing as <Nothing> instead of an empty string.

build:

>>> build (Nothing :: Maybe Int)
""
>>> build (Just 1 :: Maybe Int)
"1"

maybeF:

>>> maybeF (Nothing :: Maybe Int)
"<Nothing>"
>>> maybeF (Just 1 :: Maybe Int)
"1"

eitherF :: (Buildable a, Buildable b) => Either a b -> Builder Source #

Format an Either:

>>> eitherF (Right 1 :: Either Bool Int)
"<Right: 1>"

prefixF :: Buildable a => Int -> a -> Builder Source #

Take the first N characters:

>>> prefixF 3 "hello"
"hel"

suffixF :: Buildable a => Int -> a -> Builder Source #

Take the last N characters:

>>> suffixF 3 "hello"
"llo"

padLeftF :: Buildable a => Int -> Char -> a -> Builder Source #

padLeftF n c pads the string with character c from the left side until it becomes n characters wide (and does nothing if the string is already that long, or longer):

>>> padLeftF 5 '0' 12
"00012"
>>> padLeftF 5 '0' 123456
"123456"

padRightF :: Buildable a => Int -> Char -> a -> Builder Source #

padRightF n c pads the string with character c from the right side until it becomes n characters wide (and does nothing if the string is already that long, or longer):

>>> padRightF 5 ' ' "foo"
"foo  "
>>> padRightF 5 ' ' "foobar"
"foobar"

padBothF :: Buildable a => Int -> Char -> a -> Builder Source #

padBothF n c pads the string with character c from both sides until it becomes n characters wide (and does nothing if the string is already that long, or longer):

>>> padBothF 5 '=' "foo"
"=foo="
>>> padBothF 5 '=' "foobar"
"foobar"

When padding can't be distributed equally, the left side is preferred:

>>> padBothF 8 '=' "foo"
"===foo=="

whenF :: Bool -> Builder -> Builder Source #

Display something only if the condition is True (empty string otherwise).

Note that it can only take a Builder (because otherwise it would be unusable with (+|)-formatted strings which can resolve to any FromBuilder). You can use build to convert any value to a Builder.

unlessF :: Bool -> Builder -> Builder Source #

Display something only if the condition is False (empty string otherwise).