{-# LANGUAGE OverloadedStrings #-}

-- | Examples that should always compile. If reading on Haddock, you
-- can view the sources to each of these.

module Formatting.Examples
  ( hello
  , strings
  , texts
  , builders
  , integers
  , floats
  , hexes
  , padding
  ) where

import Data.Text.Lazy (Text)
import Data.Text.Lazy.Builder (Builder)
import Formatting

-- | Simple hello, world!
hello :: Text
hello :: Text
hello = Format Text Text -> Text
forall a. Format Text a -> a
format "Hello, World!"

-- | Printing strings.
strings :: Text
strings :: Text
strings =
  Format Text (String -> String -> Text) -> String -> String -> Text
forall a. Format Text a -> a
format ("Here comes a string: " Format (String -> String -> Text) (String -> String -> Text)
-> Format Text (String -> String -> Text)
-> Format Text (String -> String -> Text)
forall r a r'. Format r a -> Format r' r -> Format r' a
% Format (String -> Text) (String -> String -> Text)
forall r. Format r (String -> r)
string Format (String -> Text) (String -> String -> Text)
-> Format Text (String -> Text)
-> Format Text (String -> String -> Text)
forall r a r'. Format r a -> Format r' r -> Format r' a
% " and another " Format (String -> Text) (String -> Text)
-> Format Text (String -> Text) -> Format Text (String -> Text)
forall r a r'. Format r a -> Format r' r -> Format r' a
% Format Text (String -> Text)
forall r. Format r (String -> r)
string)
         "Hello, World!"
         "Ahoy!"

-- | Printing texts.
texts :: Text
texts :: Text
texts =
  Format Text (Text -> Text -> Text) -> Text -> Text -> Text
forall a. Format Text a -> a
format ("Here comes a string: " Format (Text -> Text -> Text) (Text -> Text -> Text)
-> Format Text (Text -> Text -> Text)
-> Format Text (Text -> Text -> Text)
forall r a r'. Format r a -> Format r' r -> Format r' a
% Format (Text -> Text) (Text -> Text -> Text)
forall r. Format r (Text -> r)
text Format (Text -> Text) (Text -> Text -> Text)
-> Format Text (Text -> Text) -> Format Text (Text -> Text -> Text)
forall r a r'. Format r a -> Format r' r -> Format r' a
% " and another " Format (Text -> Text) (Text -> Text)
-> Format Text (Text -> Text) -> Format Text (Text -> Text)
forall r a r'. Format r a -> Format r' r -> Format r' a
% Format Text (Text -> Text)
forall r. Format r (Text -> r)
text)
         "Hello, World!"
         "Ahoy!"

-- | Printing builders.
builders :: Text
builders :: Text
builders =
  Format Text (Builder -> Text -> Text) -> Builder -> Text -> Text
forall a. Format Text a -> a
format ("Here comes a string: " Format (Builder -> Text -> Text) (Builder -> Text -> Text)
-> Format Text (Builder -> Text -> Text)
-> Format Text (Builder -> Text -> Text)
forall r a r'. Format r a -> Format r' r -> Format r' a
% Format (Text -> Text) (Builder -> Text -> Text)
forall r. Format r (Builder -> r)
builder Format (Text -> Text) (Builder -> Text -> Text)
-> Format Text (Text -> Text)
-> Format Text (Builder -> Text -> Text)
forall r a r'. Format r a -> Format r' r -> Format r' a
% " and another " Format (Text -> Text) (Text -> Text)
-> Format Text (Text -> Text) -> Format Text (Text -> Text)
forall r a r'. Format r a -> Format r' r -> Format r' a
% Format Text (Text -> Text)
forall r. Format r (Text -> r)
text)
         ("Hello, World!" :: Builder)
         "Ahoy!"

-- | Printing integers.
integers :: Text
integers :: Text
integers =
  Format Text (Int -> Integer -> Text) -> Int -> Integer -> Text
forall a. Format Text a -> a
format ("Here comes an integer: " Format (Int -> Integer -> Text) (Int -> Integer -> Text)
-> Format Text (Int -> Integer -> Text)
-> Format Text (Int -> Integer -> Text)
forall r a r'. Format r a -> Format r' r -> Format r' a
% Format (Integer -> Text) (Int -> Integer -> Text)
forall a r. Integral a => Format r (a -> r)
int Format (Integer -> Text) (Int -> Integer -> Text)
-> Format Text (Integer -> Text)
-> Format Text (Int -> Integer -> Text)
forall r a r'. Format r a -> Format r' r -> Format r' a
% " and another: " Format (Integer -> Text) (Integer -> Text)
-> Format Text (Integer -> Text) -> Format Text (Integer -> Text)
forall r a r'. Format r a -> Format r' r -> Format r' a
% Format Text (Integer -> Text)
forall a r. Integral a => Format r (a -> r)
int)
         (23 :: Int)
         (0 :: Integer)

-- | Printing floating points.
floats :: Text
floats :: Text
floats =
  Format Text (Float -> Text) -> Float -> Text
forall a. Format Text a -> a
format ("Here comes a float: " Format (Float -> Text) (Float -> Text)
-> Format Text (Float -> Text) -> Format Text (Float -> Text)
forall r a r'. Format r a -> Format r' r -> Format r' a
% Format Text (Float -> Text)
forall a r. Real a => Format r (a -> r)
float)
         (123.2342 :: Float)

-- | Printing integrals in hex (base-16).
hexes :: Text
hexes :: Text
hexes =
  Format Text (Int -> Text) -> Int -> Text
forall a. Format Text a -> a
format ("Here comes a hex: " Format (Int -> Text) (Int -> Text)
-> Format Text (Int -> Text) -> Format Text (Int -> Text)
forall r a r'. Format r a -> Format r' r -> Format r' a
% Format Text (Int -> Text)
forall a r. Integral a => Format r (a -> r)
hex)
         (123 :: Int)

-- | Padding.
padding :: Text
padding :: Text
padding =
  Format Text (Int -> Text) -> Int -> Text
forall a. Format Text a -> a
format ("A left-padded number: " Format (Int -> Text) (Int -> Text)
-> Format Text (Int -> Text) -> Format Text (Int -> Text)
forall r a r'. Format r a -> Format r' r -> Format r' a
% Int -> Char -> Format Text (Int -> Text)
forall a r. Buildable a => Int -> Char -> Format r (a -> r)
left 3 '0')
         (9 :: Int)