diff --git a/alea.cabal b/alea.cabal index 4601d80..ba7161f 100644 --- a/alea.cabal +++ b/alea.cabal @@ -1,5 +1,5 @@ name: alea -version: 0.1.0.0 +version: 0.1.1.0 synopsis: a diceware passphrase generator description: diff --git a/main.hs b/main.hs index c212f26..260001c 100644 --- a/main.hs +++ b/main.hs @@ -2,13 +2,11 @@ import System.IO import System.Console.CmdArgs -import System.Environment (getArgs, withArgs) -import Control.Monad (when) +import Control.Monad (when) import Paths_alea (getDataFileName) - -import Diceware -import Random +import Alea.Diceware +import Alea.Random _NAME = "Alea" _VERSION = "0.1.0" @@ -41,25 +39,26 @@ getProgArgs = cmdArgs $ progArgs main :: IO () -main = do - args <- getArgs - opts <- getProgArgs - exec opts +main = getProgArgs >>= defaults >>= exec + +-- Assign defaults value to unspecified args +defaults :: Args -> IO Args +defaults args@Args{..} = do + dictionary <- getDataFileName "dict/diceware" >>= readFile + return args + { dictionary = dictionary + , phraseLength = if phraseLength == 0 then 6 else phraseLength + } exec :: Args -> IO () -exec opts@Args{..} = do - opts <- getProgArgs - defaultDict <- getDataFileName "diceware" - file <- readFile (if null dictionary then defaultDict else dictionary) +exec args@Args{..} = if interactive - then interact (unlines . map (dice file) . lines) + then interact (unlines . map dice . lines) else do - phrase <- randWords (size $ parseDiceware file) phraseLength' - putStrLn . unwords . map (dice' file) $ phrase - when (phrases > 1) $ exec opts {phrases = phrases - 1} + randWords dictSize phraseLength >>= putStrLn . unwords . map dice' + when (phrases > 1) $ exec args {phrases = phrases - 1} where -- helpers - dice x n = readDiceware (parseDiceware x) (read n :: Int) - dice' x n = readDiceware' (parseDiceware x) n - -- default arguments - phraseLength' = if phraseLength == 0 then 6 else phraseLength + dice n = readDiceware (parseDiceware dictionary) (read n :: Int) + dice' n = readDiceware' (parseDiceware dictionary) n + dictSize = size $ parseDiceware dictionary