From 43a81ed32048ea1e74a3ee8b92be33b9c95ce6c3 Mon Sep 17 00:00:00 2001 From: Andrew Lei Date: Tue, 10 Jul 2018 04:31:47 -0400 Subject: [PATCH 1/3] Allow light-bg and no-color modes for executable --- app/Main.hs | 49 +++++++++++++++++++++++++++++++++++++++++++-- pretty-simple.cabal | 1 + 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index c93fe1c..d0c36a5 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -26,10 +26,55 @@ 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 + , maybeReader, option, progDesc, short, showDefaultWith, value, (<**>)) +import Data.Semigroup ((<>)) +import Text.Pretty.Simple + ( pStringOpt, OutputOptions + , defaultOutputOptionsDarkBg + , defaultOutputOptionsLightBg + , defaultOutputOptionsNoColor + ) + +data Color = DarkBg + | LightBg + | NoColor + +newtype Args = Args { color :: Color } + +colorReader :: ReadM Color +colorReader = maybeReader colorReader' + where + colorReader' "dark-bg" = Just DarkBg + colorReader' "light-bg" = Just LightBg + colorReader' "no-color" = Just NoColor + colorReader' _ = Nothing + +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 diff --git a/pretty-simple.cabal b/pretty-simple.cabal index 3c67eb4..0030894 100644 --- a/pretty-simple.cabal +++ b/pretty-simple.cabal @@ -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 From cfe085e47a907d94453dbca9ca1e5f0c5ceadb35 Mon Sep 17 00:00:00 2001 From: Dennis Gosnell Date: Tue, 10 Jul 2018 22:26:46 +0900 Subject: [PATCH 2/3] Try to get this working on older compilers. --- app/Main.hs | 23 ++++++++++++----------- pretty-simple.cabal | 1 + 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index d0c36a5..b227449 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -28,7 +28,7 @@ import qualified Data.Text.IO as T import qualified Data.Text.Lazy.IO as LT import Options.Applicative ( Parser, ReadM, execParser, fullDesc, help, helper, info, long - , maybeReader, option, progDesc, short, showDefaultWith, value, (<**>)) + , option, progDesc, readerError, short, showDefaultWith, str, value, (<**>)) import Data.Semigroup ((<>)) import Text.Pretty.Simple ( pStringOpt, OutputOptions @@ -37,22 +37,23 @@ import Text.Pretty.Simple , defaultOutputOptionsNoColor ) -data Color = DarkBg - | LightBg +data Color = DarkBg + | LightBg | NoColor newtype Args = Args { color :: Color } colorReader :: ReadM Color -colorReader = maybeReader colorReader' - where - colorReader' "dark-bg" = Just DarkBg - colorReader' "light-bg" = Just LightBg - colorReader' "no-color" = Just NoColor - colorReader' _ = Nothing +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 +args = Args <$> option colorReader ( long "color" <> short 'c' @@ -63,7 +64,7 @@ args = Args main :: IO () main = do - args' <- execParser opts + args' <- execParser opts input <- T.getContents let printOpt = getPrintOpt $ color args' output = pStringOpt printOpt $ unpack input diff --git a/pretty-simple.cabal b/pretty-simple.cabal index 0030894..15c7986 100644 --- a/pretty-simple.cabal +++ b/pretty-simple.cabal @@ -50,6 +50,7 @@ executable pretty-simple hs-source-dirs: app build-depends: base , pretty-simple + , semigroups , text , optparse-applicative default-language: Haskell2010 From 7845f5ea1977218815b4a11f7a9e879a33b5c917 Mon Sep 17 00:00:00 2001 From: Dennis Gosnell Date: Tue, 10 Jul 2018 22:50:16 +0900 Subject: [PATCH 3/3] Try to get travis to succeed again. --- app/Main.hs | 2 +- pretty-simple.cabal | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index b227449..25ebad1 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -29,7 +29,7 @@ import qualified Data.Text.Lazy.IO as LT import Options.Applicative ( Parser, ReadM, execParser, fullDesc, help, helper, info, long , option, progDesc, readerError, short, showDefaultWith, str, value, (<**>)) -import Data.Semigroup ((<>)) +import Data.Monoid ((<>)) import Text.Pretty.Simple ( pStringOpt, OutputOptions , defaultOutputOptionsDarkBg diff --git a/pretty-simple.cabal b/pretty-simple.cabal index 15c7986..0030894 100644 --- a/pretty-simple.cabal +++ b/pretty-simple.cabal @@ -50,7 +50,6 @@ executable pretty-simple hs-source-dirs: app build-depends: base , pretty-simple - , semigroups , text , optparse-applicative default-language: Haskell2010