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

add --compact switch to the pretty-simple executable #111

Merged
merged 1 commit into from
Jun 7, 2022
Merged
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
29 changes: 20 additions & 9 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,25 @@ 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, value)
( 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

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

colorReader :: ReadM Color
Expand All @@ -65,6 +68,11 @@ args = Args
<> showDefaultWith (const "dark-bg")
<> value DarkBg
)
<*> switch
( long "compact"
<> short 'C'
<> help "Compact output"
)

versionOption :: Parser (a -> a)
versionOption =
Expand All @@ -79,16 +87,19 @@ main :: IO ()
main = do
args' <- execParser opts
input <- T.getContents
let printOpt = getPrintOpt $ color args'
output = pStringOpt printOpt $ unpack input
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 :: Color -> OutputOptions
getPrintOpt DarkBg = defaultOutputOptionsDarkBg
getPrintOpt LightBg = defaultOutputOptionsLightBg
getPrintOpt NoColor = defaultOutputOptionsNoColor
getPrintOpt :: Args -> OutputOptions
getPrintOpt as =
(getColorOpt (color as)) {outputOptionsCompact = compact as}

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