Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow light-bg and no-color modes for executable #35

Merged
merged 3 commits into from
Jul 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,56 @@ module Main where
import Data.Text (unpack)
import qualified Data.Text.IO as T
import qualified Data.Text.Lazy.IO as LT
import Text.Pretty.Simple (pString)
import Options.Applicative
( Parser, ReadM, execParser, fullDesc, help, helper, info, long
, option, progDesc, readerError, short, showDefaultWith, str, value, (<**>))
import Data.Monoid ((<>))
import Text.Pretty.Simple
( pStringOpt, OutputOptions
, defaultOutputOptionsDarkBg
, defaultOutputOptionsLightBg
, defaultOutputOptionsNoColor
)

data Color = DarkBg
| LightBg
| NoColor

newtype Args = Args { color :: Color }

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 (\_ -> "dark-bg")
<> value DarkBg
)

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

getPrintOpt :: Color -> OutputOptions
getPrintOpt DarkBg = defaultOutputOptionsDarkBg
getPrintOpt LightBg = defaultOutputOptionsLightBg
getPrintOpt NoColor = defaultOutputOptionsNoColor
1 change: 1 addition & 0 deletions pretty-simple.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ executable pretty-simple
build-depends: base
, pretty-simple
, text
, optparse-applicative
default-language: Haskell2010
ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N

Expand Down