| 1 |
-- | Constructors and updaters for all hledger-ui screens. |
| 2 |
-- |
| 3 |
-- Constructors (*New) create and initialise a new screen with valid state, |
| 4 |
-- based on the provided options, reporting date, journal, and screen-specific parameters. |
| 5 |
-- |
| 6 |
-- Updaters (*Update) recalculate an existing screen's state, |
| 7 |
-- based on new options, reporting date, journal, and the old screen state. |
| 8 |
-- |
| 9 |
-- These are gathered in this low-level module so that any screen's handler |
| 10 |
-- can create or regenerate all other screens. |
| 11 |
-- Drawing and event-handling code is elsewhere, in per-screen modules. |
| 12 |
|
| 13 |
{-# LANGUAGE LambdaCase #-} |
| 14 |
{-# LANGUAGE OverloadedStrings #-} |
| 15 |
{-# LANGUAGE NamedFieldPuns #-} |
| 16 |
|
| 17 |
module Hledger.UI.UIScreens |
| 18 |
(screenUpdate |
| 19 |
,esNew |
| 20 |
,esUpdate |
| 21 |
,msNew |
| 22 |
,msUpdate |
| 23 |
,asNew |
| 24 |
,asUpdate |
| 25 |
,asItemIndex |
| 26 |
,csNew |
| 27 |
,csUpdate |
| 28 |
,csItemIndex |
| 29 |
,bsNew |
| 30 |
,bsUpdate |
| 31 |
,bsItemIndex |
| 32 |
,isNew |
| 33 |
,isUpdate |
| 34 |
,isItemIndex |
| 35 |
,rsNew |
| 36 |
,rsUpdate |
| 37 |
,tsNew |
| 38 |
,tsUpdate |
| 39 |
) |
| 40 |
where |
| 41 |
|
| 42 |
import Brick.Widgets.List (listMoveTo, listSelectedElement, list, listSelectedL, GenericList) |
| 43 |
import Data.List |
| 44 |
import Data.Maybe |
| 45 |
import Data.Text qualified as T |
| 46 |
import Data.Time.Calendar (Day, diffDays) |
| 47 |
import Lens.Micro (over) |
| 48 |
import Safe |
| 49 |
import Data.Vector qualified as V |
| 50 |
|
| 51 |
import Hledger.Cli hiding (mode, progname,prognameandversion) |
| 52 |
import Hledger.UI.UIOptions |
| 53 |
import Hledger.UI.UITypes |
| 54 |
import Hledger.UI.UIUtils |
| 55 |
import Data.Function ((&)) |
| 56 |
|
| 57 |
|
| 58 |
-- | Regenerate the content of any screen from new options, reporting date and journal. |
| 59 |
screenUpdate :: UIOpts -> Day -> Journal -> Screen -> Screen |
| 60 |
screenUpdate opts d j = \case |
| 61 |
MS sst -> MS $ msUpdate sst -- opts d j ass |
| 62 |
AS sst -> AS $ asUpdate opts d j sst |
| 63 |
CS sst -> CS $ csUpdate opts d j sst |
| 64 |
BS sst -> BS $ bsUpdate opts d j sst |
| 65 |
IS sst -> IS $ isUpdate opts d j sst |
| 66 |
RS sst -> RS $ rsUpdate opts d j sst |
| 67 |
TS sst -> TS $ tsUpdate sst |
| 68 |
ES sst -> ES $ esUpdate sst |
| 69 |
|
| 70 |
-- | Construct an error screen. |
| 71 |
-- Screen-specific arguments: the error message to show. |
| 72 |
esNew :: String -> Screen |
| 73 |
esNew msg = |
| 74 |
dbgui "esNew" $ |
| 75 |
ES ESS { |
| 76 |
_essError = msg |
| 77 |
,_essUnused = () |
| 78 |
} |
| 79 |
|
| 80 |
-- | Update an error screen. Currently a no-op since error screen |
| 81 |
-- depends only on its screen-specific state. |
| 82 |
esUpdate :: ErrorScreenState -> ErrorScreenState |
| 83 |
esUpdate = dbgui "esUpdate`" |
| 84 |
|
| 85 |
-- | Construct a menu screen, with the first item selected. |
| 86 |
-- Screen-specific arguments: none. |
| 87 |
msNew :: Screen |
| 88 |
msNew = |
| 89 |
dbgui "msNew" $ |
| 90 |
MS MSS { _mssList = list MenuList (V.fromList items ) 1, _mssUnused = () } |
| 91 |
where |
| 92 |
-- keep synced with: indexes below, initial screen stack setup in UI.Main |
| 93 |
items = [ |
| 94 |
MenuScreenItem "Cash accounts" CashScreen |
| 95 |
,MenuScreenItem "Balance sheet accounts" Balancesheet |
| 96 |
,MenuScreenItem "Income statement accounts" Incomestatement |
| 97 |
,MenuScreenItem "All accounts" Accounts |
| 98 |
] |
| 99 |
|
| 100 |
-- keep synced with items above. |
| 101 |
-- | Positions of menu screen items, so we can move selection to them. |
| 102 |
[ |
| 103 |
csItemIndex, |
| 104 |
bsItemIndex, |
| 105 |
isItemIndex, |
| 106 |
asItemIndex |
| 107 |
] = [0..3] :: [Int] |
| 108 |
|
| 109 |
-- | Update a menu screen. Currently a no-op since menu screen |
| 110 |
-- has unchanging content. |
| 111 |
msUpdate :: MenuScreenState -> MenuScreenState |
| 112 |
msUpdate = dbgui "msUpdate" |
| 113 |
|
| 114 |
nullass macct = ASS { |
| 115 |
_assSelectedAccount = fromMaybe "" macct |
| 116 |
,_assList = list AccountsList (V.fromList []) 1 |
| 117 |
} |
| 118 |
|
| 119 |
-- | Construct an accounts screen listing the appropriate set of accounts, |
| 120 |
-- with the appropriate one selected. |
| 121 |
-- Screen-specific arguments: the account to select if any. |
| 122 |
asNew :: UIOpts -> Day -> Journal -> Maybe AccountName -> Screen |
| 123 |
asNew uopts d j macct = dbgui "asNew" $ AS $ asUpdate uopts d j $ nullass macct |
| 124 |
|
| 125 |
-- | Update an accounts screen's state from these options, reporting date, and journal. |
| 126 |
asUpdate :: UIOpts -> Day -> Journal -> AccountsScreenState -> AccountsScreenState |
| 127 |
asUpdate uopts d = dbgui "asUpdate" . |
| 128 |
asUpdateHelper rspec d copts roptsmod extraquery |
| 129 |
where |
| 130 |
UIOpts{uoCliOpts=copts@CliOpts{reportspec_=rspec}} = uopts |
| 131 |
roptsmod = id |
| 132 |
extraquery = Any |
| 133 |
|
| 134 |
-- | Update an accounts-like screen's state from this report spec, reporting date, |
| 135 |
-- cli options, report options modifier, extra query, and journal. |
| 136 |
asUpdateHelper :: ReportSpec -> Day -> CliOpts -> (ReportOpts -> ReportOpts) -> Query -> Journal -> AccountsScreenState -> AccountsScreenState |
| 137 |
asUpdateHelper rspec0 d copts roptsModify extraquery j ass = dbgui "asUpdateHelper" |
| 138 |
ass{_assList=l} |
| 139 |
where |
| 140 |
ropts = roptsModify $ _rsReportOpts rspec0 |
| 141 |
rspec = |
| 142 |
updateReportSpec |
| 143 |
ropts |
| 144 |
rspec0{_rsDay=d} -- update to the current date, might have changed since program start |
| 145 |
& either (error' "asUpdateHelper: adjusting the query, should not have failed") id -- PARTIAL: |
| 146 |
& reportSpecSetFutureAndForecast (forecast_ $ inputopts_ copts) -- include/exclude future & forecast transactions |
| 147 |
& reportSpecAddQuery extraquery -- add any extra restrictions |
| 148 |
|
| 149 |
l = listMoveToIfDisplayItems selidx displayitems $ list AccountsList (V.fromList $ displayitems ++ blankitems) 1 |
| 150 |
where |
| 151 |
-- which account should be selected ? |
| 152 |
selidx = headDef 0 $ catMaybes [ |
| 153 |
elemIndex a as -- the one previously selected, if it can be found |
| 154 |
,findIndex (a `isAccountNamePrefixOf`) as -- or the first account found with the same prefix |
| 155 |
,Just $ max 0 (length (filter (< a) as) - 1) -- otherwise, the alphabetically preceding account. |
| 156 |
] |
| 157 |
where |
| 158 |
a = _assSelectedAccount ass |
| 159 |
as = map asItemAccountName displayitems |
| 160 |
|
| 161 |
displayitems = map displayitem items |
| 162 |
where |
| 163 |
-- run the report |
| 164 |
(items, _) = styleAmounts styles $ balanceReport rspec j |
| 165 |
where |
| 166 |
styles = journalCommodityStylesWith HardRounding j |
| 167 |
|
| 168 |
-- pre-render a list item |
| 169 |
displayitem (fullacct, shortacct, indent, bal) = |
| 170 |
AccountsScreenItem{asItemIndentLevel = indent |
| 171 |
,asItemAccountName = fullacct |
| 172 |
,asItemDisplayAccountName = replaceHiddenAccountsNameWith "All" $ if tree_ ropts then shortacct else fullacct |
| 173 |
,asItemMixedAmount = Just bal |
| 174 |
} |
| 175 |
|
| 176 |
-- blanks added for scrolling control, cf RegisterScreen. |
| 177 |
blankitems = replicate uiNumBlankItems -- XXX ugly hard-coded value. When debugging, changing to 0 reduces noise. |
| 178 |
AccountsScreenItem{asItemIndentLevel = 0 |
| 179 |
,asItemAccountName = "" |
| 180 |
,asItemDisplayAccountName = "" |
| 181 |
,asItemMixedAmount = Nothing |
| 182 |
} |
| 183 |
|
| 184 |
-- | Construct a balance sheet screen listing the appropriate set of accounts, |
| 185 |
-- with the appropriate one selected. |
| 186 |
-- Screen-specific arguments: the account to select if any. |
| 187 |
bsNew :: UIOpts -> Day -> Journal -> Maybe AccountName -> Screen |
| 188 |
bsNew uopts d j macct = dbgui "bsNew" $ BS $ bsUpdate uopts d j $ nullass macct |
| 189 |
|
| 190 |
-- | Update a balance sheet screen's state from these options, reporting date, and journal. |
| 191 |
bsUpdate :: UIOpts -> Day -> Journal -> AccountsScreenState -> AccountsScreenState |
| 192 |
bsUpdate uopts d = dbgui "bsUpdate" . |
| 193 |
asUpdateHelper rspec d copts roptsmod extraquery |
| 194 |
where |
| 195 |
UIOpts{uoCliOpts=copts@CliOpts{reportspec_=rspec}} = uopts |
| 196 |
roptsmod ropts = ropts{balanceaccum_=Historical} -- always show historical end balances |
| 197 |
extraquery = Type [Asset,Liability,Equity] -- restrict to balance sheet accounts |
| 198 |
|
| 199 |
-- | Construct a cash accounts screen listing the appropriate set of accounts, |
| 200 |
-- with the appropriate one selected. |
| 201 |
-- Screen-specific arguments: the account to select if any. |
| 202 |
csNew :: UIOpts -> Day -> Journal -> Maybe AccountName -> Screen |
| 203 |
csNew uopts d j macct = dbgui "csNew" $ CS $ csUpdate uopts d j $ nullass macct |
| 204 |
|
| 205 |
-- | Update a balance sheet screen's state from these options, reporting date, and journal. |
| 206 |
csUpdate :: UIOpts -> Day -> Journal -> AccountsScreenState -> AccountsScreenState |
| 207 |
csUpdate uopts d = dbgui "csUpdate" . |
| 208 |
asUpdateHelper rspec d copts roptsmod extraquery |
| 209 |
where |
| 210 |
UIOpts{uoCliOpts=copts@CliOpts{reportspec_=rspec}} = uopts |
| 211 |
roptsmod ropts = ropts{balanceaccum_=Historical} -- always show historical end balances |
| 212 |
extraquery = Type [Cash] -- restrict to cash accounts |
| 213 |
|
| 214 |
-- | Construct an income statement screen listing the appropriate set of accounts, |
| 215 |
-- with the appropriate one selected. |
| 216 |
-- Screen-specific arguments: the account to select if any. |
| 217 |
isNew :: UIOpts -> Day -> Journal -> Maybe AccountName -> Screen |
| 218 |
isNew uopts d j macct = dbgui "isNew" $ IS $ isUpdate uopts d j $ nullass macct |
| 219 |
|
| 220 |
-- | Update an income statement screen's state from these options, reporting date, and journal. |
| 221 |
isUpdate :: UIOpts -> Day -> Journal -> AccountsScreenState -> AccountsScreenState |
| 222 |
isUpdate uopts d = dbgui "isUpdate" . |
| 223 |
asUpdateHelper rspec d copts roptsmod extraquery |
| 224 |
where |
| 225 |
UIOpts{uoCliOpts=copts@CliOpts{reportspec_=rspec}} = uopts |
| 226 |
roptsmod ropts = ropts{balanceaccum_=PerPeriod} -- always show historical end balances |
| 227 |
extraquery = Type [Revenue, Expense] -- restrict to income statement accounts |
| 228 |
|
| 229 |
-- | Construct a register screen listing the appropriate set of transactions, |
| 230 |
-- with the appropriate one selected. |
| 231 |
-- Screen-specific arguments: the account whose register this is, |
| 232 |
-- whether to force inclusive balances. |
| 233 |
rsNew :: UIOpts -> Day -> Journal -> AccountName -> Bool -> Screen |
| 234 |
rsNew uopts d j acct forceinclusive = -- XXX forcedefaultselection - whether to force selecting the last transaction. |
| 235 |
dbgui "rsNew" $ |
| 236 |
RS $ |
| 237 |
rsUpdate uopts d j $ |
| 238 |
RSS { |
| 239 |
_rssAccount = replaceHiddenAccountsNameWith "*" acct |
| 240 |
,_rssForceInclusive = forceinclusive |
| 241 |
,_rssList = list RegisterList (V.fromList []) 1 |
| 242 |
} |
| 243 |
|
| 244 |
-- | Update a register screen from these options, reporting date, and journal. |
| 245 |
rsUpdate :: UIOpts -> Day -> Journal -> RegisterScreenState -> RegisterScreenState |
| 246 |
rsUpdate uopts d j rss@RSS{_rssAccount, _rssForceInclusive, _rssList=oldlist} = |
| 247 |
dbgui "rsUpdate" |
| 248 |
rss{_rssList=l'} |
| 249 |
where |
| 250 |
UIOpts{uoCliOpts=copts@CliOpts{reportspec_=rspec@ReportSpec{_rsReportOpts=ropts}}} = uopts |
| 251 |
-- gather arguments and queries |
| 252 |
-- XXX temp |
| 253 |
inclusive = tree_ ropts || _rssForceInclusive |
| 254 |
thisacctq = Acct $ mkregex _rssAccount |
| 255 |
where |
| 256 |
mkregex = if inclusive then accountNameToAccountRegex else accountNameToAccountOnlyRegex |
| 257 |
|
| 258 |
-- adjust the report options and report spec, carefully as usual to avoid screwups (#1523) |
| 259 |
ropts' = ropts { |
| 260 |
-- ignore any depth limit, as in postingsReport; allows register's total to match accounts screen |
| 261 |
depth_=mempty |
| 262 |
-- do not strip prices so we can toggle costs within the ui |
| 263 |
, show_costs_=True |
| 264 |
-- XXX aregister also has this, needed ? |
| 265 |
-- always show historical balance |
| 266 |
-- , balanceaccum_= Historical |
| 267 |
} |
| 268 |
rspec' = |
| 269 |
updateReportSpec ropts' rspec{_rsDay=d} |
| 270 |
& either (error' "rsUpdate: adjusting the query for register, should not have failed") id -- PARTIAL: |
| 271 |
& reportSpecSetFutureAndForecast (forecast_ $ inputopts_ copts) |
| 272 |
|
| 273 |
-- gather transactions to display |
| 274 |
items = styleAmounts styles $ accountTransactionsReport rspec' j thisacctq |
| 275 |
where |
| 276 |
styles = journalCommodityStylesWith HardRounding j |
| 277 |
items' = |
| 278 |
(if empty_ ropts then id else filter (not . mixedAmountLooksZero . fifth6)) $ -- without --empty, exclude no-change txns |
| 279 |
reverse -- most recent last |
| 280 |
items |
| 281 |
|
| 282 |
-- pre-render the list items, helps calculate column widths |
| 283 |
displayitems = map displayitem items' |
| 284 |
where |
| 285 |
displayitem (t, _, _issplit, otheraccts, change, bal) = |
| 286 |
RegisterScreenItem{rsItemDate = showDate $ transactionRegisterDate wd (_rsQuery rspec') thisacctq t |
| 287 |
,rsItemStatus = tstatus t |
| 288 |
,rsItemDescription = tdescription t |
| 289 |
,rsItemOtherAccounts = T.intercalate ", " . map accountSummarisedName $ nub otheraccts |
| 290 |
-- _ -> "<split>" -- should do this if accounts field width < 30 |
| 291 |
,rsItemChangeAmount = showamt change |
| 292 |
,rsItemBalanceAmount = showamt bal |
| 293 |
,rsItemTransaction = t |
| 294 |
} |
| 295 |
where |
| 296 |
showamt = showMixedAmountB oneLineNoCostFmt{displayMaxWidth=Just 3} |
| 297 |
wd = whichDate ropts' |
| 298 |
|
| 299 |
-- blank items are added to allow more control of scroll position; we won't allow movement over these. |
| 300 |
-- XXX Ugly. Changing to 0 helps when debugging. |
| 301 |
blankitems = replicate uiNumBlankItems |
| 302 |
RegisterScreenItem{rsItemDate = "" |
| 303 |
,rsItemStatus = Unmarked |
| 304 |
,rsItemDescription = "" |
| 305 |
,rsItemOtherAccounts = "" |
| 306 |
,rsItemChangeAmount = mempty |
| 307 |
,rsItemBalanceAmount = mempty |
| 308 |
,rsItemTransaction = nulltransaction |
| 309 |
} |
| 310 |
|
| 311 |
-- build the new list widget |
| 312 |
l = list RegisterList (V.fromList $ displayitems ++ blankitems) 1 |
| 313 |
|
| 314 |
-- ensure the appropriate list item is selected: |
| 315 |
-- if forcedefaultselection is true, the last (latest) transaction; XXX still needed ? |
| 316 |
-- otherwise, the previously selected transaction if possible; |
| 317 |
-- otherwise, the transaction nearest in date to it; |
| 318 |
-- or if there's several with the same date, the nearest in journal order; |
| 319 |
-- otherwise, the last (latest) transaction. |
| 320 |
l' = listMoveToIfDisplayItems newselidx displayitems l |
| 321 |
where |
| 322 |
endidx = max 0 $ length displayitems - 1 |
| 323 |
newselidx = |
| 324 |
-- case (forcedefaultselection, listSelectedElement _rssList) of |
| 325 |
-- (True, _) -> endidx |
| 326 |
-- (_, Nothing) -> endidx |
| 327 |
-- (_, Just (_, RegisterScreenItem{rsItemTransaction=Transaction{tindex=prevselidx, tdate=prevseld}})) -> |
| 328 |
-- headDef endidx $ catMaybes [ |
| 329 |
-- findIndex ((==prevselidx) . tindex . rsItemTransaction) displayitems |
| 330 |
-- ,findIndex ((==nearestidbydatethenid) . Just . tindex . rsItemTransaction) displayitems |
| 331 |
-- ] |
| 332 |
-- where |
| 333 |
-- nearestidbydatethenid = third3 <$> (headMay $ sort |
| 334 |
-- [(abs $ diffDays (tdate t) prevseld, abs (tindex t - prevselidx), tindex t) | t <- ts]) |
| 335 |
-- ts = map rsItemTransaction displayitems |
| 336 |
case listSelectedElement oldlist of |
| 337 |
Nothing -> endidx |
| 338 |
Just (_, RegisterScreenItem{rsItemTransaction=Transaction{tindex=prevselidx, tdate=prevseld}}) -> |
| 339 |
headDef endidx $ catMaybes [ |
| 340 |
findIndex ((==prevselidx) . tindex . rsItemTransaction) displayitems |
| 341 |
,findIndex ((==nearestidbydatethenid) . Just . tindex . rsItemTransaction) displayitems |
| 342 |
] |
| 343 |
where |
| 344 |
nearestidbydatethenid = third3 <$> (headMay $ sort |
| 345 |
[(abs $ diffDays (tdate t) prevseld, abs (tindex t - prevselidx), tindex t) | t <- ts]) |
| 346 |
ts = map rsItemTransaction displayitems |
| 347 |
|
| 348 |
-- | Construct a transaction screen showing one of a given list of transactions, |
| 349 |
-- with the ability to step back and forth through the list. |
| 350 |
-- Screen-specific arguments: the account whose transactions are being shown, |
| 351 |
-- the list of showable transactions, the currently shown transaction. |
| 352 |
tsNew :: AccountName -> [NumberedTransaction] -> NumberedTransaction -> Screen |
| 353 |
tsNew acct nts nt = |
| 354 |
dbgui "tsNew" $ |
| 355 |
TS TSS{ |
| 356 |
_tssAccount = acct |
| 357 |
,_tssTransactions = nts |
| 358 |
,_tssTransaction = nt |
| 359 |
} |
| 360 |
|
| 361 |
-- | Update a transaction screen. |
| 362 |
-- This currently does nothing because the initialisation in rsHandle is not so easy to extract. |
| 363 |
-- To see the updated transaction, one must exit and re-enter the transaction screen. |
| 364 |
-- See also tsHandle. |
| 365 |
tsUpdate :: TransactionScreenState -> TransactionScreenState |
| 366 |
tsUpdate = dbgui "tsUpdate" |
| 367 |
|
| 368 |
-- | Set selected index of a list if there are displayitems. |
| 369 |
-- If there are no displayitems, remove the selected index of the list. |
| 370 |
listMoveToIfDisplayItems :: Int -> [item] -> GenericList Name V.Vector item -> GenericList Name V.Vector item |
| 371 |
listMoveToIfDisplayItems idx displayItems = |
| 372 |
if null displayItems |
| 373 |
then over listSelectedL $ const Nothing |
| 374 |
else listMoveTo idx |