File: Main.hs

package info (click to toggle)
haskell-pretty-simple 4.1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 204 kB
  • sloc: haskell: 1,075; makefile: 4
file content (105 lines) | stat: -rw-r--r-- 2,719 bytes parent folder | download | duplicates (2)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
module Main where

-- This is a small executable that will pretty-print anything from stdin.
-- It can be installed to `~/.local/bin` if you enable the flag `buildexe` like so:
--
-- @
--   $ stack install pretty-simple-2.0.1.1 --flag pretty-simple:buildexe
-- @
--
-- When you run it, you can paste something you want formatted on stdin, then
-- press @Ctrl-D@.  It will print the formatted version on stdout:
--
-- @
--   $ pretty-simple
--   [(Just 3, Just 4)]
--
--   ^D
--
--   [
--       ( Just 3
--       , Just 4
--       )
--   ]
-- @

import Data.Monoid ((<>))
import Data.Text (unpack)
import qualified Data.Text.IO as T
import qualified Data.Text.Lazy.IO as LT
import Data.Version (showVersion)
import Options.Applicative
       ( Parser, ReadM, execParser, fullDesc, help, helper, info, infoOption
       , long, option, progDesc, readerError, short, showDefaultWith, str
       , switch, value)
import Paths_pretty_simple (version)
import Text.Pretty.Simple
       ( pStringOpt, OutputOptions
       , defaultOutputOptionsDarkBg
       , defaultOutputOptionsLightBg
       , defaultOutputOptionsNoColor
       , outputOptionsCompact
       )

data Color = DarkBg
           | LightBg
           | NoColor

data Args = Args
  { color :: Color
  , compact :: Bool
  }

colorReader :: ReadM Color
colorReader = do
  string <- str
  case string of
    "dark-bg"  -> pure DarkBg
    "light-bg" -> pure LightBg
    "no-color" -> pure NoColor
    x          -> readerError $ "Could not parse " <> x <> " as a color."

args :: Parser Args
args = Args
    <$> option colorReader
        ( long "color"
       <> short 'c'
       <> help "Select printing color. Available options: dark-bg (default), light-bg, no-color."
       <> showDefaultWith (const "dark-bg")
       <> value DarkBg
        )
    <*> switch
        ( long "compact"
       <> short 'C'
       <> help "Compact output"
        )

versionOption :: Parser (a -> a)
versionOption =
  infoOption
    (showVersion version)
    ( long "version"
   <> short 'V'
   <> help "Show version"
    )

main :: IO ()
main = do
  args' <- execParser opts
  input <- T.getContents
  let output = pStringOpt (getPrintOpt args') $ unpack input
  LT.putStrLn output
  where
    opts = info (helper <*> versionOption <*> args)
      ( fullDesc
     <> progDesc "Format Haskell data types with indentation and highlighting"
      )

    getPrintOpt :: Args -> OutputOptions
    getPrintOpt as =
      (getColorOpt (color as)) {outputOptionsCompact = compact as}

    getColorOpt :: Color -> OutputOptions
    getColorOpt DarkBg  = defaultOutputOptionsDarkBg
    getColorOpt LightBg = defaultOutputOptionsLightBg
    getColorOpt NoColor = defaultOutputOptionsNoColor