DEV Community

Cover image for Playing with Fmt
Riccardo Odone
Riccardo Odone

Posted on • Updated on • Originally published at odone.io

Playing with Fmt

You can keep reading here or jump to my blog to get the full experience, including the wonderful pink, blue and white palette.


{-# 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
Enter fullscreen mode Exit fullscreen mode

And yeah, the name is actually Mario Mario!


Get the latest content via email from me personally. Reply with your thoughts. Let's learn from each other. Subscribe to my PinkLetter!

Top comments (0)