From 5880d1186960884230c5221641b6ca55021df857 Mon Sep 17 00:00:00 2001 From: Rnhmjoj Date: Fri, 12 Dec 2014 22:14:34 +0100 Subject: [PATCH] Fix dictionary reading --- main.hs | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/main.hs b/main.hs index a8b8e02..21b9f0c 100644 --- a/main.hs +++ b/main.hs @@ -1,7 +1,9 @@ {-# LANGUAGE RecordWildCards #-} import System.IO -import Control.Monad (when) +import System.Console.ArgParser +import Control.Monad +import Control.Applicative import Paths_alea (getDataFileName) import Alea.Diceware @@ -28,18 +30,26 @@ interface = (mkApp =<< parser) 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 exec :: ProgArgs -> IO () exec args@ProgArgs{..} = if interactive - then interact (unlines . map dice . lines) - else do - randWords dictSize phraseLength >>= putStrLn . unwords . map dice' - when (phrases > 1) $ exec args {phrases = phrases - 1} + then interact (unlines . map dice . lines) + else do + randWords dictSize phraseLength >>= putStrLn . unwords . map dice' + when (phrases > 1) $ exec args {phrases = phrases - 1} where - (dict, dictSize) = (parseDiceware dictionary, length dict) - dice n = readDiceware dict (read n :: Int) - dice' n = readDiceware' dict n \ No newline at end of file + (dict, dictSize) = (parseDiceware dictionary, length dict) + dice n = readDiceware dict (read n :: Int) + dice' n = readDiceware' dict n \ No newline at end of file