2010-11-25 10:10:38 +03:00
|
|
|
{-|
|
2015-08-13 21:22:40 +03:00
|
|
|
hledger-ui - a hledger add-on providing a curses-style interface.
|
2015-08-18 03:44:18 +03:00
|
|
|
Copyright (c) 2007-2015 Simon Michael <simon@joyful.com>
|
2010-11-25 10:10:38 +03:00
|
|
|
Released under GPL version 3 or later.
|
2008-12-08 20:27:16 +03:00
|
|
|
-}
|
2015-08-18 03:44:18 +03:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
|
|
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
2008-12-08 20:27:16 +03:00
|
|
|
|
2015-08-18 03:44:18 +03:00
|
|
|
module Hledger.UI.Main where
|
2010-11-25 10:10:38 +03:00
|
|
|
|
2015-08-18 03:44:18 +03:00
|
|
|
-- import Control.Applicative
|
|
|
|
-- import Control.Lens ((^.))
|
2011-06-06 22:59:24 +04:00
|
|
|
import Control.Monad
|
2015-08-23 03:46:57 +03:00
|
|
|
-- import Control.Monad.IO.Class (liftIO)
|
2015-08-18 03:44:18 +03:00
|
|
|
-- import Data.Default
|
|
|
|
-- import Data.Monoid --
|
|
|
|
-- import Data.List
|
2015-08-23 03:46:57 +03:00
|
|
|
import Data.Maybe
|
2015-08-18 03:44:18 +03:00
|
|
|
-- import Data.Time.Calendar
|
|
|
|
-- import Safe
|
2011-08-22 18:55:39 +04:00
|
|
|
import System.Exit
|
2010-11-25 10:10:38 +03:00
|
|
|
|
2015-08-18 03:44:18 +03:00
|
|
|
import qualified Graphics.Vty as V
|
2015-08-20 14:54:23 +03:00
|
|
|
import Brick
|
2015-08-18 03:44:18 +03:00
|
|
|
|
2011-07-18 03:05:56 +04:00
|
|
|
import Hledger
|
2015-08-12 05:08:33 +03:00
|
|
|
import Hledger.Cli hiding (progname,prognameandversion,green)
|
2015-08-13 21:22:40 +03:00
|
|
|
import Hledger.UI.Options
|
2015-08-18 03:44:18 +03:00
|
|
|
import Hledger.UI.UITypes
|
2015-08-23 03:46:57 +03:00
|
|
|
-- import Hledger.UI.UIUtils
|
|
|
|
import Hledger.UI.Theme
|
2015-08-18 03:44:18 +03:00
|
|
|
import Hledger.UI.AccountsScreen as AS
|
2015-08-20 21:05:42 +03:00
|
|
|
-- import Hledger.UI.RegisterScreen as RS
|
2010-11-25 10:10:38 +03:00
|
|
|
|
2015-08-18 03:44:18 +03:00
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
2010-11-25 10:10:38 +03:00
|
|
|
main :: IO ()
|
|
|
|
main = do
|
2015-08-13 21:22:40 +03:00
|
|
|
opts <- getHledgerUIOpts
|
2015-08-12 05:08:33 +03:00
|
|
|
-- when (debug_ $ cliopts_ opts) $ printf "%s\n" prognameandversion >> printf "opts: %s\n" (show opts)
|
2015-08-18 03:44:18 +03:00
|
|
|
run opts
|
2010-11-25 10:10:38 +03:00
|
|
|
where
|
2011-08-16 02:50:09 +04:00
|
|
|
run opts
|
2015-08-18 03:44:18 +03:00
|
|
|
| "help" `inRawOpts` (rawopts_ $ cliopts_ opts) = putStr (showModeHelp uimode) >> exitSuccess
|
|
|
|
| "version" `inRawOpts` (rawopts_ $ cliopts_ opts) = putStrLn prognameandversion >> exitSuccess
|
|
|
|
| "binary-filename" `inRawOpts` (rawopts_ $ cliopts_ opts) = putStrLn (binaryfilename progname)
|
|
|
|
| otherwise = withJournalDo' opts runBrickUi
|
2011-08-16 02:50:09 +04:00
|
|
|
|
2015-08-13 21:22:40 +03:00
|
|
|
withJournalDo' :: UIOpts -> (UIOpts -> Journal -> IO ()) -> IO ()
|
2011-08-16 02:50:09 +04:00
|
|
|
withJournalDo' opts cmd = do
|
2015-08-12 05:08:33 +03:00
|
|
|
-- journalFilePathFromOpts (cliopts_ opts) >>= readJournalFile Nothing >>=
|
|
|
|
-- either error' (cmd opts . journalApplyAliases (aliasesFromOpts $ cliopts_ opts))
|
|
|
|
-- XXX head should be safe for now
|
|
|
|
(head `fmap` journalFilePathFromOpts (cliopts_ opts)) >>= readJournalFile Nothing Nothing True >>=
|
2011-08-16 02:50:09 +04:00
|
|
|
either error' (cmd opts . journalApplyAliases (aliasesFromOpts $ cliopts_ opts))
|
2008-12-08 20:27:16 +03:00
|
|
|
|
2015-08-18 03:44:18 +03:00
|
|
|
runBrickUi :: UIOpts -> Journal -> IO ()
|
|
|
|
runBrickUi opts j = do
|
2011-06-14 03:28:39 +04:00
|
|
|
d <- getCurrentDay
|
2008-12-08 20:27:16 +03:00
|
|
|
|
2015-08-18 03:44:18 +03:00
|
|
|
let
|
2015-08-23 03:46:57 +03:00
|
|
|
theme = maybe defaultTheme (fromMaybe defaultTheme . getTheme) $
|
|
|
|
maybestringopt "theme" $ rawopts_ $ cliopts_ opts
|
|
|
|
scr = AS.screen
|
2015-08-28 08:45:49 +03:00
|
|
|
st = (sInitFn scr) d
|
2015-08-18 03:44:18 +03:00
|
|
|
AppState{
|
|
|
|
aopts=opts
|
|
|
|
,ajournal=j
|
|
|
|
,aScreen=scr
|
|
|
|
,aPrevScreens=[]
|
|
|
|
}
|
|
|
|
|
2015-08-20 14:54:23 +03:00
|
|
|
app :: App (AppState) V.Event
|
|
|
|
app = App {
|
|
|
|
appLiftVtyEvent = id
|
|
|
|
, appStartEvent = return
|
2015-08-23 03:46:57 +03:00
|
|
|
, appAttrMap = const theme
|
2015-08-20 14:54:23 +03:00
|
|
|
, appChooseCursor = showFirstCursor
|
|
|
|
, appHandleEvent = \st ev -> (sHandleFn $ aScreen st) st ev
|
|
|
|
, appDraw = \st -> (sDrawFn $ aScreen st) st
|
2015-08-18 03:44:18 +03:00
|
|
|
}
|
|
|
|
|
2015-08-20 14:54:23 +03:00
|
|
|
void $ defaultMain app st
|
2015-08-23 03:46:57 +03:00
|
|
|
|