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
32 data Opt
= LockSize
Int | ForceCurses | Help | Version
33 deriving (Eq
, Ord
, Show)
35 [ Option
['c
'] ["curses"] (NoArg ForceCurses
) "force curses UI"
36 , Option
['s
'] ["locksize"] (ReqArg
(LockSize
. read) "SIZE") "locksize"
37 , Option
['h
'] ["help"] (NoArg Help
) "show usage information"
38 , Option
['v
'] ["version"] (NoArg Version
) "show version information"
42 usage
= usageInfo header options
43 where header
= "Usage: intricacy [OPTION...] [file]"
45 parseArgs
:: [String] -> IO ([Opt
],[String])
47 case getOpt Permute options argv
of
48 (o
,n
,[]) -> return (o
,n
)
49 (_
,_
,errs
) -> ioError (userError (concat errs
++ usage
))
51 setup
:: IO (Maybe Lock
,[Opt
],Maybe String)
54 (opts
,args
) <- parseArgs argv
55 when (Help `
elem` opts
) $ putStr usage
>> exitSuccess
56 when (Version `
elem` opts
) $ putStrLn version
>> exitSuccess
57 let size
= fromMaybe 8 $ listToMaybe [ size | LockSize size
<- opts
]
58 curDir
<- getCurrentDirectory
59 (fromJust <$>) $ runMaybeT
$ msum
61 path
<- liftMaybe
((curDir
</>) <$> listToMaybe args
)
63 lock
<- reframe
.fst <$> MaybeT
(readLock path
)
64 return (Just lock
,opts
,Just path
)
65 , return (Just
$ baseLock size
, opts
, Just path
) ]
66 , return (Nothing
,opts
,Nothing
) ]
68 main
' :: (UIMonad s
, UIMonad c
) =>
69 Maybe (s MainState
-> IO (Maybe MainState
)) ->
70 Maybe (c MainState
-> IO (Maybe MainState
)) -> IO ()
71 main
' msdlUI mcursesUI
= do
72 (mlock
,opts
,mpath
) <- setup
73 initMState
<- case mlock
of
74 Nothing
-> initMetaState
75 Just lock
-> return $ newEditState lock Nothing mpath
76 void
$ runMaybeT
$ msum [ do
79 guard $ ForceCurses `
notElem` opts
80 sdlUI
<- liftMaybe msdlUI
81 MaybeT
$ sdlUI
$ interactUI `execStateT` initMState
83 cursesUI
<- liftMaybe mcursesUI
84 MaybeT
$ cursesUI
$ interactUI `execStateT` initMState
86 when (isNothing mlock
) $ lift
$ writeMetaState finalState