File: Example.hs

package info (click to toggle)
haskell-ansi-wl-pprint 1.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 80 kB
  • sloc: haskell: 53; makefile: 2
file content (28 lines) | stat: -rw-r--r-- 1,526 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
module Main (main) where

import Text.PrettyPrint.ANSI.Leijen

import System.IO

import Prettyprinter (layoutPretty, defaultLayoutOptions)
import Prettyprinter.Render.Terminal (renderLazy)
import Data.Text.Lazy (unpack)


main :: IO ()
main = do
    -- Going directly to the console is portable across Unix and Windows...
    putDoc $ red (text "Red") <> comma <+> white (text "white") <+> text "and" <+> blue (text "blue") <> char '!' <> linebreak
    putDoc $ blue (text "Nested" <+> dullyellow (text "colors") <+> text "example") <> linebreak
    hPutDoc stdout $ onred (text "Red") <> comma <+> onwhite (text "white") <+> text "and" <+> onblue (text "blue") <> char '!' <> linebreak
    hPutDoc stdout $ onblue (text "Nested" <+> ondullyellow (text "colors") <+> text "example") <> linebreak
    
    -- ...but going via a string will only preserve formatting information information on Unix
    putStr $ unpack . renderLazy . layoutPretty defaultLayoutOptions $ green (text "I will be green on Unix but uncolored on Windows") <> linebreak
    
    -- Let's see some non-color formatting:
    putDoc $ text "We can do" <+> bold (text "boldness") <> text ", if your terminal supports it, and even perhaps" <+> underline (text "underlining") <> linebreak

    -- Just a little test of the formatting removal:
    putDoc $ text "There is a handy utility called 'plain' to" <+> plain (bold $ text "remove formatting") <+>
              plain (text "if you need to e.g. support" <+> red (text "non-ANSI") <+> text "terminals")