| 1 |
-- The transaction screen, showing a single transaction's general journal entry. |
| 2 |
|
| 3 |
{-# LANGUAGE OverloadedStrings #-} |
| 4 |
{-# LANGUAGE RecordWildCards #-} |
| 5 |
{-# LANGUAGE TupleSections #-} |
| 6 |
{-# OPTIONS_GHC -Wno-incomplete-record-updates #-} |
| 7 |
|
| 8 |
module Hledger.UI.TransactionScreen |
| 9 |
(tsNew |
| 10 |
,tsUpdate |
| 11 |
,tsDraw |
| 12 |
,tsHandle |
| 13 |
) where |
| 14 |
|
| 15 |
import Brick |
| 16 |
import Brick.Widgets.Edit (editorText, renderEditor) |
| 17 |
import Brick.Widgets.List (listMoveTo) |
| 18 |
import Control.Monad.IO.Class (liftIO) |
| 19 |
import Data.List |
| 20 |
import Data.Maybe |
| 21 |
import Data.Text qualified as T |
| 22 |
import Graphics.Vty (Event(..),Key(..),Modifier(..), Button (BLeft)) |
| 23 |
import System.Exit (ExitCode (..)) |
| 24 |
|
| 25 |
import Hledger |
| 26 |
import Hledger.Cli hiding (mode, prices, progname,prognameandversion) |
| 27 |
import Hledger.UI.UIOptions |
| 28 |
import Hledger.UI.UITypes |
| 29 |
import Hledger.UI.UIState |
| 30 |
import Hledger.UI.UIUtils |
| 31 |
import Hledger.UI.UIScreens |
| 32 |
import Hledger.UI.Editor |
| 33 |
import Hledger.UI.ErrorScreen (uiCheckBalanceAssertions, uiReload, uiReloadIfFileChanged, uiToggleBalanceAssertions) |
| 34 |
import Hledger.UI.RegisterScreen (rsHandle) |
| 35 |
|
| 36 |
tsDraw :: UIState -> [Widget Name] |
| 37 |
tsDraw UIState{aopts=UIOpts{uoCliOpts=copts@CliOpts{reportspec_=rspec@ReportSpec{_rsReportOpts=ropts}}} |
| 38 |
,ajournal=j |
| 39 |
,aScreen=TS TSS{_tssTransaction=(i,t') |
| 40 |
,_tssTransactions=nts |
| 41 |
,_tssAccount=acct |
| 42 |
} |
| 43 |
,aMode=mode |
| 44 |
} = |
| 45 |
case mode of |
| 46 |
Help -> [helpDialog, maincontent] |
| 47 |
_ -> [maincontent] |
| 48 |
where |
| 49 |
maincontent = Widget Greedy Greedy $ render $ defaultLayout toplabel bottomlabel txneditor |
| 50 |
where |
| 51 |
-- as with print, show amounts with all of their decimal places |
| 52 |
t = transactionMapPostingAmounts mixedAmountSetFullPrecision t' |
| 53 |
|
| 54 |
-- XXX would like to shrink the editor to the size of the entry, |
| 55 |
-- so handler can more easily detect clicks below it |
| 56 |
txneditor = |
| 57 |
renderEditor (vBox . map txt) False $ |
| 58 |
editorText TransactionEditor Nothing $ |
| 59 |
showTxn ropts rspec j t |
| 60 |
|
| 61 |
toplabel = |
| 62 |
str "Transaction " |
| 63 |
-- <+> withAttr ("border" <> "bold") (str $ "#" ++ show (tindex t)) |
| 64 |
-- <+> str (" ("++show i++" of "++show (length nts)++" in "++acct++")") |
| 65 |
<+> (str $ "#" ++ show (tindex t)) |
| 66 |
<+> str " (" |
| 67 |
<+> withAttr (attrName "border" <> attrName "bold") (str $ show i) |
| 68 |
<+> str (" of "++show (length nts)) |
| 69 |
<+> togglefilters |
| 70 |
<+> borderQueryStr (unwords . map (quoteIfNeeded . T.unpack) $ querystring_ ropts) |
| 71 |
<+> str (" in "++T.unpack (replaceHiddenAccountsNameWith "All" acct)++")") |
| 72 |
<+> (if ignore_assertions_ . balancingopts_ $ inputopts_ copts then withAttr (attrName "border" <> attrName "query") (str " ignoring balance assertions") else str "") |
| 73 |
where |
| 74 |
togglefilters = |
| 75 |
case concat [ |
| 76 |
uiShowStatus copts $ statuses_ ropts |
| 77 |
,if real_ ropts then ["real"] else [] |
| 78 |
,if empty_ ropts then [] else ["nonzero"] |
| 79 |
] of |
| 80 |
[] -> str "" |
| 81 |
fs -> withAttr (attrName "border" <> attrName "query") (str $ " " ++ intercalate ", " fs) |
| 82 |
|
| 83 |
bottomlabel = quickhelp |
| 84 |
-- case mode of |
| 85 |
-- Minibuffer ed -> minibuffer ed |
| 86 |
-- _ -> quickhelp |
| 87 |
where |
| 88 |
quickhelp = borderKeysStr [ |
| 89 |
("LEFT", "back") |
| 90 |
,("UP/DOWN", "prev/next txn") |
| 91 |
--,("ESC", "cancel/top") |
| 92 |
-- ,("a", "add") |
| 93 |
,("E", "edit") |
| 94 |
,("g", "reload") |
| 95 |
,("?", "help") |
| 96 |
-- ,("q", "quit") |
| 97 |
] |
| 98 |
|
| 99 |
tsDraw _ = errorWrongScreenType "tsDraw" -- PARTIAL: |
| 100 |
|
| 101 |
-- Render a transaction suitably for the transaction screen. |
| 102 |
showTxn :: ReportOpts -> ReportSpec -> Journal -> Transaction -> T.Text |
| 103 |
showTxn ropts rspec j t = |
| 104 |
showTransactionOneLineAmounts |
| 105 |
$ maybe id (transactionApplyValuation prices styles periodlast (_rsDay rspec)) (value_ ropts) |
| 106 |
$ maybe id transactionToCost (conversionop_ ropts) t |
| 107 |
-- (if real_ ropts then filterTransactionPostings (Real True) else id) -- filter postings by --real |
| 108 |
where |
| 109 |
prices = journalPriceOracle (infer_prices_ ropts) j |
| 110 |
styles = journalCommodityStyles j |
| 111 |
periodlast = |
| 112 |
fromMaybe (error' "TransactionScreen: expected a non-empty journal") $ -- PARTIAL: shouldn't happen |
| 113 |
reportPeriodOrJournalLastDay rspec j |
| 114 |
|
| 115 |
tsHandle :: BrickEvent Name AppEvent -> EventM Name UIState () |
| 116 |
tsHandle ev = do |
| 117 |
ui0 <- get' |
| 118 |
case ui0 of |
| 119 |
ui@UIState{aScreen=TS TSS{_tssTransaction=(i,t), _tssTransactions=nts} |
| 120 |
,aopts=UIOpts{uoCliOpts=copts} |
| 121 |
,ajournal=j |
| 122 |
,aMode=mode |
| 123 |
} -> |
| 124 |
case mode of |
| 125 |
Help -> |
| 126 |
case ev of |
| 127 |
-- VtyEvent (EvKey (KChar 'q') []) -> halt |
| 128 |
VtyEvent (EvKey (KChar 'l') [MCtrl]) -> redraw |
| 129 |
VtyEvent (EvKey (KChar 'z') [MCtrl]) -> suspend ui |
| 130 |
_ -> helpHandle ev |
| 131 |
|
| 132 |
_ -> do |
| 133 |
d <- liftIO getCurrentDay |
| 134 |
let |
| 135 |
(iprev,tprev) = maybe (i,t) ((i-1),) $ lookup (i-1) nts |
| 136 |
(inext,tnext) = maybe (i,t) ((i+1),) $ lookup (i+1) nts |
| 137 |
case ev of |
| 138 |
VtyEvent (EvKey (KChar 'q') []) -> halt |
| 139 |
VtyEvent (EvKey KEsc []) -> put' $ resetScreens d ui |
| 140 |
VtyEvent (EvKey (KChar c) []) | c == '?' -> put' $ setMode Help ui |
| 141 |
|
| 142 |
-- g or file change: reload the journal and rebuild app state. |
| 143 |
e | e `elem` [VtyEvent (EvKey (KChar 'g') []), AppEvent FileChange] -> |
| 144 |
tsReload copts d ui |
| 145 |
|
| 146 |
-- for debugging; leaving these here because they were hard to find |
| 147 |
-- \u -> dbguiEv (pshow u) >> put' u -- doesn't log |
| 148 |
-- \UIState{aScreen=TS tss} -> error' $ pshow $ _tssTransaction tss |
| 149 |
|
| 150 |
-- E: run editor, reload the journal. |
| 151 |
VtyEvent (EvKey (KChar 'E') []) -> do |
| 152 |
suspendAndResume' $ do |
| 153 |
let (pos,f) = case tsourcepos t of (SourcePos f' l1 c1,_) -> (Just (unPos l1, Just $ unPos c1),f') |
| 154 |
exitcode <- runEditor pos f |
| 155 |
case exitcode of |
| 156 |
ExitSuccess -> return () |
| 157 |
ExitFailure c -> error' $ "running the text editor failed with exit code " ++ show c |
| 158 |
tsReloadIfFileChanged copts d j ui |
| 159 |
|
| 160 |
AppEvent (DateChange old _) | isStandardPeriod p && p `periodContainsDate` old -> |
| 161 |
put' $ regenerateScreens j d $ setReportPeriod (DayPeriod d) ui |
| 162 |
where |
| 163 |
p = reportPeriod ui |
| 164 |
|
| 165 |
VtyEvent (EvKey (KChar 'I') []) -> uiToggleBalanceAssertions d ui |
| 166 |
|
| 167 |
-- for toggles that may change the current/prev/next transactions, |
| 168 |
-- we must regenerate the transaction list, like the g handler above ? with regenerateTransactions ? TODO WIP |
| 169 |
-- EvKey (KChar 'E') [] -> put' $ regenerateScreens j d $ stToggleEmpty ui |
| 170 |
-- EvKey (KChar 'C') [] -> put' $ regenerateScreens j d $ stToggleCleared ui |
| 171 |
-- EvKey (KChar 'R') [] -> put' $ regenerateScreens j d $ stToggleReal ui |
| 172 |
VtyEvent (EvKey (KChar 'B') []) -> put' . regenerateScreens j d $ toggleConversionOp ui |
| 173 |
VtyEvent (EvKey (KChar 'V') []) -> put' . regenerateScreens j d $ toggleValue ui |
| 174 |
|
| 175 |
VtyEvent e | e `elem` moveUpEvents -> put' $ tsSelect iprev tprev ui |
| 176 |
VtyEvent e | e `elem` moveDownEvents -> put' $ tsSelect inext tnext ui |
| 177 |
|
| 178 |
-- exit screen on LEFT |
| 179 |
VtyEvent e | e `elem` moveLeftEvents -> put' . popScreen $ tsSelect i t ui -- Probably not necessary to tsSelect here, but it's safe. |
| 180 |
-- or on a click in the app's left margin. |
| 181 |
VtyEvent (EvMouseUp x _y (Just BLeft)) | x==0 -> put' . popScreen $ tsSelect i t ui |
| 182 |
|
| 183 |
VtyEvent (EvKey (KChar 'l') [MCtrl]) -> redraw |
| 184 |
VtyEvent (EvKey (KChar 'z') [MCtrl]) -> suspend ui |
| 185 |
_ -> return () |
| 186 |
|
| 187 |
_ -> errorWrongScreenType "tsHandle" |
| 188 |
|
| 189 |
where |
| 190 |
-- Reload and fully regenerate the transaction screen. |
| 191 |
-- XXX On transaction screen or below, this is tricky because of a current limitation of regenerateScreens. |
| 192 |
-- For now we try to work around by re-entering the screen(s). |
| 193 |
-- This can show flicker in the UI and it's hard to handle all situations robustly. |
| 194 |
tsReload copts d ui = uiReload copts d ui >>= reEnterTransactionScreen copts d |
| 195 |
tsReloadIfFileChanged copts d j ui = liftIO (uiReloadIfFileChanged copts d j ui) >>= reEnterTransactionScreen copts d |
| 196 |
|
| 197 |
reEnterTransactionScreen _copts d ui = do |
| 198 |
-- 1. If uiReload (or checking balance assertions) moved us to the error screen, save that, and return to the transaction screen. |
| 199 |
let |
| 200 |
(merrscr, uiTxn) = case aScreen $ uiCheckBalanceAssertions d ui of |
| 201 |
s@(ES _) -> (Just s, popScreen ui) |
| 202 |
_ -> (Nothing, ui) |
| 203 |
-- 2. Exit to register screen |
| 204 |
let uiReg = popScreen uiTxn |
| 205 |
put' uiReg |
| 206 |
-- 3. Re-enter the transaction screen |
| 207 |
rsHandle (VtyEvent (EvKey KEnter [])) -- PARTIAL assumes we are on the register screen. |
| 208 |
-- 4. Return to the error screen (below the transaction screen) if there was one. |
| 209 |
-- Next events will be handled by esHandle. Error repair will return to the transaction screen. |
| 210 |
maybe (return ()) (put' . flip pushScreen uiTxn) merrscr |
| 211 |
-- doesn't uiTxn have old state from before step 3 ? seems to work |
| 212 |
|
| 213 |
-- XXX some problem: |
| 214 |
-- 4. Reload once more, possibly re-entering the error screen, by sending a g event. |
| 215 |
-- sendVtyEvents [EvKey (KChar 'g') []] -- XXX Might be disrupted if other events are queued |
| 216 |
|
| 217 |
-- XXX doesn't update on non-error change: |
| 218 |
-- 4. Reload once more, possibly re-entering the error screen. |
| 219 |
-- uiTxnOrErr <- uiReload copts d uiTxn |
| 220 |
-- uiReloadIfChanged ? |
| 221 |
-- uiCheckBalanceAssertions ? seems unneeded |
| 222 |
-- put' uiTxnOrErr |
| 223 |
|
| 224 |
-- XXX not working right: |
| 225 |
-- -- 1. If uiReload (or checking balance assertions) moved us to the error screen, exit to the transaction screen. |
| 226 |
-- let |
| 227 |
-- uiTxn = case aScreen $ uiCheckBalanceAssertions d ui of |
| 228 |
-- ES _ -> popScreen ui |
| 229 |
-- _ -> ui |
| 230 |
-- -- 2. Exit to register screen |
| 231 |
-- put' $ popScreen uiTxn |
| 232 |
-- -- 3. Re-enter the transaction screen, and reload once more. |
| 233 |
-- sendVtyEvents [EvKey KEnter [], EvKey (KChar 'g') []] -- XXX Might be disrupted if other events are queued |
| 234 |
|
| 235 |
|
| 236 |
-- | Select a new transaction and update the previous register screen |
| 237 |
tsSelect :: Integer -> Transaction -> UIState -> UIState |
| 238 |
tsSelect i t ui@UIState{aScreen=TS sst} = case aPrevScreens ui of |
| 239 |
x:xs -> ui'{aPrevScreens=rsSelect i x : xs} |
| 240 |
[] -> ui' |
| 241 |
where ui' = ui{aScreen=TS sst{_tssTransaction=(i,t)}} |
| 242 |
tsSelect _ _ ui = ui |
| 243 |
|
| 244 |
-- | Select the nth item on the register screen. |
| 245 |
rsSelect :: Integer -> Screen -> Screen |
| 246 |
rsSelect i (RS sst@RSS{..}) = RS sst{_rssList=listMoveTo (fromInteger $ i-1) _rssList} |
| 247 |
rsSelect _ scr = scr |