packages feed

wordchoice-0.1.1.1 / src / Data / Text / WordCount / Exec.hs

module Data.Text.WordCount.Exec whereimport           Data.Maybeimport           Data.Monoidimport qualified Data.Text.IO                 as TLIOimport           Data.Text.WordCountimport           Data.Text.WordCount.FileReadimport           Data.Versionimport           Options.Applicativeimport           Paths_wordchoice-- | Program datatype to be parseddata Program = Program { file         :: FilePath                       , num          :: Maybe Int                       , output       :: Maybe FilePath                       , filterOutput :: Bool } -- TODO add option for separators-- | Command line argument parserprogram :: Parser Programprogram = Program    <$> (argument str        (metavar "FILEPATH"        <> completer (bashCompleter "file -o plusdirs")        <> help "File to analyze"))    <*> (optional (read <$> strOption        (short 'n'        <> long "number"        <>  metavar "NUM"        <> help "Top NUM words will be listed")))    <*> (optional (strOption        (short 'o'        <> long "output"        <>  metavar "OUTPUT"        <> help "Filepath for output graph")))    <*> switch        (short 'f'        <> long "filter"        <> help "Filter common English words from output.")-- | Parse for version infoversionInfo :: Parser (a -> a)versionInfo = infoOption    ("wordchoice version: " ++ showVersion version)    (short 'v' <> long "version" <> help "Show version")-- | Wraps parser with help parserwrapper :: ParserInfo Programwrapper = info (helper <*> versionInfo <*> program)    (fullDesc    <> progDesc "Word choice is a command-line meant to help you improve your writing. Simply point it to a file containing text and it will list your most frequently used words and their frequencies."    <> header "Word choice command-line utility")-- | Actual executableexec :: IO ()exec = execParser wrapper >>= pick-- | Run parsed recordpick :: Program -> IO ()pick rec = let n = fromMaybe 25 (num rec) in do    contents <- globFile (file rec)    let pick = if not (filterOutput rec) then topN n else filterTop n small    TLIO.putStrLn . displayWords . pick $ contents    case output rec of        (Just out) -> flip makeFile out . topN n $ contents        _          -> pure ()