Fix dictionary reading

This commit is contained in:
Rnhmjoj 2014-12-12 22:14:34 +01:00
parent 04d4119b7a
commit 5880d11869

28
main.hs
View File

@ -1,7 +1,9 @@
{-# LANGUAGE RecordWildCards #-} {-# LANGUAGE RecordWildCards #-}
import System.IO import System.IO
import Control.Monad (when) import System.Console.ArgParser
import Control.Monad
import Control.Applicative
import Paths_alea (getDataFileName) import Paths_alea (getDataFileName)
import Alea.Diceware import Alea.Diceware
@ -28,18 +30,26 @@ interface =
(mkApp =<< parser) (mkApp =<< parser)
main :: IO () main :: IO ()
main = getProgArgs >>= defaults >>= exec main = interface >>= flip runApp (readDict >=> exec)
-- Default path of the dictionary
path :: IO FilePath
path = getDataFileName "dict/diceware"
-- Read dictionary file
readDict :: ProgArgs -> IO ProgArgs
readDict args@ProgArgs{..} =
readFile dictionary >>= \x -> return args {dictionary = x}
-- Main function -- Main function
exec :: ProgArgs -> IO () exec :: ProgArgs -> IO ()
exec args@ProgArgs{..} = exec args@ProgArgs{..} =
if interactive if interactive
then interact (unlines . map dice . lines) then interact (unlines . map dice . lines)
else do else do
randWords dictSize phraseLength >>= putStrLn . unwords . map dice' randWords dictSize phraseLength >>= putStrLn . unwords . map dice'
when (phrases > 1) $ exec args {phrases = phrases - 1} when (phrases > 1) $ exec args {phrases = phrases - 1}
where where
(dict, dictSize) = (parseDiceware dictionary, length dict) (dict, dictSize) = (parseDiceware dictionary, length dict)
dice n = readDiceware dict (read n :: Int) dice n = readDiceware dict (read n :: Int)
dice' n = readDiceware' dict n dice' n = readDiceware' dict n