2015-08-18 03:44:18 +03:00
|
|
|
-- The accounts screen, showing accounts and balances like the CLI balance command.
|
|
|
|
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2015-08-28 08:45:49 +03:00
|
|
|
{-# LANGUAGE RecordWildCards #-}
|
2015-08-18 03:44:18 +03:00
|
|
|
|
|
|
|
module Hledger.UI.AccountsScreen
|
|
|
|
(screen)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Control.Lens ((^.))
|
|
|
|
-- import Control.Monad
|
|
|
|
import Control.Monad.IO.Class
|
|
|
|
-- import Data.Default
|
|
|
|
import Data.List
|
2015-08-28 08:45:49 +03:00
|
|
|
import Data.Maybe
|
2015-08-25 16:56:04 +03:00
|
|
|
import Data.Monoid
|
2015-08-18 03:44:18 +03:00
|
|
|
import Data.Time.Calendar (Day)
|
2015-08-25 16:56:04 +03:00
|
|
|
import System.FilePath (takeFileName)
|
2015-08-18 03:44:18 +03:00
|
|
|
import qualified Data.Vector as V
|
2015-08-23 03:46:57 +03:00
|
|
|
import Graphics.Vty as Vty
|
2015-08-20 14:54:23 +03:00
|
|
|
import Brick
|
|
|
|
import Brick.Widgets.List
|
2015-08-23 03:46:57 +03:00
|
|
|
-- import Brick.Widgets.Border
|
|
|
|
-- import Brick.Widgets.Center
|
2015-08-18 03:44:18 +03:00
|
|
|
|
|
|
|
import Hledger
|
|
|
|
import Hledger.Cli hiding (progname,prognameandversion,green)
|
|
|
|
-- import Hledger.Cli.Options (defaultBalanceLineFormat)
|
|
|
|
import Hledger.UI.Options
|
2015-08-23 03:46:57 +03:00
|
|
|
-- import Hledger.UI.Theme
|
2015-08-18 03:44:18 +03:00
|
|
|
import Hledger.UI.UITypes
|
|
|
|
import Hledger.UI.UIUtils
|
2015-08-28 08:53:12 +03:00
|
|
|
import qualified Hledger.UI.RegisterScreen as RS (screen)
|
2015-08-18 03:44:18 +03:00
|
|
|
|
|
|
|
screen = AccountsScreen{
|
2015-08-20 14:54:23 +03:00
|
|
|
asState = list "accounts" V.empty 1
|
2015-08-28 08:45:49 +03:00
|
|
|
,sInitFn = initAccountsScreen Nothing
|
2015-08-18 03:44:18 +03:00
|
|
|
,sDrawFn = drawAccountsScreen
|
|
|
|
,sHandleFn = handleAccountsScreen
|
|
|
|
}
|
|
|
|
|
2015-08-28 08:45:49 +03:00
|
|
|
initAccountsScreen :: Maybe AccountName -> Day -> AppState -> AppState
|
2015-08-28 16:36:07 +03:00
|
|
|
initAccountsScreen mselacct d st@AppState{aopts=opts, ajournal=j, aScreen=s@AccountsScreen{}} =
|
2015-08-28 08:45:49 +03:00
|
|
|
st{aScreen=s{asState=is''}}
|
2015-08-18 03:44:18 +03:00
|
|
|
where
|
2015-08-20 14:54:23 +03:00
|
|
|
is' = list (Name "accounts") (V.fromList items) 1
|
2015-08-28 08:45:49 +03:00
|
|
|
-- crazy hacks dept.
|
|
|
|
-- when we're adjusting depth, mselacct is the account that was selected previously,
|
|
|
|
-- in which case try and keep the selection near where it was
|
|
|
|
is'' = case mselacct of
|
|
|
|
Nothing -> is'
|
|
|
|
Just a -> -- vScrollToBeginning $ viewportScroll "accounts"
|
|
|
|
maybe is' (flip listMoveTo is') mi
|
|
|
|
where
|
|
|
|
mi = findIndex (\((acct,_,_),_) -> acct==a') items
|
|
|
|
a' = maybe a (flip clipAccountName a) $ depth_ ropts
|
|
|
|
|
|
|
|
q = queryFromOpts d ropts
|
|
|
|
-- query_="cur:\\$"} -- XXX limit to one commodity to ensure one-line items
|
|
|
|
--{query_=unwords' $ locArgs l}
|
|
|
|
ropts = (reportopts_ cliopts)
|
|
|
|
{
|
|
|
|
balancetype_=HistoricalBalance -- XXX balanceReport doesn't respect this yet
|
|
|
|
}
|
|
|
|
cliopts = cliopts_ opts
|
|
|
|
convert | value_ ropts = balanceReportValue j valuedate
|
|
|
|
| otherwise = id
|
2015-08-18 03:44:18 +03:00
|
|
|
where
|
2015-08-28 08:45:49 +03:00
|
|
|
valuedate = fromMaybe d $ queryEndDate False q
|
|
|
|
|
|
|
|
(items,_total) = convert $ balanceReport ropts q j
|
|
|
|
|
2015-08-18 03:44:18 +03:00
|
|
|
initAccountsScreen _ _ _ = error "init function called with wrong screen type, should not happen"
|
|
|
|
|
|
|
|
drawAccountsScreen :: AppState -> [Widget]
|
2015-08-25 16:56:04 +03:00
|
|
|
drawAccountsScreen st@AppState{aopts=uopts, ajournal=j, aScreen=AccountsScreen{asState=is}} = [ui]
|
2015-08-18 03:44:18 +03:00
|
|
|
where
|
2015-08-28 08:45:49 +03:00
|
|
|
toplabel = files
|
2015-08-26 02:01:12 +03:00
|
|
|
<+> str " accounts"
|
2015-08-28 08:45:49 +03:00
|
|
|
<+> borderQueryStr querystr
|
|
|
|
<+> borderDepthStr depth
|
2015-08-25 16:56:04 +03:00
|
|
|
<+> str " ("
|
|
|
|
<+> cur
|
|
|
|
<+> str " of "
|
|
|
|
<+> total
|
|
|
|
<+> str ")"
|
2015-08-26 02:01:12 +03:00
|
|
|
files = case journalFilePaths j of
|
|
|
|
[] -> str ""
|
|
|
|
[f] -> withAttr ("border" <> "bold") $ str $ takeFileName f
|
|
|
|
[f,_] -> (withAttr ("border" <> "bold") $ str $ takeFileName f) <+> str " (& 1 included file)"
|
|
|
|
f:fs -> (withAttr ("border" <> "bold") $ str $ takeFileName f) <+> str (" (& " ++ show (length fs) ++ " included files)")
|
2015-08-25 16:56:04 +03:00
|
|
|
querystr = query_ $ reportopts_ $ cliopts_ uopts
|
2015-08-28 08:45:49 +03:00
|
|
|
depth = depth_ $ reportopts_ $ cliopts_ uopts
|
2015-08-20 14:54:23 +03:00
|
|
|
cur = str (case is^.listSelectedL of
|
|
|
|
Nothing -> "-"
|
|
|
|
Just i -> show (i + 1))
|
|
|
|
total = str $ show $ length $ is^.listElementsL
|
2015-08-23 03:46:57 +03:00
|
|
|
|
2015-08-20 14:54:23 +03:00
|
|
|
items = listElements is
|
2015-08-18 03:44:18 +03:00
|
|
|
flat = flat_ $ reportopts_ $ cliopts_ $ aopts st
|
|
|
|
acctcolwidth = maximum $
|
|
|
|
V.map
|
|
|
|
(\((full,short,indent),_) ->
|
|
|
|
if flat then length full else length short + indent*2)
|
|
|
|
items
|
|
|
|
fmt = OneLine [ -- use a one-line format, List elements must have equal height
|
|
|
|
FormatField True (Just 2) Nothing DepthSpacerField
|
|
|
|
, FormatField True (Just acctcolwidth) Nothing AccountField
|
|
|
|
, FormatLiteral " "
|
|
|
|
, FormatField False (Just 40) Nothing TotalField
|
|
|
|
]
|
|
|
|
|
2015-08-28 08:45:49 +03:00
|
|
|
bottomlabel = borderKeysStr [
|
|
|
|
-- "up/down/pgup/pgdown/home/end: move"
|
|
|
|
"1-0: adjust depth limit"
|
|
|
|
,"right: show transactions"
|
|
|
|
,"q: quit"
|
|
|
|
]
|
|
|
|
|
|
|
|
ui = defaultLayout toplabel bottomlabel $ renderList is (drawAccountsItem fmt)
|
2015-08-23 03:46:57 +03:00
|
|
|
|
2015-08-18 03:44:18 +03:00
|
|
|
drawAccountsScreen _ = error "draw function called with wrong screen type, should not happen"
|
|
|
|
|
|
|
|
drawAccountsItem :: StringFormat -> Bool -> BalanceReportItem -> Widget
|
2015-08-23 03:46:57 +03:00
|
|
|
drawAccountsItem fmt _sel item =
|
|
|
|
Widget Greedy Fixed $ do
|
|
|
|
-- c <- getContext
|
|
|
|
let
|
|
|
|
showitem = intercalate "\n" . balanceReportItemAsText defreportopts fmt
|
|
|
|
render $ str $ showitem item
|
2015-08-18 03:44:18 +03:00
|
|
|
|
2015-08-20 14:54:23 +03:00
|
|
|
handleAccountsScreen :: AppState -> Vty.Event -> EventM (Next AppState)
|
2015-08-28 16:36:07 +03:00
|
|
|
handleAccountsScreen st@AppState{aScreen=scr@AccountsScreen{asState=is}} e = do
|
2015-08-18 03:44:18 +03:00
|
|
|
d <- liftIO getCurrentDay
|
|
|
|
-- c <- getContext
|
|
|
|
-- let h = c^.availHeightL
|
2015-08-20 14:54:23 +03:00
|
|
|
-- moveSel n l = listMoveBy n l
|
2015-08-28 08:45:49 +03:00
|
|
|
let
|
|
|
|
acct = case listSelectedElement is of
|
|
|
|
Just (_, ((a, _, _), _)) -> a
|
|
|
|
Nothing -> ""
|
2015-08-18 03:44:18 +03:00
|
|
|
case e of
|
2015-08-20 14:54:23 +03:00
|
|
|
Vty.EvKey Vty.KEsc [] -> halt st
|
|
|
|
Vty.EvKey (Vty.KChar 'q') [] -> halt st
|
2015-08-28 08:45:49 +03:00
|
|
|
Vty.EvKey (Vty.KChar '0') [] -> continue $ initAccountsScreen (Just acct) d $ setDepth 0 st
|
|
|
|
Vty.EvKey (Vty.KChar '1') [] -> continue $ initAccountsScreen (Just acct) d $ setDepth 1 st
|
|
|
|
Vty.EvKey (Vty.KChar '2') [] -> continue $ initAccountsScreen (Just acct) d $ setDepth 2 st
|
|
|
|
Vty.EvKey (Vty.KChar '3') [] -> continue $ initAccountsScreen (Just acct) d $ setDepth 3 st
|
|
|
|
Vty.EvKey (Vty.KChar '4') [] -> continue $ initAccountsScreen (Just acct) d $ setDepth 4 st
|
|
|
|
Vty.EvKey (Vty.KChar '5') [] -> continue $ initAccountsScreen (Just acct) d $ setDepth 5 st
|
|
|
|
Vty.EvKey (Vty.KChar '6') [] -> continue $ initAccountsScreen (Just acct) d $ setDepth 6 st
|
|
|
|
Vty.EvKey (Vty.KChar '7') [] -> continue $ initAccountsScreen (Just acct) d $ setDepth 7 st
|
|
|
|
Vty.EvKey (Vty.KChar '8') [] -> continue $ initAccountsScreen (Just acct) d $ setDepth 8 st
|
|
|
|
Vty.EvKey (Vty.KChar '9') [] -> continue $ initAccountsScreen (Just acct) d $ setDepth 9 st
|
2015-08-20 14:54:23 +03:00
|
|
|
Vty.EvKey (Vty.KLeft) [] -> continue $ popScreen st
|
2015-08-21 18:40:05 +03:00
|
|
|
Vty.EvKey (Vty.KRight) [] -> do
|
2015-08-28 16:36:07 +03:00
|
|
|
let st' = screenEnter d RS.screen{rsAcct=acct} st
|
2015-08-23 03:46:57 +03:00
|
|
|
vScrollToBeginning $ viewportScroll "register"
|
|
|
|
continue st'
|
2015-08-18 03:44:18 +03:00
|
|
|
|
2015-08-20 14:54:23 +03:00
|
|
|
-- Vty.EvKey (Vty.KPageDown) [] -> continue $ st{aScreen=scr{asState=moveSel h is}}
|
|
|
|
-- Vty.EvKey (Vty.KPageUp) [] -> continue $ st{aScreen=scr{asState=moveSel (-h) is}}
|
2015-08-18 03:44:18 +03:00
|
|
|
|
|
|
|
-- fall through to the list's event handler (handles up/down)
|
2015-08-20 14:54:23 +03:00
|
|
|
ev -> do
|
|
|
|
is' <- handleEvent ev is
|
|
|
|
continue $ st{aScreen=scr{asState=is'}}
|
|
|
|
-- continue =<< handleEventLensed st someLens ev
|
2015-08-18 03:44:18 +03:00
|
|
|
handleAccountsScreen _ _ = error "event handler called with wrong screen type, should not happen"
|
2015-08-28 08:45:49 +03:00
|
|
|
|
|
|
|
setDepth :: Int -> AppState -> AppState
|
|
|
|
setDepth depth st@AppState{aopts=uopts@UIOpts{cliopts_=copts@CliOpts{..}}} =
|
|
|
|
st{aopts=uopts{cliopts_=copts{reportopts_=reportopts_{depth_=md}}}}
|
|
|
|
where
|
|
|
|
md | depth==0 = Nothing
|
|
|
|
| otherwise = Just depth
|