| 1 |
{- | Rendering & misc. helpers. -} |
| 2 |
|
| 3 |
{-# LANGUAGE CPP #-} |
| 4 |
{-# LANGUAGE OverloadedStrings #-} |
| 5 |
{-# LANGUAGE NamedFieldPuns #-} |
| 6 |
{-# LANGUAGE LambdaCase #-} |
| 7 |
|
| 8 |
module Hledger.UI.UIUtils ( |
| 9 |
borderDepthStr |
| 10 |
,borderKeysStr |
| 11 |
,borderKeysStr' |
| 12 |
,borderPeriodStr |
| 13 |
,borderQueryStr |
| 14 |
,defaultLayout |
| 15 |
,helpDialog |
| 16 |
,helpHandle |
| 17 |
,minibuffer |
| 18 |
,moveDownEvents |
| 19 |
,moveLeftEvents |
| 20 |
,moveRightEvents |
| 21 |
,moveUpEvents |
| 22 |
,normaliseMovementKeys |
| 23 |
,renderToggle |
| 24 |
,renderToggle1 |
| 25 |
,replaceHiddenAccountsNameWith |
| 26 |
,scrollSelectionToMiddle |
| 27 |
,get' |
| 28 |
,put' |
| 29 |
,modify' |
| 30 |
,suspend |
| 31 |
,redraw |
| 32 |
,reportSpecAddQuery |
| 33 |
,reportSpecSetFutureAndForecast |
| 34 |
,listScrollPushingSelection |
| 35 |
,dbgui |
| 36 |
,dbguiIO |
| 37 |
,dbguiEv |
| 38 |
,dbguiScreensEv |
| 39 |
,showScreenId |
| 40 |
,showScreenRegisterDescriptions |
| 41 |
,showScreenSelection |
| 42 |
,mapScreens |
| 43 |
,uiNumBlankItems |
| 44 |
,showScreenStack |
| 45 |
,sendVtyEvents |
| 46 |
) |
| 47 |
where |
| 48 |
|
| 49 |
import Brick |
| 50 |
import Brick.Widgets.Border |
| 51 |
import Brick.Widgets.Border.Style |
| 52 |
import Brick.Widgets.Dialog |
| 53 |
import Brick.Widgets.Edit |
| 54 |
import Brick.Widgets.List (List, listSelectedL, listNameL, listItemHeightL, listSelected, listMoveDown, listMoveUp, GenericList, listElements) |
| 55 |
import Control.Concurrent.STM (atomically, writeTChan) -- GHC only |
| 56 |
import Control.Monad.IO.Class |
| 57 |
import Data.Bifunctor (second) |
| 58 |
import Data.List |
| 59 |
import Data.Text qualified as T |
| 60 |
import Data.Time (addDays) |
| 61 |
import Graphics.Vty |
| 62 |
(Event(..),Key(..),Modifier(..),Vty(..),Color,Attr,currentAttr,refresh, displayBounds |
| 63 |
-- ,Output(displayBounds,mkDisplayContext),DisplayContext(..) |
| 64 |
,Vty (inputIface), InternalEvent (InputEvent), Input (eventChannel) |
| 65 |
) |
| 66 |
import Lens.Micro.Platform |
| 67 |
|
| 68 |
import Hledger |
| 69 |
-- import Hledger.Cli.CliOptions (CliOpts(reportspec_)) |
| 70 |
import Hledger.Cli.DocFiles |
| 71 |
-- import Hledger.UI.UIOptions (UIOpts(uoCliOpts)) |
| 72 |
import Hledger.UI.UITypes |
| 73 |
|
| 74 |
import Data.Vector (Vector) |
| 75 |
import Data.Vector qualified as V |
| 76 |
|
| 77 |
-- | On posix platforms, send the system STOP signal to suspend the |
| 78 |
-- current program. On windows, does nothing. |
| 79 |
-- (Though, currently hledger-ui is not built on windows.) |
| 80 |
#ifdef mingw32_HOST_OS |
| 81 |
suspendSignal :: IO () |
| 82 |
suspendSignal = return () |
| 83 |
#else |
| 84 |
import System.Posix.Signals |
| 85 |
suspendSignal :: IO () |
| 86 |
suspendSignal = raiseSignal sigSTOP |
| 87 |
#endif |
| 88 |
|
| 89 |
-- Debug logging for UI state changes. |
| 90 |
-- A good place to log things of interest while debugging, see commented examples below. |
| 91 |
|
| 92 |
get' = do |
| 93 |
ui <- get |
| 94 |
dbguiEv $ "getting state: " ++ |
| 95 |
showScreenStack "" showScreenSelection ui |
| 96 |
-- (head $ lines $ pshow $ aScreen x) |
| 97 |
-- ++ " " ++ (show $ map tdescription $ jtxns $ ajournal x) |
| 98 |
-- dbguiEv $ ("query: "++) $ pshow' $ x & aopts & uoCliOpts & reportspec_ & _rsQuery |
| 99 |
-- dbguiScreensEv "getting" showScreenId x |
| 100 |
-- dbguiScreensEv "getting, with register descriptions" showScreenRegisterDescriptions x |
| 101 |
return ui |
| 102 |
|
| 103 |
put' ui = do |
| 104 |
dbguiEv $ "putting state: " ++ |
| 105 |
showScreenStack "" showScreenSelection ui |
| 106 |
-- (head $ lines $ pshow $ aScreen x) |
| 107 |
-- ++ " " ++ (show $ map tdescription $ jtxns $ ajournal x) |
| 108 |
-- dbguiEv $ ("query: "++) $ pshow' $ x & aopts & uoCliOpts & reportspec_ & _rsQuery |
| 109 |
-- dbguiScreensEv "putting" showScreenId x |
| 110 |
-- dbguiScreensEv "putting, with register descriptions" showScreenRegisterDescriptions x |
| 111 |
put ui |
| 112 |
|
| 113 |
modify' f = do |
| 114 |
ui <- get |
| 115 |
let ui' = f ui |
| 116 |
dbguiEv $ "getting state: " ++ (showScreenStack "" showScreenSelection ui) |
| 117 |
dbguiEv $ "putting state: " ++ (showScreenStack "" showScreenSelection ui') |
| 118 |
-- (head $ lines $ pshow $ aScreen x') |
| 119 |
-- ++ " " ++ (show $ map tdescription $ jtxns $ ajournal x') |
| 120 |
-- dbguiEv $ ("from: "++) $ pshow' $ x & aopts & uoCliOpts & reportspec_ & _rsQuery |
| 121 |
-- dbguiEv $ ("to: "++) $ pshow' $ x' & aopts & uoCliOpts & reportspec_ & _rsQuery |
| 122 |
-- dbguiScreensEv "getting" showScreenId x |
| 123 |
-- dbguiScreensEv "putting" showScreenId x' |
| 124 |
-- dbguiScreensEv "getting, with register descriptions" showScreenRegisterDescriptions x |
| 125 |
-- dbguiScreensEv "putting, with register descriptions" showScreenRegisterDescriptions x' |
| 126 |
modify f |
| 127 |
|
| 128 |
-- | On posix platforms, suspend the program using the STOP signal, |
| 129 |
-- like control-z in bash, returning to the original shell prompt, |
| 130 |
-- and when resumed, continue where we left off. |
| 131 |
-- On windows, does nothing. |
| 132 |
suspend :: Ord a => s -> EventM a s () |
| 133 |
suspend st = suspendAndResume $ suspendSignal >> return st |
| 134 |
|
| 135 |
-- | Tell vty to redraw the whole screen. |
| 136 |
redraw :: EventM a s () |
| 137 |
redraw = getVtyHandle >>= liftIO . refresh |
| 138 |
|
| 139 |
-- | Wrap a widget in the default hledger-ui screen layout. |
| 140 |
defaultLayout :: Widget Name -> Widget Name -> Widget Name -> Widget Name |
| 141 |
defaultLayout toplabel bottomlabel = |
| 142 |
topBottomBorderWithLabels (str " "<+>toplabel<+>str " ") (str " "<+>bottomlabel<+>str " ") . |
| 143 |
margin 1 0 Nothing |
| 144 |
-- topBottomBorderWithLabel label . |
| 145 |
-- padLeftRight 1 -- XXX should reduce inner widget's width by 2, but doesn't |
| 146 |
-- "the layout adjusts... if you use the core combinators" |
| 147 |
|
| 148 |
-- | Draw the help dialog, called when help mode is active. |
| 149 |
helpDialog :: Widget Name |
| 150 |
helpDialog = |
| 151 |
Widget Fixed Fixed $ do |
| 152 |
c <- getContext |
| 153 |
render $ |
| 154 |
withDefAttr (attrName "help") $ |
| 155 |
renderDialog (dialog (Just $ str "Help (LEFT/ESC/?/q to close help)") Nothing (c^.availWidthL)) $ -- (Just (0,[("ok",())])) |
| 156 |
padTop (Pad 0) $ padLeft (Pad 1) $ padRight (Pad 1) $ |
| 157 |
vBox [ |
| 158 |
hBox [ |
| 159 |
padRight (Pad 1) $ |
| 160 |
vBox [ |
| 161 |
withAttr (attrName "help" <> attrName "heading") $ str "Navigation" |
| 162 |
,renderKey ("UP/DOWN/PUP/PDN/HOME/END/k/j/C-p/C-n", "") |
| 163 |
,str " move selection up/down" |
| 164 |
,renderKey ("RIGHT/l/C-f", "show txns, or txn detail") |
| 165 |
,renderKey ("LEFT/h/C-b ", "go back/see other screens") |
| 166 |
,renderKey ("ESC ", "cancel, or reset app state") |
| 167 |
|
| 168 |
,str " " |
| 169 |
,withAttr (attrName "help" <> attrName "heading") $ str "Accounts screens" |
| 170 |
,renderKey ("1234567890-+ ", "set/adjust depth limit") |
| 171 |
,renderKey ("t ", "toggle accounts tree/list mode") |
| 172 |
,renderKey ("H ", "toggle historical balance/change") |
| 173 |
,str " " |
| 174 |
,withAttr (attrName "help" <> attrName "heading") $ str "Register screens" |
| 175 |
,renderKey ("t ", "toggle subaccount txns\n(and accounts tree/list mode)") |
| 176 |
,renderKey ("H ", "toggle historical/period total") |
| 177 |
,str " " |
| 178 |
,withAttr (attrName "help" <> attrName "heading") $ str "Help" |
| 179 |
,renderKey ("? ", "toggle this help") |
| 180 |
,renderKey ("p/m/i", "while help is open:\nshow manual in pager/man/info") |
| 181 |
,str " " |
| 182 |
] |
| 183 |
,padLeft (Pad 1) $ padRight (Pad 0) $ |
| 184 |
vBox [ |
| 185 |
withAttr (attrName "help" <> attrName "heading") $ str "Filtering" |
| 186 |
,renderKey ("/ ", "set a filter query") |
| 187 |
,renderKey ("F ", "show future & forecast txns") |
| 188 |
,renderKey ("R ", "show real/all postings") |
| 189 |
,renderKey ("z ", "show nonzero/all amounts") |
| 190 |
,renderKey ("U/P/C ", "show unmarked/pending/cleared") |
| 191 |
,renderKey ("S-DOWN /S-UP ", "shrink/grow period") |
| 192 |
,renderKey ("S-RIGHT/S-LEFT", "next/previous period") |
| 193 |
,renderKey ("T ", "set period to today") |
| 194 |
,renderKey ("DEL ", "reset filters") |
| 195 |
,str " " |
| 196 |
,withAttr (attrName "help" <> attrName "heading") $ str "Other" |
| 197 |
,renderKey ("a ", "add transaction (hledger add)") |
| 198 |
,renderKey ("A ", "add transaction (hledger-iadd)") |
| 199 |
,renderKey ("B ", "show amounts/costs") |
| 200 |
,renderKey ("E ", "open editor") |
| 201 |
,renderKey ("I ", "toggle balance assertions") |
| 202 |
,renderKey ("V ", "show amounts/market values") |
| 203 |
,renderKey ("g ", "reload data") |
| 204 |
,renderKey ("C-l ", "redraw & recenter") |
| 205 |
,renderKey ("C-z ", "suspend") |
| 206 |
,renderKey ("q ", "quit") |
| 207 |
] |
| 208 |
] |
| 209 |
-- ,vBox [ |
| 210 |
-- str " " |
| 211 |
-- ,hCenter $ padLeftRight 1 $ |
| 212 |
-- hCenter (str "MANUAL") |
| 213 |
-- <=> |
| 214 |
-- hCenter (hBox [ |
| 215 |
-- renderKey ("t", "text") |
| 216 |
-- ,str " " |
| 217 |
-- ,renderKey ("m", "man page") |
| 218 |
-- ,str " " |
| 219 |
-- ,renderKey ("i", "info") |
| 220 |
-- ]) |
| 221 |
-- ] |
| 222 |
] |
| 223 |
where |
| 224 |
renderKey (key,desc) = withAttr (attrName "help" <> attrName "key") (str key) <+> str " " <+> str desc |
| 225 |
|
| 226 |
-- | Event handler used when help mode is active. |
| 227 |
-- May invoke $PAGER, less, man or info, which is likely to fail on MS Windows, TODO. |
| 228 |
helpHandle :: BrickEvent Name AppEvent -> EventM Name UIState () |
| 229 |
helpHandle ev = do |
| 230 |
ui <- get |
| 231 |
let ui' = ui{aMode=Normal} |
| 232 |
case ev of |
| 233 |
VtyEvent e | e `elem` closeHelpEvents -> put' ui' |
| 234 |
VtyEvent (EvKey (KChar 'p') []) -> suspendAndResume (runPagerForTopic "hledger-ui" Nothing >> return ui') |
| 235 |
VtyEvent (EvKey (KChar 'm') []) -> suspendAndResume (runManForTopic "hledger-ui" Nothing >> return ui') |
| 236 |
VtyEvent (EvKey (KChar 'i') []) -> suspendAndResume (runInfoForTopic "hledger-ui" Nothing >> return ui') |
| 237 |
_ -> return () |
| 238 |
where |
| 239 |
closeHelpEvents = moveLeftEvents ++ [EvKey KEsc [], EvKey (KChar '?') [], EvKey (KChar 'q') []] |
| 240 |
|
| 241 |
-- | Draw the minibuffer with the given label. |
| 242 |
minibuffer :: T.Text -> Editor String Name -> Widget Name |
| 243 |
minibuffer string ed = |
| 244 |
forceAttr (attrName "border" <> attrName "minibuffer") $ |
| 245 |
hBox [txt $ string <> ": ", renderEditor (str . unlines) True ed] |
| 246 |
|
| 247 |
borderQueryStr :: String -> Widget Name |
| 248 |
borderQueryStr "" = str "" |
| 249 |
borderQueryStr qry = str " matching " <+> withAttr (attrName "border" <> attrName "query") (str qry) |
| 250 |
|
| 251 |
borderDepthStr :: Maybe Int -> Widget Name |
| 252 |
borderDepthStr Nothing = str "" |
| 253 |
borderDepthStr (Just d) = str " to depth " <+> withAttr (attrName "border" <> attrName "query") (str $ show d) |
| 254 |
|
| 255 |
borderPeriodStr :: String -> Period -> Widget Name |
| 256 |
borderPeriodStr _ PeriodAll = str "" |
| 257 |
borderPeriodStr preposition p = str (" "++preposition++" ") <+> withAttr (attrName "border" <> attrName "query") (str . T.unpack $ showPeriod p) |
| 258 |
|
| 259 |
borderKeysStr :: [(String,String)] -> Widget Name |
| 260 |
borderKeysStr = borderKeysStr' . map (second str) |
| 261 |
|
| 262 |
borderKeysStr' :: [(String,Widget Name)] -> Widget Name |
| 263 |
borderKeysStr' keydescs = |
| 264 |
hBox $ |
| 265 |
intersperse sep $ |
| 266 |
[withAttr (attrName "border" <> attrName "key") (str keys) <+> str ":" <+> desc | (keys, desc) <- keydescs] |
| 267 |
where |
| 268 |
-- sep = str " | " |
| 269 |
sep = str " " |
| 270 |
|
| 271 |
-- | Show both states of a toggle ("aaa/bbb"), highlighting the active one. |
| 272 |
renderToggle :: Bool -> String -> String -> Widget Name |
| 273 |
renderToggle isright l r = |
| 274 |
let bold = withAttr (attrName "border" <> attrName "selected") in |
| 275 |
if isright |
| 276 |
then str (l++"/") <+> bold (str r) |
| 277 |
else bold (str l) <+> str ("/"++r) |
| 278 |
|
| 279 |
-- | Show a toggle's label, highlighted (bold) when the toggle is active. |
| 280 |
renderToggle1 :: Bool -> String -> Widget Name |
| 281 |
renderToggle1 isactive l = |
| 282 |
let bold = withAttr (attrName "border" <> attrName "selected") in |
| 283 |
if isactive |
| 284 |
then bold (str l) |
| 285 |
else str l |
| 286 |
|
| 287 |
-- temporary shenanigans: |
| 288 |
|
| 289 |
-- | Replace the special account names "*" and "..." (from balance reports with depth limit 0) |
| 290 |
-- to something clearer. |
| 291 |
replaceHiddenAccountsNameWith :: AccountName -> AccountName -> AccountName |
| 292 |
replaceHiddenAccountsNameWith anew a | a == hiddenAccountsName = anew |
| 293 |
| a == "*" = anew |
| 294 |
| otherwise = a |
| 295 |
|
| 296 |
hiddenAccountsName = "..." -- for now |
| 297 |
|
| 298 |
-- generic |
| 299 |
|
| 300 |
--topBottomBorderWithLabel :: Widget Name -> Widget Name -> Widget Name |
| 301 |
--topBottomBorderWithLabel label = \wrapped -> |
| 302 |
-- Widget Greedy Greedy $ do |
| 303 |
-- c <- getContext |
| 304 |
-- let (_w,h) = (c^.availWidthL, c^.availHeightL) |
| 305 |
-- h' = h - 2 |
| 306 |
-- wrapped' = vLimit (h') wrapped |
| 307 |
-- debugmsg = |
| 308 |
-- "" |
| 309 |
-- -- " debug: "++show (_w,h') |
| 310 |
-- render $ |
| 311 |
-- hBorderWithLabel (label <+> str debugmsg) |
| 312 |
-- <=> |
| 313 |
-- wrapped' |
| 314 |
-- <=> |
| 315 |
-- hBorder |
| 316 |
|
| 317 |
topBottomBorderWithLabels :: Widget Name -> Widget Name -> Widget Name -> Widget Name |
| 318 |
topBottomBorderWithLabels toplabel bottomlabel body = |
| 319 |
Widget Greedy Greedy $ do |
| 320 |
c <- getContext |
| 321 |
let (_w,h) = (c^.availWidthL, c^.availHeightL) |
| 322 |
h' = h - 2 |
| 323 |
body' = vLimit (h') body |
| 324 |
debugmsg = |
| 325 |
"" |
| 326 |
-- " debug: "++show (_w,h') |
| 327 |
render $ |
| 328 |
hBorderWithLabel (withAttr (attrName "border") $ toplabel <+> str debugmsg) |
| 329 |
<=> |
| 330 |
body' |
| 331 |
<=> |
| 332 |
hBorderWithLabel (withAttr (attrName "border") bottomlabel) |
| 333 |
|
| 334 |
---- XXX should be equivalent to the above, but isn't (page down goes offscreen) |
| 335 |
--_topBottomBorderWithLabel :: Widget Name -> Widget Name -> Widget Name |
| 336 |
--_topBottomBorderWithLabel label = \wrapped -> |
| 337 |
-- let debugmsg = "" |
| 338 |
-- in hBorderWithLabel (label <+> str debugmsg) |
| 339 |
-- <=> |
| 340 |
-- wrapped |
| 341 |
-- <=> |
| 342 |
-- hBorder |
| 343 |
|
| 344 |
-- XXX superseded by pad, in theory |
| 345 |
-- | Wrap a widget in a margin with the given horizontal and vertical |
| 346 |
-- thickness, using the current background colour or the specified |
| 347 |
-- colour. |
| 348 |
-- XXX May disrupt border style of inner widgets. |
| 349 |
-- XXX Should reduce the available size visible to inner widget, but doesn't seem to (cf rsDraw). |
| 350 |
margin :: Int -> Int -> Maybe Color -> Widget Name -> Widget Name |
| 351 |
margin h v mcolour w = Widget Greedy Greedy $ do |
| 352 |
ctx <- getContext |
| 353 |
let w' = vLimit (ctx^.availHeightL - v*2) $ hLimit (ctx^.availWidthL - h*2) w |
| 354 |
attr = maybe currentAttr (\c -> c `on` c) mcolour |
| 355 |
render $ |
| 356 |
withBorderAttr attr $ |
| 357 |
withBorderStyle (borderStyleFromChar ' ') $ |
| 358 |
applyN v (hBorder <=>) $ |
| 359 |
applyN h (vBorder <+>) $ |
| 360 |
applyN v (<=> hBorder) $ |
| 361 |
applyN h (<+> vBorder) $ |
| 362 |
w' |
| 363 |
|
| 364 |
-- withBorderAttr attr . |
| 365 |
-- withBorderStyle (borderStyleFromChar ' ') . |
| 366 |
-- applyN n border |
| 367 |
|
| 368 |
withBorderAttr :: Attr -> Widget Name -> Widget Name |
| 369 |
withBorderAttr attr = updateAttrMap (applyAttrMappings [(attrName "border", attr)]) |
| 370 |
|
| 371 |
---- | Like brick's continue, but first run some action to modify brick's state. |
| 372 |
---- This action does not affect the app state, but might eg adjust a widget's scroll position. |
| 373 |
--continueWith :: EventM n () -> ui -> EventM n (Next ui) |
| 374 |
--continueWith brickaction ui = brickaction >> continue ui |
| 375 |
|
| 376 |
---- | Scroll a list's viewport so that the selected item is at the top |
| 377 |
---- of the display area. |
| 378 |
--scrollToTop :: List Name e -> EventM Name () |
| 379 |
--scrollToTop list = do |
| 380 |
-- let vpname = list^.listNameL |
| 381 |
-- setTop (viewportScroll vpname) 0 |
| 382 |
|
| 383 |
-- | Scroll a list's viewport so that the selected item is centered in the |
| 384 |
-- middle of the display area. |
| 385 |
scrollSelectionToMiddle :: Brick.Widgets.List.List Name item -> EventM Name UIState () |
| 386 |
scrollSelectionToMiddle list = do |
| 387 |
case list^.listSelectedL of |
| 388 |
Nothing -> return () |
| 389 |
Just selectedrow -> do |
| 390 |
Vty{outputIface} <- getVtyHandle |
| 391 |
pageheight <- dbg4 "pageheight" . snd <$> liftIO (displayBounds outputIface) |
| 392 |
let |
| 393 |
itemheight = dbg4 "itemheight" $ list^.listItemHeightL |
| 394 |
itemsperpage = dbg4 "itemsperpage" $ pageheight `div` itemheight |
| 395 |
toprow = dbg4 "toprow" $ max 0 (selectedrow - (itemsperpage `div` 2)) -- assuming ViewportScroll's row offset is measured in list items not screen rows |
| 396 |
setTop (viewportScroll $ list^.listNameL) toprow |
| 397 |
|
| 398 |
-- arrow keys vi keys emacs keys enter key |
| 399 |
moveUpEvents = [EvKey KUp [] , EvKey (KChar 'k') [], EvKey (KChar 'p') [MCtrl]] |
| 400 |
moveDownEvents = [EvKey KDown [] , EvKey (KChar 'j') [], EvKey (KChar 'n') [MCtrl]] |
| 401 |
moveLeftEvents = [EvKey KLeft [] , EvKey (KChar 'h') [], EvKey (KChar 'b') [MCtrl]] |
| 402 |
moveRightEvents = [EvKey KRight [], EvKey (KChar 'l') [], EvKey (KChar 'f') [MCtrl], EvKey KEnter []] |
| 403 |
|
| 404 |
normaliseMovementKeys ev |
| 405 |
| ev `elem` moveUpEvents = EvKey KUp [] |
| 406 |
| ev `elem` moveDownEvents = EvKey KDown [] |
| 407 |
| ev `elem` moveLeftEvents = EvKey KLeft [] |
| 408 |
| ev `elem` moveRightEvents = EvKey KRight [] |
| 409 |
| otherwise = ev |
| 410 |
|
| 411 |
-- | Restrict the ReportSpec's query by adding the given additional query. |
| 412 |
reportSpecAddQuery :: Query -> ReportSpec -> ReportSpec |
| 413 |
reportSpecAddQuery q rspec = |
| 414 |
rspec{_rsQuery=simplifyQuery $ And [_rsQuery rspec, q]} |
| 415 |
|
| 416 |
-- | Update the ReportSpec's query to exclude future transactions (later than its "today" date) |
| 417 |
-- and forecast transactions (generated by --forecast), if the given forecast DateSpan is Nothing, |
| 418 |
-- and include them otherwise. |
| 419 |
reportSpecSetFutureAndForecast :: Maybe DateSpan -> ReportSpec -> ReportSpec |
| 420 |
reportSpecSetFutureAndForecast fcast rspec = |
| 421 |
rspec{_rsQuery=simplifyQuery $ And [_rsQuery rspec, periodq, excludeforecastq fcast]} |
| 422 |
where |
| 423 |
periodq = Date . periodAsDateSpan . period_ $ _rsReportOpts rspec |
| 424 |
-- Except in forecast mode, exclude future/forecast transactions. |
| 425 |
excludeforecastq (Just _) = Any |
| 426 |
excludeforecastq Nothing = -- not:date:tomorrow- not:tag:generated-transaction |
| 427 |
And [ |
| 428 |
Not (Date $ DateSpan (Just $ Exact $ addDays 1 $ _rsDay rspec) Nothing) |
| 429 |
,Not generatedTransactionTag |
| 430 |
] |
| 431 |
|
| 432 |
-- Vertically scroll the named list's viewport with the given number of non-empty items |
| 433 |
-- by the given positive or negative number of items (usually 1 or -1). |
| 434 |
-- The selection will be moved when necessary to keep it visible and allow the scroll. |
| 435 |
listScrollPushingSelection :: Name -> Int -> Int -> EventM Name (Brick.Widgets.List.List Name item) (GenericList Name Vector item) |
| 436 |
listScrollPushingSelection name listheight scrollamt = do |
| 437 |
list <- get |
| 438 |
viewportScroll name `vScrollBy` scrollamt |
| 439 |
mvp <- lookupViewport name |
| 440 |
case mvp of |
| 441 |
Just VP{_vpTop, _vpSize=(_,vpheight)} -> do |
| 442 |
let mselidx = listSelected list |
| 443 |
case mselidx of |
| 444 |
Just selidx -> return $ pushsel list |
| 445 |
where |
| 446 |
pushsel |
| 447 |
| scrollamt > 0, selidx <= _vpTop && selidx < (listheight-1) = listMoveDown |
| 448 |
| scrollamt < 0, selidx >= _vpTop + vpheight - 1 && selidx > 0 = listMoveUp |
| 449 |
| otherwise = id |
| 450 |
_ -> return list |
| 451 |
_ -> return list |
| 452 |
|
| 453 |
-- | A debug logging helper for hledger-ui code: at any debug level >= 1, |
| 454 |
-- logs the string to hledger-ui.log before returning the second argument. |
| 455 |
-- Uses unsafePerformIO. |
| 456 |
dbgui :: String -> a -> a |
| 457 |
dbgui = dbg1Msg |
| 458 |
|
| 459 |
-- | Like dbgui, but convenient to use in IO. |
| 460 |
dbguiIO :: String -> IO () |
| 461 |
dbguiIO = dbg1MsgIO |
| 462 |
|
| 463 |
-- | Like dbgui, but convenient to use in EventM handlers. |
| 464 |
dbguiEv :: String -> EventM Name s () |
| 465 |
dbguiEv s = dbg1Msg s $ return () |
| 466 |
|
| 467 |
-- | Like dbguiEv, but log a compact view of the current screen stack. |
| 468 |
-- See showScreenStack. |
| 469 |
-- To just log the stack: @dbguiScreensEv "" showScreenId ui@ |
| 470 |
dbguiScreensEv :: String -> (Screen -> String) -> UIState -> EventM Name UIState () |
| 471 |
dbguiScreensEv postfix showscr ui = dbguiEv $ showScreenStack postfix showscr ui |
| 472 |
|
| 473 |
-- Render a compact labelled view of the current screen stack, |
| 474 |
-- adding the given postfix to the label (can be empty), |
| 475 |
-- from the topmost screen to the currently-viewed screen, |
| 476 |
-- with each screen rendered by the given rendering function. |
| 477 |
-- Useful for inspecting states across the whole screen stack. |
| 478 |
-- Some screen rendering functions are |
| 479 |
-- @showScreenId@, @showScreenSelection@, @showScreenRegisterDescriptions@. |
| 480 |
-- |
| 481 |
-- Eg to just show the stack: @showScreenStack "" showScreenId ui@ |
| 482 |
-- |
| 483 |
-- To to show the stack plus selected item indexes: @showScreenStack "" showScreenSelection ui@ |
| 484 |
-- |
| 485 |
showScreenStack :: String -> (Screen -> String) -> UIState -> String |
| 486 |
showScreenStack postfix showscr ui = concat [ |
| 487 |
"screen stack" |
| 488 |
,if null postfix then "" else ", " ++ postfix |
| 489 |
,": " |
| 490 |
,unwords $ mapScreens showscr ui |
| 491 |
] |
| 492 |
|
| 493 |
-- | Run a function on each screen in a UIState's screen "stack", |
| 494 |
-- from topmost screen down to currently-viewed screen. |
| 495 |
mapScreens :: (Screen -> a) -> UIState -> [a] |
| 496 |
mapScreens f UIState{aPrevScreens, aScreen} = map f $ reverse $ aScreen : aPrevScreens |
| 497 |
|
| 498 |
-- Show a screen's compact id (first letter of its constructor). |
| 499 |
showScreenId :: Screen -> String |
| 500 |
showScreenId = \case |
| 501 |
MS _ -> "M" -- menu |
| 502 |
AS _ -> "A" -- all accounts |
| 503 |
CS _ -> "C" -- cash accounts |
| 504 |
BS _ -> "B" -- bs accounts |
| 505 |
IS _ -> "I" -- is accounts |
| 506 |
RS _ -> "R" -- menu |
| 507 |
TS _ -> "T" -- transaction |
| 508 |
ES _ -> "E" -- error |
| 509 |
|
| 510 |
-- Show a screen's compact id, plus for register screens, the transaction descriptions. |
| 511 |
showScreenRegisterDescriptions :: Screen -> String |
| 512 |
showScreenRegisterDescriptions scr = case scr of |
| 513 |
RS sst -> ((showScreenId scr ++ ":") ++) $ -- menu |
| 514 |
intercalate "," $ map (T.unpack . rsItemDescription) $ |
| 515 |
takeWhile (not . T.null . rsItemDate) $ V.toList $ listElements $ _rssList sst |
| 516 |
_ -> showScreenId scr |
| 517 |
|
| 518 |
-- Show a screen's compact id, plus index of its selected list item if any. |
| 519 |
showScreenSelection :: Screen -> String |
| 520 |
showScreenSelection = \case |
| 521 |
MS MSS{_mssList} -> "M" ++ (maybe "" show $ listSelected _mssList) -- menu |
| 522 |
AS ASS{_assList} -> "A" ++ (maybe "" show $ listSelected _assList) -- all accounts |
| 523 |
CS ASS{_assList} -> "C" ++ (maybe "" show $ listSelected _assList) -- cash accounts |
| 524 |
BS ASS{_assList} -> "B" ++ (maybe "" show $ listSelected _assList) -- bs accounts |
| 525 |
IS ASS{_assList} -> "I" ++ (maybe "" show $ listSelected _assList) -- is accounts |
| 526 |
RS RSS{_rssList} -> "R" ++ (maybe "" show $ listSelected _rssList) -- menu |
| 527 |
TS _ -> "T" -- transaction |
| 528 |
ES _ -> "E" -- error |
| 529 |
|
| 530 |
-- | How many blank items to add to lists to fill the full window height. |
| 531 |
uiNumBlankItems :: Int |
| 532 |
uiNumBlankItems |
| 533 |
-- | debugLevel >= uiDebugLevel = 0 -- suppress to improve debug output. |
| 534 |
-- | otherwise |
| 535 |
= 100 -- 100 ought to be enough for anyone |
| 536 |
|
| 537 |
-- Send some events to vty, atomically so they won't have other events interleaved. |
| 538 |
-- (But there may be events already in the channel ahead of them.) |
| 539 |
sendVtyEvents :: [Event] -> EventM n s () |
| 540 |
sendVtyEvents evs = do |
| 541 |
input <- eventChannel . inputIface <$> getVtyHandle |
| 542 |
liftIO $ atomically $ mapM_ (writeTChan input . InputEvent) evs |