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
|
2015-09-04 06:51:05 +03:00
|
|
|
(screen
|
|
|
|
,initAccountsScreen
|
|
|
|
)
|
2015-08-18 03:44:18 +03:00
|
|
|
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)
|
2015-08-28 22:33:33 +03:00
|
|
|
-- import Hledger.Cli.CliOptions (defaultBalanceLineFormat)
|
|
|
|
import Hledger.UI.UIOptions
|
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 18:07:54 +03:00
|
|
|
initAccountsScreen mselacct d st@AppState{
|
|
|
|
aopts=uopts@UIOpts{cliopts_=copts@CliOpts{reportopts_=ropts}},
|
|
|
|
ajournal=j,
|
|
|
|
aScreen=s@AccountsScreen{}
|
|
|
|
} =
|
2015-09-04 06:40:43 +03:00
|
|
|
st{aopts=uopts', aScreen=s{asState=l'}}
|
2015-08-18 03:44:18 +03:00
|
|
|
where
|
2015-08-29 03:55:50 +03:00
|
|
|
l = list (Name "accounts") (V.fromList displayitems) 1
|
2015-08-28 18:07:54 +03:00
|
|
|
|
|
|
|
-- hacky: when we're adjusting depth, mselacct is the account that was selected previously,
|
2015-08-28 08:45:49 +03:00
|
|
|
-- in which case try and keep the selection near where it was
|
2015-08-28 18:07:54 +03:00
|
|
|
l' = case mselacct of
|
|
|
|
Nothing -> l
|
2015-08-28 08:45:49 +03:00
|
|
|
Just a -> -- vScrollToBeginning $ viewportScroll "accounts"
|
2015-08-28 18:07:54 +03:00
|
|
|
maybe l (flip listMoveTo l) mi
|
2015-08-28 08:45:49 +03:00
|
|
|
where
|
|
|
|
mi = findIndex (\((acct,_,_),_) -> acct==a') items
|
|
|
|
a' = maybe a (flip clipAccountName a) $ depth_ ropts
|
|
|
|
|
2015-09-04 06:40:43 +03:00
|
|
|
uopts' = uopts{cliopts_=copts{reportopts_=ropts'}}
|
|
|
|
ropts' = ropts {
|
|
|
|
-- XXX balanceReport doesn't respect this yet
|
|
|
|
balancetype_=HistoricalBalance
|
|
|
|
}
|
|
|
|
|
2015-08-28 08:45:49 +03:00
|
|
|
q = queryFromOpts d ropts
|
2015-09-04 06:40:43 +03:00
|
|
|
|
2015-08-28 18:07:54 +03:00
|
|
|
-- maybe convert balances to market value
|
|
|
|
convert | value_ ropts' = balanceReportValue j valuedate
|
2015-08-28 08:45:49 +03:00
|
|
|
| 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
|
|
|
|
|
2015-08-28 18:07:54 +03:00
|
|
|
-- run the report
|
|
|
|
(items,_total) = convert $ balanceReport ropts' q j
|
2015-08-28 08:45:49 +03:00
|
|
|
|
2015-08-29 03:55:50 +03:00
|
|
|
-- pre-render the list items
|
|
|
|
displayitem ((fullacct, shortacct, indent), bal) =
|
|
|
|
(indent
|
|
|
|
,fullacct
|
2015-09-04 17:29:34 +03:00
|
|
|
,if flat_ ropts' then fullacct else shortacct
|
2015-08-29 03:55:50 +03:00
|
|
|
,map showAmountWithoutPrice amts -- like showMixedAmountOneLineWithoutPrice
|
|
|
|
)
|
|
|
|
where
|
|
|
|
Mixed amts = normaliseMixedAmountSquashPricesForDisplay $ stripPrices bal
|
|
|
|
stripPrices (Mixed as) = Mixed $ map stripprice as where stripprice a = a{aprice=NoPrice}
|
|
|
|
displayitems = map displayitem items
|
|
|
|
|
|
|
|
|
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-29 03:55:50 +03:00
|
|
|
drawAccountsScreen _st@AppState{aopts=uopts, ajournal=j, aScreen=AccountsScreen{asState=l}} =
|
2015-08-28 18:07:54 +03:00
|
|
|
[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
|
2015-08-29 03:55:50 +03:00
|
|
|
<+> borderDepthStr mdepth
|
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 ""
|
2015-09-04 17:40:09 +03:00
|
|
|
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-29 03:55:50 +03:00
|
|
|
mdepth = depth_ $ reportopts_ $ cliopts_ uopts
|
|
|
|
cur = str (case l^.listSelectedL of
|
2015-08-20 14:54:23 +03:00
|
|
|
Nothing -> "-"
|
|
|
|
Just i -> show (i + 1))
|
2015-08-29 03:55:50 +03:00
|
|
|
total = str $ show $ V.length $ l^.listElementsL
|
2015-08-18 03:44:18 +03:00
|
|
|
|
2015-08-28 08:45:49 +03:00
|
|
|
bottomlabel = borderKeysStr [
|
2015-09-04 07:03:03 +03:00
|
|
|
-- ("up/down/pgup/pgdown/home/end", "move")
|
|
|
|
("-+1234567890", "adjust depth limit")
|
2015-09-04 17:36:23 +03:00
|
|
|
,("right/enter", "show transactions")
|
2015-09-04 07:03:03 +03:00
|
|
|
,("q", "quit")
|
2015-08-28 08:45:49 +03:00
|
|
|
]
|
|
|
|
|
2015-08-29 03:55:50 +03:00
|
|
|
ui = Widget Greedy Greedy $ do
|
|
|
|
c <- getContext
|
|
|
|
let
|
|
|
|
availwidth =
|
|
|
|
-- ltrace "availwidth" $
|
|
|
|
c^.availWidthL
|
|
|
|
- 2 -- XXX due to margin ? shouldn't be necessary (cf UIUtils)
|
|
|
|
displayitems = listElements l
|
|
|
|
maxacctwidthseen =
|
|
|
|
-- ltrace "maxacctwidthseen" $
|
|
|
|
V.maximum $
|
|
|
|
V.map (\(indent,_,displayacct,_) -> indent*2 + length displayacct) $
|
|
|
|
-- V.filter (\(indent,_,_,_) -> (indent-1) <= fromMaybe 99999 mdepth) $
|
|
|
|
displayitems
|
|
|
|
maxbalwidthseen =
|
|
|
|
-- ltrace "maxbalwidthseen" $
|
|
|
|
V.maximum $ V.map (\(_,_,_,amts) -> sum (map length amts) + 2 * (length amts-1)) displayitems
|
|
|
|
maxbalwidth =
|
|
|
|
-- ltrace "maxbalwidth" $
|
|
|
|
max 0 (availwidth - 2 - 4) -- leave 2 whitespace plus least 4 for accts
|
|
|
|
balwidth =
|
|
|
|
-- ltrace "balwidth" $
|
|
|
|
min maxbalwidth maxbalwidthseen
|
|
|
|
maxacctwidth =
|
|
|
|
-- ltrace "maxacctwidth" $
|
|
|
|
availwidth - 2 - balwidth
|
|
|
|
acctwidth =
|
|
|
|
-- ltrace "acctwidth" $
|
|
|
|
min maxacctwidth maxacctwidthseen
|
|
|
|
|
|
|
|
-- XXX how to minimise the balance column's jumping around
|
|
|
|
-- as you change the depth limit ?
|
|
|
|
|
|
|
|
colwidths = (acctwidth, balwidth)
|
|
|
|
|
|
|
|
render $ defaultLayout toplabel bottomlabel $ renderList l (drawAccountsItem colwidths)
|
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"
|
|
|
|
|
2015-08-29 03:55:50 +03:00
|
|
|
drawAccountsItem :: (Int,Int) -> Bool -> (Int, String, String, [String]) -> Widget
|
|
|
|
drawAccountsItem (acctwidth, balwidth) selected (indent, _fullacct, displayacct, balamts) =
|
2015-08-23 03:46:57 +03:00
|
|
|
Widget Greedy Fixed $ do
|
|
|
|
-- c <- getContext
|
2015-08-29 03:55:50 +03:00
|
|
|
-- let showitem = intercalate "\n" . balanceReportItemAsText defreportopts fmt
|
|
|
|
render $
|
|
|
|
addamts balamts $
|
|
|
|
str (padright acctwidth $ elideRight acctwidth $ replicate (2*indent) ' ' ++ displayacct) <+>
|
|
|
|
str " " <+>
|
|
|
|
str (balspace balamts)
|
|
|
|
where
|
|
|
|
balspace as = replicate n ' '
|
|
|
|
where n = max 0 (balwidth - (sum (map length as) + 2 * (length as - 1)))
|
|
|
|
addamts :: [String] -> Widget -> Widget
|
|
|
|
addamts [] w = w
|
|
|
|
addamts [a] w = (<+> renderamt a) w
|
|
|
|
-- foldl' :: (b -> a -> b) -> b -> t a -> b
|
|
|
|
-- foldl' (Widget -> String -> Widget) -> Widget -> [String] -> Widget
|
|
|
|
addamts (a:as) w = foldl' addamt (addamts [a] w) as
|
|
|
|
addamt :: Widget -> String -> Widget
|
|
|
|
addamt w a = ((<+> renderamt a) . (<+> str ", ")) w
|
|
|
|
renderamt :: String -> Widget
|
|
|
|
renderamt a | '-' `elem` a = withAttr (sel $ "list" <> "balance" <> "negative") $ str a
|
|
|
|
| otherwise = withAttr (sel $ "list" <> "balance" <> "positive") $ str a
|
|
|
|
sel | selected = (<> "selected")
|
|
|
|
| otherwise = id
|
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-29 03:55:50 +03:00
|
|
|
handleAccountsScreen st@AppState{aScreen=scr@AccountsScreen{asState=l}} 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
|
2015-08-29 03:55:50 +03:00
|
|
|
acct = case listSelectedElement l of
|
|
|
|
Just (_, (_, fullacct, _, _)) -> fullacct
|
2015-08-28 08:45:49 +03:00
|
|
|
Nothing -> ""
|
2015-08-28 21:18:48 +03:00
|
|
|
reload = continue . initAccountsScreen (Just acct) d
|
|
|
|
|
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 21:18:48 +03:00
|
|
|
Vty.EvKey (Vty.KChar '-') [] -> reload $ decDepth st
|
|
|
|
Vty.EvKey (Vty.KChar '+') [] -> reload $ incDepth st
|
|
|
|
Vty.EvKey (Vty.KChar '1') [] -> reload $ setDepth 1 st
|
|
|
|
Vty.EvKey (Vty.KChar '2') [] -> reload $ setDepth 2 st
|
|
|
|
Vty.EvKey (Vty.KChar '3') [] -> reload $ setDepth 3 st
|
|
|
|
Vty.EvKey (Vty.KChar '4') [] -> reload $ setDepth 4 st
|
|
|
|
Vty.EvKey (Vty.KChar '5') [] -> reload $ setDepth 5 st
|
|
|
|
Vty.EvKey (Vty.KChar '6') [] -> reload $ setDepth 6 st
|
|
|
|
Vty.EvKey (Vty.KChar '7') [] -> reload $ setDepth 7 st
|
|
|
|
Vty.EvKey (Vty.KChar '8') [] -> reload $ setDepth 8 st
|
|
|
|
Vty.EvKey (Vty.KChar '9') [] -> reload $ setDepth 9 st
|
|
|
|
Vty.EvKey (Vty.KChar '0') [] -> reload $ setDepth 0 st
|
2015-08-20 14:54:23 +03:00
|
|
|
Vty.EvKey (Vty.KLeft) [] -> continue $ popScreen st
|
2015-09-04 07:05:15 +03:00
|
|
|
Vty.EvKey (k) [] | k `elem` [Vty.KRight, Vty.KEnter] -> 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-29 03:55:50 +03:00
|
|
|
-- Vty.EvKey (Vty.KPageDown) [] -> continue $ st{aScreen=scr{asState=moveSel h l}}
|
|
|
|
-- Vty.EvKey (Vty.KPageUp) [] -> continue $ st{aScreen=scr{asState=moveSel (-h) l}}
|
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
|
2015-08-29 03:55:50 +03:00
|
|
|
l' <- handleEvent ev l
|
|
|
|
continue $ st{aScreen=scr{asState=l'}}
|
2015-08-20 14:54:23 +03:00
|
|
|
-- 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
|
|
|
|
2015-08-28 20:31:40 +03:00
|
|
|
-- | Get the maximum account depth in the current journal.
|
|
|
|
maxDepth :: AppState -> Int
|
|
|
|
maxDepth AppState{ajournal=j} = maximum $ map accountNameLevel $ journalAccountNames j
|
|
|
|
|
|
|
|
-- | Decrement the current depth limit towards 0. If there was no depth limit,
|
|
|
|
-- set it to one less than the maximum account depth.
|
|
|
|
decDepth :: AppState -> AppState
|
|
|
|
decDepth st@AppState{aopts=uopts@UIOpts{cliopts_=copts@CliOpts{reportopts_=ropts@ReportOpts{..}}}}
|
|
|
|
= st{aopts=uopts{cliopts_=copts{reportopts_=ropts{depth_=dec depth_}}}}
|
2015-08-28 08:45:49 +03:00
|
|
|
where
|
2015-08-28 20:31:40 +03:00
|
|
|
dec (Just d) = Just $ max 0 (d-1)
|
|
|
|
dec Nothing = Just $ maxDepth st - 1
|
2015-08-28 20:01:54 +03:00
|
|
|
|
|
|
|
-- | Increment the current depth limit. If this makes it equal to the
|
|
|
|
-- the maximum account depth, remove the depth limit.
|
|
|
|
incDepth :: AppState -> AppState
|
2015-08-28 20:31:40 +03:00
|
|
|
incDepth st@AppState{aopts=uopts@UIOpts{cliopts_=copts@CliOpts{reportopts_=ropts@ReportOpts{..}}}}
|
|
|
|
= st{aopts=uopts{cliopts_=copts{reportopts_=ropts{depth_=inc depth_}}}}
|
2015-08-28 20:01:54 +03:00
|
|
|
where
|
2015-08-28 20:31:40 +03:00
|
|
|
inc (Just d) | d < (maxDepth st - 1) = Just $ d+1
|
2015-08-28 20:01:54 +03:00
|
|
|
inc _ = Nothing
|
|
|
|
|
2015-08-28 20:31:40 +03:00
|
|
|
-- | Set the current depth limit to the specified depth, which should
|
|
|
|
-- be a positive number. If it is zero, or equal to or greater than the
|
|
|
|
-- current maximum account depth, the depth limit will be removed.
|
|
|
|
-- (Slight inconsistency here: zero is currently a valid display depth
|
|
|
|
-- which can be reached using the - key. But we need a key to remove
|
|
|
|
-- the depth limit, and 0 is it.)
|
|
|
|
setDepth :: Int -> AppState -> AppState
|
|
|
|
setDepth depth st@AppState{aopts=uopts@UIOpts{cliopts_=copts@CliOpts{reportopts_=ropts}}}
|
|
|
|
= st{aopts=uopts{cliopts_=copts{reportopts_=ropts{depth_=mdepth'}}}}
|
2015-08-28 20:01:54 +03:00
|
|
|
where
|
2015-08-28 20:31:40 +03:00
|
|
|
mdepth' | depth < 0 = depth_ ropts
|
|
|
|
| depth == 0 = Nothing
|
|
|
|
| depth >= maxDepth st = Nothing
|
|
|
|
| otherwise = Just depth
|
|
|
|
|