Load from and save to file

This commit is contained in:
Joscha 2018-01-05 18:23:47 +00:00
parent e9b4edc6e0
commit 8973d30cae

View file

@ -10,6 +10,7 @@ import Data.Char
import Data.Time import Data.Time
import System.Console.Haskeline import System.Console.Haskeline
import System.Console.Haskeline.History import System.Console.Haskeline.History
import System.Environment
import System.Random.Shuffle import System.Random.Shuffle
type Input = InputT IO type Input = InputT IO
@ -194,9 +195,28 @@ run elms = do
Just x -> do Just x -> do
outputStrLn $ "Unknown command " ++ show x ++ ". Try \"help\" for a list of commands." outputStrLn $ "Unknown command " ++ show x ++ ". Try \"help\" for a list of commands."
run elms run elms
-- Maybe save cards?
fromFile :: FilePath -> Input ()
fromFile filepath = do
time <- lift $ getCurrentTime
content <- lift $ readFile filepath
let maybeElms = parseElementsMaybe time content
case maybeElms of
Nothing -> outputStrLn $ "Could not parse contents of " ++ show filepath ++ "."
Just elms -> do
newElms <- run elms
toFile filepath newElms
toFile :: FilePath -> Elements -> Input ()
toFile filepath elms = void $ runMaybeT $ do
result <- promptYesNo "Do you want to save the cards?"
when result $ do
lift $ lift $ writeFile filepath $ elementsToString elms
main :: IO () main :: IO ()
main = do main = runInputT inputSettings $ do
elms <- runInputT inputSettings $ run testElements name <- lift $ getProgName
putStrLn $ elementsToString elms args <- lift $ getArgs
if length args == 1
then fromFile (args !! 0)
else outputStrLn $ " USAGE: " ++ name ++ " <cards file>"