1 -- This file is part of Intricacy
2 -- Copyright (C) 2013 Martin Bays <mbays@sdf.org>
4 -- This program is free software: you can redistribute it and/or modify
5 -- it under the terms of version 3 of the GNU General Public License as
6 -- published by the Free Software Foundation, or any later version.
8 -- You should have received a copy of the GNU General Public License
9 -- along with this program. If not, see http://www.gnu.org/licenses/.
14 import Control
.Applicative
16 import Control
.Monad
.Trans
17 import Control
.Monad
.Trans
.Maybe
18 import Control
.Monad
.Trans
.State
20 import System
.Console
.GetOpt
21 import System
.Directory
22 import System
.Environment
24 import System
.FilePath
39 deriving (Eq
, Ord
, Show)
42 [ Option
['d
'] ["datadir"] (ReqArg DataDir
"PATH") "user data and conf directory (default: ~/.intricacy)"
43 , Option
['c
'] ["curses"] (NoArg ForceCurses
) "force curses UI"
44 , Option
['s
'] ["locksize"] (ReqArg
(LockSize
. read) "SIZE") "locksize"
45 , Option
['h
'] ["help"] (NoArg Help
) "show usage information"
46 , Option
['v
'] ["version"] (NoArg Version
) "show version information"
50 usage
= usageInfo header options
51 where header
= "Usage: intricacy [OPTION...] [file]"
53 parseArgs
:: [String] -> IO ([Opt
],[String])
55 case getOpt Permute options argv
of
56 (o
,n
,[]) -> return (o
,n
)
57 (_
,_
,errs
) -> ioError (userError (concat errs
++ usage
))
59 setup
:: IO (Maybe (Lock
, Maybe Solution
), [Opt
], Maybe String)
62 (opts
,args
) <- parseArgs argv
63 when (Help `
elem` opts
) $ putStr usage
>> exitSuccess
64 when (Version `
elem` opts
) $ putStrLn version
>> exitSuccess
65 let size
= fromMaybe 8 $ listToMaybe [ size | LockSize size
<- opts
]
66 mapM_ (setEnv
"INTRICACY_PATH") [ dir | DataDir dir
<- opts
]
68 curDir
<- getCurrentDirectory
69 (fromJust <$>) $ runMaybeT
$ msum
71 path
<- liftMaybe
((curDir
</>) <$> listToMaybe args
)
73 (lock
, msoln
) <- MaybeT
(readLock path
)
74 return (Just
(reframe lock
, msoln
), opts
, Just path
)
75 , return (Just
(baseLock size
, Nothing
), opts
, Just path
) ]
76 , return (Nothing
, opts
, Nothing
) ]
78 main
' :: (UIMonad s
, UIMonad c
) =>
79 Maybe (s MainState
-> IO (Maybe MainState
)) ->
80 Maybe (c MainState
-> IO (Maybe MainState
)) -> IO ()
81 main
' msdlUI mcursesUI
= do
82 (mlock
,opts
,mpath
) <- setup
83 initMState
<- case mlock
of
84 Just
(lock
, msoln
) -> return $ newEditState lock msoln mpath
85 Nothing
-> initMetaState
86 void
$ runMaybeT
$ msum [ do
89 guard $ ForceCurses `
notElem` opts
90 sdlUI
<- liftMaybe msdlUI
91 MaybeT
$ sdlUI
$ interactUI `execStateT` initMState
93 cursesUI
<- liftMaybe mcursesUI
94 MaybeT
$ cursesUI
$ interactUI `execStateT` initMState
96 lift
$ writeMetaState finalState