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.
|
2015-08-18 03:44:18 +03:00
|
|
|
|
|
|
|
TODO:
|
|
|
|
reg: don't repeat date/description for postings in same txn
|
|
|
|
reg: show a hledger-web-style register
|
|
|
|
--
|
|
|
|
switch to next brick release
|
|
|
|
reg: use full width
|
|
|
|
page up/down
|
|
|
|
home/end
|
|
|
|
search
|
|
|
|
filter
|
|
|
|
--
|
|
|
|
show journal entries
|
|
|
|
add
|
|
|
|
edit
|
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-18 03:44:18 +03:00
|
|
|
-- import Data.Default
|
|
|
|
-- import Data.Monoid --
|
|
|
|
-- import Data.List
|
|
|
|
-- import Data.Maybe
|
|
|
|
-- 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
|
|
|
|
-- import qualified Brick.Types as T
|
|
|
|
import qualified Brick.Main as M
|
|
|
|
-- import qualified Brick.AttrMap as A
|
|
|
|
-- import qualified Brick.Widgets.Border as B
|
|
|
|
-- import qualified Brick.Widgets.Center as C
|
|
|
|
-- import qualified Brick.Widgets.List as L
|
|
|
|
-- import Brick.Util (fg, on)
|
|
|
|
-- import Brick.Widgets.Core
|
|
|
|
|
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
|
|
|
|
import Hledger.UI.UIUtils
|
|
|
|
import Hledger.UI.AccountsScreen as AS
|
|
|
|
import Hledger.UI.RegisterScreen as RS
|
2010-11-25 10:10:38 +03:00
|
|
|
|
2015-08-18 03:44:18 +03:00
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- | The available screens.
|
|
|
|
appScreens = [
|
|
|
|
AS.screen
|
|
|
|
,RS.screen
|
|
|
|
]
|
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
|
|
|
|
args = words' $ query_ $ reportopts_ $ cliopts_ opts
|
|
|
|
scr = head appScreens
|
|
|
|
st = (sInitFn scr) d args
|
|
|
|
AppState{
|
|
|
|
aopts=opts
|
|
|
|
,aargs=args
|
|
|
|
,ajournal=j
|
|
|
|
,aScreen=scr
|
|
|
|
,aPrevScreens=[]
|
|
|
|
}
|
|
|
|
|
|
|
|
app :: M.App (AppState) V.Event
|
|
|
|
app = M.App {
|
|
|
|
M.appLiftVtyEvent = id
|
|
|
|
, M.appStartEvent = return
|
|
|
|
, M.appAttrMap = const attrMap
|
|
|
|
, M.appChooseCursor = M.showFirstCursor
|
|
|
|
, M.appHandleEvent = \st ev -> (sHandleFn $ aScreen st) st ev
|
|
|
|
, M.appDraw = \st -> (sDrawFn $ aScreen st) st
|
|
|
|
}
|
|
|
|
|
|
|
|
void $ M.defaultMain app st
|