Skip to content

Commit

Permalink
Merge pull request #35 from andrew-lei/light_bg
Browse files Browse the repository at this point in the history
Allow light-bg and no-color modes for executable
  • Loading branch information
cdepillabout authored Jul 10, 2018
2 parents 089f7db + 7845f5e commit 94a6c26
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
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

0 comments on commit 94a6c26

Please sign in to comment.