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-10-28 21:35:40 +03:00
|
|
|
,asSetSelectedAccount
|
2015-09-04 06:51:05 +03:00
|
|
|
)
|
2015-08-18 03:44:18 +03:00
|
|
|
where
|
|
|
|
|
2016-04-05 03:44:29 +03:00
|
|
|
import Lens.Micro ((^.))
|
2015-08-18 03:44:18 +03:00
|
|
|
-- import Control.Monad
|
2015-09-04 19:10:00 +03:00
|
|
|
import Control.Monad.IO.Class (liftIO)
|
2015-08-18 03:44:18 +03:00
|
|
|
-- 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
|
lib: textification begins! account names
The first of several conversions from String to (strict) Text, hopefully
reducing space and time usage.
This one shows a small improvement, with GHC 7.10.3 and text-1.2.2.1:
hledger -f data/100x100x10.journal stats
string: <<ghc: 39471064 bytes, 77 GCs, 198421/275048 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.015 MUT (0.020 elapsed), 0.010 GC (0.014 elapsed) :ghc>>
text: <<ghc: 39268024 bytes, 77 GCs, 197018/270840 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.002 elapsed), 0.016 MUT (0.022 elapsed), 0.009 GC (0.011 elapsed) :ghc>>
hledger -f data/1000x100x10.journal stats
string: <<ghc: 318555920 bytes, 617 GCs, 2178997/7134472 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.001 elapsed), 0.129 MUT (0.136 elapsed), 0.067 GC (0.077 elapsed) :ghc>>
text: <<ghc: 314248496 bytes, 612 GCs, 2074045/6617960 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.003 elapsed), 0.137 MUT (0.145 elapsed), 0.067 GC (0.079 elapsed) :ghc>>
hledger -f data/10000x100x10.journal stats
string: <<ghc: 3114763608 bytes, 6026 GCs, 18858950/75552024 avg/max bytes residency (11 samples), 201M in use, 0.000 INIT (0.000 elapsed), 1.331 MUT (1.372 elapsed), 0.699 GC (0.812 elapsed) :ghc>>
text: <<ghc: 3071468920 bytes, 5968 GCs, 14120344/62951360 avg/max bytes residency (9 samples), 124M in use, 0.000 INIT (0.003 elapsed), 1.272 MUT (1.349 elapsed), 0.513 GC (0.578 elapsed) :ghc>>
hledger -f data/100000x100x10.journal stats
string: <<ghc: 31186579432 bytes, 60278 GCs, 135332581/740228992 avg/max bytes residency (13 samples), 1697M in use, 0.000 INIT (0.008 elapsed), 14.677 MUT (15.508 elapsed), 7.081 GC (8.074 elapsed) :ghc>>
text: <<ghc: 30753427672 bytes, 59763 GCs, 117595958/666457240 avg/max bytes residency (14 samples), 1588M in use, 0.000 INIT (0.008 elapsed), 13.713 MUT (13.966 elapsed), 6.220 GC (7.108 elapsed) :ghc>>
2016-05-24 04:16:21 +03:00
|
|
|
-- import Data.Text (Text)
|
|
|
|
import qualified Data.Text as T
|
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
|
2016-06-04 21:51:28 +03:00
|
|
|
import Brick.Widgets.Edit
|
2015-10-30 20:42:44 +03:00
|
|
|
import Brick.Widgets.Border (borderAttr)
|
2015-08-23 03:46:57 +03:00
|
|
|
-- 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-10-28 21:35:40 +03:00
|
|
|
import qualified Hledger.UI.RegisterScreen as RS (screen, rsSetCurrentAccount)
|
2015-10-26 17:41:45 +03:00
|
|
|
import qualified Hledger.UI.ErrorScreen as ES (screen)
|
2015-08-18 03:44:18 +03:00
|
|
|
|
|
|
|
screen = AccountsScreen{
|
2015-10-28 21:29:49 +03:00
|
|
|
asState = (list "accounts" V.empty 1, "")
|
|
|
|
,sInitFn = initAccountsScreen
|
|
|
|
,sDrawFn = drawAccountsScreen
|
2015-08-18 03:44:18 +03:00
|
|
|
,sHandleFn = handleAccountsScreen
|
|
|
|
}
|
|
|
|
|
2015-10-28 21:35:40 +03:00
|
|
|
asSetSelectedAccount a scr@AccountsScreen{asState=(l,_)} = scr{asState=(l,a)}
|
|
|
|
asSetSelectedAccount _ scr = scr
|
|
|
|
|
2015-10-28 21:13:33 +03:00
|
|
|
initAccountsScreen :: Day -> AppState -> AppState
|
|
|
|
initAccountsScreen d st@AppState{
|
2015-08-28 18:07:54 +03:00
|
|
|
aopts=uopts@UIOpts{cliopts_=copts@CliOpts{reportopts_=ropts}},
|
|
|
|
ajournal=j,
|
2016-06-04 02:31:53 +03:00
|
|
|
aScreen=s@AccountsScreen{asState=(oldl,selacct)}
|
2015-08-28 18:07:54 +03:00
|
|
|
} =
|
2016-06-04 02:31:53 +03:00
|
|
|
st{aopts=uopts', aScreen=s{asState=(newl',selacct)}}
|
2015-08-18 03:44:18 +03:00
|
|
|
where
|
2016-06-04 02:31:53 +03:00
|
|
|
newl = list (Name "accounts") (V.fromList displayitems) 1
|
2015-08-28 18:07:54 +03:00
|
|
|
|
2016-06-04 02:31:53 +03:00
|
|
|
-- keep the selection near the last selected account
|
|
|
|
-- (may need to move to the next leaf account when entering flat mode)
|
|
|
|
newl' = listMoveTo selidx newl
|
2015-10-28 21:13:33 +03:00
|
|
|
where
|
2016-06-04 02:31:53 +03:00
|
|
|
selidx = case listSelectedElement oldl of
|
|
|
|
Nothing -> 0
|
|
|
|
Just (_,(_,a,_,_)) -> fromMaybe (fromMaybe 0 mprefixmatch) mexactmatch
|
|
|
|
where
|
|
|
|
mexactmatch = findIndex ((a ==) . second4) displayitems
|
|
|
|
mprefixmatch = findIndex ((a `isAccountNamePrefixOf`) . second4) displayitems
|
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-10-28 21:13:33 +03:00
|
|
|
initAccountsScreen _ _ = error "init function called with wrong screen type, should not happen"
|
2015-08-18 03:44:18 +03:00
|
|
|
|
|
|
|
drawAccountsScreen :: AppState -> [Widget]
|
2016-06-01 22:09:16 +03:00
|
|
|
drawAccountsScreen AppState{aopts=UIOpts{cliopts_=CliOpts{reportopts_=ropts}}
|
|
|
|
,ajournal=j
|
2016-06-04 21:51:28 +03:00
|
|
|
,aScreen=AccountsScreen{asState=(l,_)}
|
|
|
|
,aMinibuffer=mbuf} =
|
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
|
2016-06-03 19:01:54 +03:00
|
|
|
<+> nonzero
|
2015-08-26 02:01:12 +03:00
|
|
|
<+> str " accounts"
|
2015-08-28 08:45:49 +03:00
|
|
|
<+> borderQueryStr querystr
|
2016-06-01 22:09:16 +03:00
|
|
|
<+> togglefilters
|
2015-08-29 03:55:50 +03:00
|
|
|
<+> borderDepthStr mdepth
|
2015-08-25 16:56:04 +03:00
|
|
|
<+> str " ("
|
|
|
|
<+> cur
|
2015-09-04 19:10:00 +03:00
|
|
|
<+> str "/"
|
2015-08-25 16:56:04 +03:00
|
|
|
<+> 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)")
|
2016-06-01 22:09:16 +03:00
|
|
|
querystr = query_ ropts
|
|
|
|
mdepth = depth_ ropts
|
|
|
|
togglefilters =
|
|
|
|
case concat [
|
|
|
|
if cleared_ ropts then ["cleared"] else []
|
2016-06-04 04:27:35 +03:00
|
|
|
,if uncleared_ ropts then ["uncleared"] else []
|
|
|
|
,if pending_ ropts then ["pending"] else []
|
2016-06-01 22:09:16 +03:00
|
|
|
,if real_ ropts then ["real"] else []
|
|
|
|
] of
|
|
|
|
[] -> str ""
|
|
|
|
fs -> str " with " <+> withAttr (borderAttr <> "query") (str $ intercalate ", " fs) <+> str " txns"
|
2016-06-03 19:01:54 +03:00
|
|
|
nonzero | empty_ ropts = str ""
|
|
|
|
| otherwise = withAttr (borderAttr <> "query") (str " nonzero")
|
2015-08-29 03:55:50 +03:00
|
|
|
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")
|
2015-10-30 20:42:44 +03:00
|
|
|
("-=1234567890", "depth")
|
|
|
|
,("F", "flat?")
|
2016-06-03 19:01:54 +03:00
|
|
|
,("E", "nonzero?")
|
2015-10-30 20:42:44 +03:00
|
|
|
,("C", "cleared?")
|
2016-06-04 04:27:35 +03:00
|
|
|
,("U", "uncleared?")
|
2016-06-01 22:09:16 +03:00
|
|
|
,("R", "real?")
|
2016-06-07 19:21:51 +03:00
|
|
|
,("/", "filter")
|
|
|
|
,("DEL", "unfilter")
|
2015-10-30 20:42:44 +03:00
|
|
|
,("right/enter", "register")
|
2015-09-04 18:14:36 +03:00
|
|
|
,("g", "reload")
|
2015-09-04 07:03:03 +03:00
|
|
|
,("q", "quit")
|
2015-08-28 08:45:49 +03:00
|
|
|
]
|
|
|
|
|
2016-06-04 21:51:28 +03:00
|
|
|
bottomarea = case mbuf of
|
|
|
|
Nothing -> bottomlabel
|
|
|
|
Just ed -> minibuffer ed
|
|
|
|
|
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 $
|
lib: textification begins! account names
The first of several conversions from String to (strict) Text, hopefully
reducing space and time usage.
This one shows a small improvement, with GHC 7.10.3 and text-1.2.2.1:
hledger -f data/100x100x10.journal stats
string: <<ghc: 39471064 bytes, 77 GCs, 198421/275048 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.015 MUT (0.020 elapsed), 0.010 GC (0.014 elapsed) :ghc>>
text: <<ghc: 39268024 bytes, 77 GCs, 197018/270840 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.002 elapsed), 0.016 MUT (0.022 elapsed), 0.009 GC (0.011 elapsed) :ghc>>
hledger -f data/1000x100x10.journal stats
string: <<ghc: 318555920 bytes, 617 GCs, 2178997/7134472 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.001 elapsed), 0.129 MUT (0.136 elapsed), 0.067 GC (0.077 elapsed) :ghc>>
text: <<ghc: 314248496 bytes, 612 GCs, 2074045/6617960 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.003 elapsed), 0.137 MUT (0.145 elapsed), 0.067 GC (0.079 elapsed) :ghc>>
hledger -f data/10000x100x10.journal stats
string: <<ghc: 3114763608 bytes, 6026 GCs, 18858950/75552024 avg/max bytes residency (11 samples), 201M in use, 0.000 INIT (0.000 elapsed), 1.331 MUT (1.372 elapsed), 0.699 GC (0.812 elapsed) :ghc>>
text: <<ghc: 3071468920 bytes, 5968 GCs, 14120344/62951360 avg/max bytes residency (9 samples), 124M in use, 0.000 INIT (0.003 elapsed), 1.272 MUT (1.349 elapsed), 0.513 GC (0.578 elapsed) :ghc>>
hledger -f data/100000x100x10.journal stats
string: <<ghc: 31186579432 bytes, 60278 GCs, 135332581/740228992 avg/max bytes residency (13 samples), 1697M in use, 0.000 INIT (0.008 elapsed), 14.677 MUT (15.508 elapsed), 7.081 GC (8.074 elapsed) :ghc>>
text: <<ghc: 30753427672 bytes, 59763 GCs, 117595958/666457240 avg/max bytes residency (14 samples), 1588M in use, 0.000 INIT (0.008 elapsed), 13.713 MUT (13.966 elapsed), 6.220 GC (7.108 elapsed) :ghc>>
2016-05-24 04:16:21 +03:00
|
|
|
V.map (\(indent,_,displayacct,_) -> indent*2 + textWidth displayacct) $
|
2015-08-29 03:55:50 +03:00
|
|
|
-- V.filter (\(indent,_,_,_) -> (indent-1) <= fromMaybe 99999 mdepth) $
|
|
|
|
displayitems
|
|
|
|
maxbalwidthseen =
|
|
|
|
-- ltrace "maxbalwidthseen" $
|
2015-09-30 10:17:24 +03:00
|
|
|
V.maximum $ V.map (\(_,_,_,amts) -> sum (map strWidth amts) + 2 * (length amts-1)) displayitems
|
2015-08-29 03:55:50 +03:00
|
|
|
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)
|
|
|
|
|
2016-06-04 21:51:28 +03:00
|
|
|
render $ defaultLayout toplabel bottomarea $ 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"
|
|
|
|
|
lib: textification begins! account names
The first of several conversions from String to (strict) Text, hopefully
reducing space and time usage.
This one shows a small improvement, with GHC 7.10.3 and text-1.2.2.1:
hledger -f data/100x100x10.journal stats
string: <<ghc: 39471064 bytes, 77 GCs, 198421/275048 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.015 MUT (0.020 elapsed), 0.010 GC (0.014 elapsed) :ghc>>
text: <<ghc: 39268024 bytes, 77 GCs, 197018/270840 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.002 elapsed), 0.016 MUT (0.022 elapsed), 0.009 GC (0.011 elapsed) :ghc>>
hledger -f data/1000x100x10.journal stats
string: <<ghc: 318555920 bytes, 617 GCs, 2178997/7134472 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.001 elapsed), 0.129 MUT (0.136 elapsed), 0.067 GC (0.077 elapsed) :ghc>>
text: <<ghc: 314248496 bytes, 612 GCs, 2074045/6617960 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.003 elapsed), 0.137 MUT (0.145 elapsed), 0.067 GC (0.079 elapsed) :ghc>>
hledger -f data/10000x100x10.journal stats
string: <<ghc: 3114763608 bytes, 6026 GCs, 18858950/75552024 avg/max bytes residency (11 samples), 201M in use, 0.000 INIT (0.000 elapsed), 1.331 MUT (1.372 elapsed), 0.699 GC (0.812 elapsed) :ghc>>
text: <<ghc: 3071468920 bytes, 5968 GCs, 14120344/62951360 avg/max bytes residency (9 samples), 124M in use, 0.000 INIT (0.003 elapsed), 1.272 MUT (1.349 elapsed), 0.513 GC (0.578 elapsed) :ghc>>
hledger -f data/100000x100x10.journal stats
string: <<ghc: 31186579432 bytes, 60278 GCs, 135332581/740228992 avg/max bytes residency (13 samples), 1697M in use, 0.000 INIT (0.008 elapsed), 14.677 MUT (15.508 elapsed), 7.081 GC (8.074 elapsed) :ghc>>
text: <<ghc: 30753427672 bytes, 59763 GCs, 117595958/666457240 avg/max bytes residency (14 samples), 1588M in use, 0.000 INIT (0.008 elapsed), 13.713 MUT (13.966 elapsed), 6.220 GC (7.108 elapsed) :ghc>>
2016-05-24 04:16:21 +03:00
|
|
|
drawAccountsItem :: (Int,Int) -> Bool -> (Int, AccountName, AccountName, [String]) -> Widget
|
2015-08-29 03:55:50 +03:00
|
|
|
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 $
|
lib: textification begins! account names
The first of several conversions from String to (strict) Text, hopefully
reducing space and time usage.
This one shows a small improvement, with GHC 7.10.3 and text-1.2.2.1:
hledger -f data/100x100x10.journal stats
string: <<ghc: 39471064 bytes, 77 GCs, 198421/275048 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.001 elapsed), 0.015 MUT (0.020 elapsed), 0.010 GC (0.014 elapsed) :ghc>>
text: <<ghc: 39268024 bytes, 77 GCs, 197018/270840 avg/max bytes residency (3 samples), 2M in use, 0.000 INIT (0.002 elapsed), 0.016 MUT (0.022 elapsed), 0.009 GC (0.011 elapsed) :ghc>>
hledger -f data/1000x100x10.journal stats
string: <<ghc: 318555920 bytes, 617 GCs, 2178997/7134472 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.001 elapsed), 0.129 MUT (0.136 elapsed), 0.067 GC (0.077 elapsed) :ghc>>
text: <<ghc: 314248496 bytes, 612 GCs, 2074045/6617960 avg/max bytes residency (7 samples), 16M in use, 0.000 INIT (0.003 elapsed), 0.137 MUT (0.145 elapsed), 0.067 GC (0.079 elapsed) :ghc>>
hledger -f data/10000x100x10.journal stats
string: <<ghc: 3114763608 bytes, 6026 GCs, 18858950/75552024 avg/max bytes residency (11 samples), 201M in use, 0.000 INIT (0.000 elapsed), 1.331 MUT (1.372 elapsed), 0.699 GC (0.812 elapsed) :ghc>>
text: <<ghc: 3071468920 bytes, 5968 GCs, 14120344/62951360 avg/max bytes residency (9 samples), 124M in use, 0.000 INIT (0.003 elapsed), 1.272 MUT (1.349 elapsed), 0.513 GC (0.578 elapsed) :ghc>>
hledger -f data/100000x100x10.journal stats
string: <<ghc: 31186579432 bytes, 60278 GCs, 135332581/740228992 avg/max bytes residency (13 samples), 1697M in use, 0.000 INIT (0.008 elapsed), 14.677 MUT (15.508 elapsed), 7.081 GC (8.074 elapsed) :ghc>>
text: <<ghc: 30753427672 bytes, 59763 GCs, 117595958/666457240 avg/max bytes residency (14 samples), 1588M in use, 0.000 INIT (0.008 elapsed), 13.713 MUT (13.966 elapsed), 6.220 GC (7.108 elapsed) :ghc>>
2016-05-24 04:16:21 +03:00
|
|
|
str (T.unpack $ fitText (Just acctwidth) (Just acctwidth) True True $ T.replicate (2*indent) " " <> displayacct) <+>
|
2015-08-29 03:55:50 +03:00
|
|
|
str " " <+>
|
|
|
|
str (balspace balamts)
|
|
|
|
where
|
|
|
|
balspace as = replicate n ' '
|
2015-09-30 10:17:24 +03:00
|
|
|
where n = max 0 (balwidth - (sum (map strWidth as) + 2 * (length as - 1)))
|
2015-08-29 03:55:50 +03:00
|
|
|
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-09-04 18:14:36 +03:00
|
|
|
handleAccountsScreen st@AppState{
|
2015-10-30 20:42:44 +03:00
|
|
|
aScreen=scr@AccountsScreen{asState=(l,selacct)}
|
2016-05-03 06:12:11 +03:00
|
|
|
,aopts=UIOpts{cliopts_=copts}
|
2015-09-04 18:14:36 +03:00
|
|
|
,ajournal=j
|
2016-06-04 21:51:28 +03:00
|
|
|
,aMinibuffer=mbuf
|
|
|
|
} ev = 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-10-28 20:17:15 +03:00
|
|
|
|
2015-10-28 21:13:33 +03:00
|
|
|
-- before we go anywhere, remember the currently selected account.
|
|
|
|
-- (This is preserved across screen changes, unlike List's selection state)
|
|
|
|
let
|
|
|
|
selacct' = case listSelectedElement l of
|
|
|
|
Just (_, (_, fullacct, _, _)) -> fullacct
|
|
|
|
Nothing -> selacct
|
|
|
|
st' = st{aScreen=scr{asState=(l,selacct')}}
|
2015-08-28 21:18:48 +03:00
|
|
|
|
2016-06-04 21:51:28 +03:00
|
|
|
case mbuf of
|
|
|
|
Nothing ->
|
|
|
|
|
|
|
|
case ev of
|
|
|
|
Vty.EvKey (Vty.KChar 'q') [] -> halt st'
|
|
|
|
-- Vty.EvKey (Vty.KChar 'l') [Vty.MCtrl] -> do
|
|
|
|
|
|
|
|
Vty.EvKey (Vty.KChar 'g') [] -> do
|
|
|
|
(ej, _) <- liftIO $ journalReloadIfChanged copts d j
|
|
|
|
case ej of
|
|
|
|
Right j' -> continue $ reload j' d st'
|
|
|
|
Left err -> continue $ screenEnter d ES.screen{esState=err} st'
|
|
|
|
|
|
|
|
Vty.EvKey (Vty.KChar '-') [] -> continue $ reload j d $ decDepth st'
|
|
|
|
Vty.EvKey (Vty.KChar '+') [] -> continue $ reload j d $ incDepth st'
|
|
|
|
Vty.EvKey (Vty.KChar '=') [] -> continue $ reload j d $ incDepth st'
|
|
|
|
Vty.EvKey (Vty.KChar '1') [] -> continue $ reload j d $ setDepth 1 st'
|
|
|
|
Vty.EvKey (Vty.KChar '2') [] -> continue $ reload j d $ setDepth 2 st'
|
|
|
|
Vty.EvKey (Vty.KChar '3') [] -> continue $ reload j d $ setDepth 3 st'
|
|
|
|
Vty.EvKey (Vty.KChar '4') [] -> continue $ reload j d $ setDepth 4 st'
|
|
|
|
Vty.EvKey (Vty.KChar '5') [] -> continue $ reload j d $ setDepth 5 st'
|
|
|
|
Vty.EvKey (Vty.KChar '6') [] -> continue $ reload j d $ setDepth 6 st'
|
|
|
|
Vty.EvKey (Vty.KChar '7') [] -> continue $ reload j d $ setDepth 7 st'
|
|
|
|
Vty.EvKey (Vty.KChar '8') [] -> continue $ reload j d $ setDepth 8 st'
|
|
|
|
Vty.EvKey (Vty.KChar '9') [] -> continue $ reload j d $ setDepth 9 st'
|
|
|
|
Vty.EvKey (Vty.KChar '0') [] -> continue $ reload j d $ setDepth 0 st'
|
|
|
|
Vty.EvKey (Vty.KChar 'F') [] -> continue $ reload j d $ stToggleFlat st'
|
|
|
|
Vty.EvKey (Vty.KChar 'E') [] -> scrollTop >> (continue $ reload j d $ stToggleEmpty st')
|
|
|
|
Vty.EvKey (Vty.KChar 'C') [] -> scrollTop >> (continue $ reload j d $ stToggleCleared st')
|
|
|
|
Vty.EvKey (Vty.KChar 'U') [] -> scrollTop >> (continue $ reload j d $ stToggleUncleared st')
|
|
|
|
Vty.EvKey (Vty.KChar 'R') [] -> scrollTop >> (continue $ reload j d $ stToggleReal st')
|
2016-06-07 19:21:51 +03:00
|
|
|
Vty.EvKey k [] | k `elem` [Vty.KChar '/'] -> continue $ reload j d $ stShowMinibuffer st'
|
|
|
|
Vty.EvKey k [] | k `elem` [Vty.KBS, Vty.KDel] -> (continue $ reload j d $ stResetFilter st')
|
2016-06-04 21:51:28 +03:00
|
|
|
Vty.EvKey (Vty.KLeft) [] -> continue $ popScreen st'
|
|
|
|
Vty.EvKey (k) [] | k `elem` [Vty.KRight, Vty.KEnter] -> do
|
|
|
|
let
|
|
|
|
scr = RS.rsSetCurrentAccount selacct' RS.screen
|
|
|
|
st'' = screenEnter d scr st'
|
|
|
|
scrollTopRegister
|
|
|
|
continue st''
|
|
|
|
|
|
|
|
-- fall through to the list's event handler (handles up/down)
|
|
|
|
ev -> do
|
|
|
|
l' <- handleEvent ev l
|
|
|
|
continue $ st'{aScreen=scr{asState=(l',selacct')}}
|
|
|
|
-- continue =<< handleEventLensed st' someLens ev
|
|
|
|
|
|
|
|
Just ed ->
|
|
|
|
case ev of
|
|
|
|
Vty.EvKey Vty.KEsc [] -> continue $ stHideMinibuffer st'
|
|
|
|
Vty.EvKey Vty.KEnter [] -> continue $ reload j d $ stFilter s $ stHideMinibuffer st'
|
|
|
|
where s = chomp $ unlines $ getEditContents ed
|
|
|
|
ev -> do ed' <- handleEvent ev ed
|
|
|
|
continue $ st'{aMinibuffer=Just ed'}
|
|
|
|
|
2016-06-04 03:53:49 +03:00
|
|
|
where
|
|
|
|
-- Encourage a more stable scroll position when toggling list items.
|
|
|
|
-- We scroll to the top, and the viewport will automatically
|
|
|
|
-- scroll down just far enough to reveal the selection, which
|
|
|
|
-- usually leaves it at bottom of screen).
|
|
|
|
-- XXX better: scroll so selection is in middle of screen ?
|
|
|
|
scrollTop = vScrollToBeginning $ viewportScroll "accounts"
|
|
|
|
scrollTopRegister = vScrollToBeginning $ viewportScroll "register"
|
|
|
|
|
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
|
|
|
|
|