Playing with Fmt

Posted on October 7, 2019 by Riccardo
{-# LANGUAGE OverloadedStrings #-}

module Main where

import Data.Text (Text)
import Data.Text as T
import Data.Text.IO as T
import Fmt

data Person = Person
    { firstName :: Text
    , lastName :: Text
    }
    deriving (Show)

instance Buildable Person where
    build person =
        (toTitle $ firstName person) |+ 
            ", " +|
            (toTitle $ lastName person)
            |+ ""

main :: IO ()
main = do
    let name = "mario" :: Text
        person = Person "mario" "mario"
        people = 
            [ Person "mario" "mario"
            , Person "luigi" "mario"
            ]

    T.putStrLn $ "hello " +| name |+ "!"
    -- hello mario!

    T.putStrLn $ "hello " +| toTitle name |+ "!"
    -- hello Mario!

    T.putStrLn $ "hello " +| padBothF 10 '=' name |+ "!"
    -- hello ===mario==!

    T.putStrLn $ "hello " +| whenF False (build name) |+ "!"
    -- hello !

    T.putStrLn $ "hello " +|| person ||+ "!"
    -- hello Person {firstName = "mario", lastName = "mario"}!

    T.putStrLn $ "hello " +| person |+ "!"
    -- hello Mario, Mario!

    T.putStrLn $ "hello " +| people |+ "!"
    -- hello [Mario, Mario,Luigi, Mario]!

    T.putStrLn $ fmt $ unlinesF people
    -- Mario, Mario
    -- Luigi, Mario

    T.putStrLn $ fmt $ jsonListF people
    -- [
    --   Mario, Mario
    -- , Luigi, Mario
    -- ]

    T.putStrLn $ fmt $ blockListF people
    -- - Mario, Mario
    -- - Luigi, Mario

And yeah, the name is actually Mario Mario!

PinkLetter

It's one of the selected few I follow every week – Mateusz

Tired of RELEARNING webdev stuff?

  • A 100+ page book with the best links I curated over the years
  • An email once a week full of timeless software wisdom
  • Your recommended weekly dose of pink
  • Try before you buy? Check the archives.