{- | Rendering & misc. helpers. -}

{-# LANGUAGE CPP               #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE LambdaCase #-}

module Hledger.UI.UIUtils (
   borderDepthStr
  ,borderKeysStr
  ,borderKeysStr'
  ,borderPeriodStr
  ,borderQueryStr
  ,clickFocusStep
  ,defaultLayout
  ,helpDialog
  ,helpHandle
  ,minibuffer
  ,moveDownEvents
  ,moveLeftEvents
  ,moveRightEvents
  ,moveUpEvents
  ,normaliseMovementKeys
  ,renderToggle
  ,renderToggle1
  ,replaceHiddenAccountsNameWith
  ,scrollSelectionToMiddle
  ,get'
  ,put'
  ,modify'
  ,suspend
  ,redraw
  ,uiInstallWarningCollector
  ,uiTakeWarnings
  ,journalIsFromStdin
  ,reportSpecAddQuery
  ,reportSpecSetFutureAndForecast
  ,listScrollPushingSelection
  ,dbgui
  ,dbguiIO
  ,dbguiEv
  ,dbguiScreensEv
  ,showScreenId
  ,showScreenRegisterDescriptions
  ,showScreenSelection
  ,mapScreens
  ,uiNumBlankItems
  ,showScreenStack
  ,sendVtyEvents  -- currently unused, kept (and exported, to avoid an unused-binding warning) for future use
  )
where

import Brick
import Brick.Widgets.Border
import Brick.Widgets.Border.Style
import Brick.Widgets.Dialog
import Brick.Widgets.Edit
import Brick.Widgets.List (List, listSelectedL, listNameL, listItemHeightL, listSelected, listMoveTo, listElements)
import Control.Concurrent.STM (atomically, writeTChan)  -- GHC only
import Control.Monad.IO.Class
import Data.Bifunctor (second)
import Data.IORef (IORef, newIORef, atomicModifyIORef')
import Data.List
import Data.Text qualified as T
import Data.Time (NominalDiffTime, UTCTime, addDays, diffUTCTime)
import Graphics.Vty
  (Event(..),Key(..),Modifier(..),Button(..),Vty(..),Color,Attr,currentAttr,refresh, displayBounds
  -- ,Output(displayBounds,mkDisplayContext),DisplayContext(..)
  ,Vty (inputIface), InternalEvent (InputEvent), Input (eventChannel)
  )
import Lens.Micro.Platform
import System.IO.Unsafe (unsafePerformIO)

import Hledger
-- import Hledger.Cli.CliOptions (CliOpts(reportspec_))
import Hledger.Cli.DocFiles
-- import Hledger.UI.UIOptions (UIOpts(uoCliOpts))
import Hledger.UI.UITypes

import Data.Vector qualified as V

-- | On posix platforms, send the system STOP signal to suspend the
-- current program. On windows, does nothing.
-- (Though, currently hledger-ui is not built on windows.)
#ifdef mingw32_HOST_OS
suspendSignal :: IO ()
suspendSignal = return ()
#else
import System.Posix.Signals
suspendSignal :: IO ()
suspendSignal :: IO ()
suspendSignal = Signal -> IO ()
raiseSignal Signal
sigSTOP
#endif

-- Debug logging for UI state changes.
-- A good place to log things of interest while debugging, see commented examples below.

get' :: EventM Name UIState UIState
get' = do
  ui <- EventM Name UIState UIState
forall s (m :: * -> *). MonadState s m => m s
get
  dbguiEv $ "getting state: " ++ 
    showScreenStack "" showScreenSelection ui
    -- (head $ lines $ pshow $ aScreen x)
    -- ++ " " ++ (show $ map tdescription $ jtxns $ ajournal x)
  -- dbguiEv $ ("query: "++) $ pshow' $ x  & aopts & uoCliOpts & reportspec_ & _rsQuery
  -- dbguiScreensEv "getting" showScreenId x
  -- dbguiScreensEv "getting, with register descriptions" showScreenRegisterDescriptions x
  return ui

put' :: UIState -> EventM Name UIState ()
put' UIState
ui = do
  [Char] -> EventM Name UIState ()
forall s. [Char] -> EventM Name s ()
dbguiEv ([Char] -> EventM Name UIState ())
-> [Char] -> EventM Name UIState ()
forall a b. (a -> b) -> a -> b
$ [Char]
"putting state: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++
    [Char] -> (Screen -> [Char]) -> UIState -> [Char]
showScreenStack [Char]
"" Screen -> [Char]
showScreenSelection UIState
ui
    -- (head $ lines $ pshow $ aScreen x)
    -- ++ " " ++ (show $ map tdescription $ jtxns $ ajournal x)
  -- dbguiEv $ ("query: "++) $ pshow' $ x  & aopts & uoCliOpts & reportspec_ & _rsQuery
  -- dbguiScreensEv "putting" showScreenId x
  -- dbguiScreensEv "putting, with register descriptions" showScreenRegisterDescriptions x
  UIState -> EventM Name UIState ()
forall s (m :: * -> *). MonadState s m => s -> m ()
put UIState
ui

modify' :: (UIState -> UIState) -> EventM Name UIState ()
modify' UIState -> UIState
f = do
  ui <- EventM Name UIState UIState
forall s (m :: * -> *). MonadState s m => m s
get
  let ui' = UIState -> UIState
f UIState
ui
  dbguiEv $ "getting state: " ++ (showScreenStack "" showScreenSelection ui)
  dbguiEv $ "putting state: " ++ (showScreenStack "" showScreenSelection ui')
    -- (head $ lines $ pshow $ aScreen x')
    -- ++ " " ++ (show $ map tdescription $ jtxns $ ajournal x')
  -- dbguiEv $ ("from: "++) $ pshow' $ x  & aopts & uoCliOpts & reportspec_ & _rsQuery
  -- dbguiEv $ ("to:   "++) $ pshow' $ x' & aopts & uoCliOpts & reportspec_ & _rsQuery
  -- dbguiScreensEv "getting" showScreenId x
  -- dbguiScreensEv "putting" showScreenId x'
  -- dbguiScreensEv "getting, with register descriptions" showScreenRegisterDescriptions x
  -- dbguiScreensEv "putting, with register descriptions" showScreenRegisterDescriptions x'
  modify f

-- | On posix platforms, suspend the program using the STOP signal,
-- like control-z in bash, returning to the original shell prompt,
-- and when resumed, continue where we left off.
-- On windows, does nothing.
suspend :: Ord a => s -> EventM a s ()
suspend :: forall a s. Ord a => s -> EventM a s ()
suspend s
st = IO s -> EventM a s ()
forall n s. Ord n => IO s -> EventM n s ()
suspendAndResume (IO s -> EventM a s ()) -> IO s -> EventM a s ()
forall a b. (a -> b) -> a -> b
$ IO ()
suspendSignal IO () -> IO s -> IO s
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> s -> IO s
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return s
st

-- | Tell vty to redraw the whole screen.
redraw :: EventM a s ()
redraw :: forall a s. EventM a s ()
redraw = EventM a s Vty
forall n s. EventM n s Vty
getVtyHandle EventM a s Vty -> (Vty -> EventM a s ()) -> EventM a s ()
forall a b. EventM a s a -> (a -> EventM a s b) -> EventM a s b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= IO () -> EventM a s ()
forall a. IO a -> EventM a s a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> EventM a s ()) -> (Vty -> IO ()) -> Vty -> EventM a s ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vty -> IO ()
refresh

-- | Where warnings emitted while the TUI is running are collected, oldest first,
-- so they can be shown in the UI instead of being printed to stderr
-- (which would disrupt the terminal display). See uiInstallWarningCollector.
{-# NOINLINE uiWarningsRef #-}
uiWarningsRef :: IORef [String]
uiWarningsRef :: IORef [[Char]]
uiWarningsRef = IO (IORef [[Char]]) -> IORef [[Char]]
forall a. IO a -> a
unsafePerformIO (IO (IORef [[Char]]) -> IORef [[Char]])
-> IO (IORef [[Char]]) -> IORef [[Char]]
forall a b. (a -> b) -> a -> b
$ [[Char]] -> IO (IORef [[Char]])
forall a. a -> IO (IORef a)
newIORef []

-- | Make warnIO collect warnings in uiWarningsRef instead of printing them to stderr.
-- Call once, before starting the brick app.
uiInstallWarningCollector :: IO ()
uiInstallWarningCollector :: IO ()
uiInstallWarningCollector = ([Char] -> IO ()) -> IO ()
setWarningHandler (([Char] -> IO ()) -> IO ()) -> ([Char] -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \[Char]
msg ->
  IORef [[Char]] -> ([[Char]] -> ([[Char]], ())) -> IO ()
forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef' IORef [[Char]]
uiWarningsRef (([[Char]] -> ([[Char]], ())) -> IO ())
-> ([[Char]] -> ([[Char]], ())) -> IO ()
forall a b. (a -> b) -> a -> b
$ \[[Char]]
ws -> ([[Char]]
ws [[Char]] -> [[Char]] -> [[Char]]
forall a. [a] -> [a] -> [a]
++ [[Char]
msg], ())

-- | Take any warnings collected since last time, clearing the collection.
uiTakeWarnings :: IO [String]
uiTakeWarnings :: IO [[Char]]
uiTakeWarnings = IORef [[Char]] -> ([[Char]] -> ([[Char]], [[Char]])) -> IO [[Char]]
forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef' IORef [[Char]]
uiWarningsRef (([[Char]] -> ([[Char]], [[Char]])) -> IO [[Char]])
-> ([[Char]] -> ([[Char]], [[Char]])) -> IO [[Char]]
forall a b. (a -> b) -> a -> b
$ \[[Char]]
ws -> ([], [[Char]]
ws)

-- | Wrap a widget in the default hledger-ui screen layout.
defaultLayout :: Widget Name -> Widget Name -> Widget Name -> Widget Name
defaultLayout :: Widget Name -> Widget Name -> Widget Name -> Widget Name
defaultLayout Widget Name
toplabel Widget Name
bottomlabel =
  Widget Name -> Widget Name -> Widget Name -> Widget Name
topBottomBorderWithLabels ([Char] -> Widget Name
forall n. [Char] -> Widget n
str [Char]
" "Widget Name -> Widget Name -> Widget Name
forall n. Widget n -> Widget n -> Widget n
<+>Widget Name
toplabelWidget Name -> Widget Name -> Widget Name
forall n. Widget n -> Widget n -> Widget n
<+>[Char] -> Widget Name
forall n. [Char] -> Widget n
str [Char]
" ") ([Char] -> Widget Name
forall n. [Char] -> Widget n
str [Char]
" "Widget Name -> Widget Name -> Widget Name
forall n. Widget n -> Widget n -> Widget n
<+>Widget Name
bottomlabelWidget Name -> Widget Name -> Widget Name
forall n. Widget n -> Widget n -> Widget n
<+>[Char] -> Widget Name
forall n. [Char] -> Widget n
str [Char]
" ") (Widget Name -> Widget Name)
-> (Widget Name -> Widget Name) -> Widget Name -> Widget Name
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  Int -> Int -> Maybe Color -> Widget Name -> Widget Name
margin Int
1 Int
0 Maybe Color
forall a. Maybe a
Nothing
  -- topBottomBorderWithLabel label .
  -- padLeftRight 1 -- XXX should reduce inner widget's width by 2, but doesn't
                    -- "the layout adjusts... if you use the core combinators"

-- | Draw the help dialog, called when help mode is active.
helpDialog :: Widget Name
helpDialog :: Widget Name
helpDialog =
  Size -> Size -> RenderM Name (Result Name) -> Widget Name
forall n. Size -> Size -> RenderM n (Result n) -> Widget n
Widget Size
Fixed Size
Fixed (RenderM Name (Result Name) -> Widget Name)
-> RenderM Name (Result Name) -> Widget Name
forall a b. (a -> b) -> a -> b
$ do
    c <- RenderM Name (Context Name)
forall n. RenderM n (Context n)
getContext
    render $
      withDefAttr (attrName "help") $
      renderDialog (dialog (Just $ str "Help (LEFT/ESC/?/q to close help)") Nothing (c^.availWidthL)) $ -- (Just (0,[("ok",())]))
      padTop (Pad 0) $ padLeft (Pad 1) $ padRight (Pad 1) $
        vBox [
           hBox [
              padRight (Pad 1) $
                vBox [
                   withAttr (attrName "help" <> attrName "heading") $ str "Navigation"
                  ,renderKey ("UP/DOWN/PUP/PDN/HOME/END/k/j/C-p/C-n", "")
                  ,str "     move selection up/down"
                  ,renderKey ("RIGHT/l/C-f", "show txns, or txn detail")
                  ,renderKey ("LEFT/h/C-b ", "go back/see other screens")
                  ,renderKey ("ESC ", "cancel, or reset app state")

                  ,str " "
                  ,withAttr (attrName "help" <> attrName "heading") $ str "Accounts screens"
                  ,renderKey ("1234567890-+ ", "set/adjust depth limit")
                  ,renderKey ("t ", "toggle accounts tree/list mode")
                  ,renderKey ("H ", "toggle historical balance/change")
                  ,str " "
                  ,withAttr (attrName "help" <> attrName "heading") $ str "Register screens"
                  ,renderKey ("t ", "toggle subaccount txns\n(and accounts tree/list mode)")
                  ,renderKey ("H ", "toggle historical/period total")
                  ,str " "
                  ,withAttr (attrName "help" <> attrName "heading") $ str "Help"
                  ,renderKey ("?    ", "toggle this help")
                  ,renderKey ("p/m/i", "while help is open:\nshow manual in pager/man/info")
                  ,str " "
                ]
             ,padLeft (Pad 1) $ padRight (Pad 0) $
                vBox [
                   withAttr (attrName "help" <> attrName "heading") $ str "Filtering"
                  ,renderKey ("/   ", "set a filter query")
                  ,renderKey ("F   ", "show future & forecast txns")
                  ,renderKey ("R   ", "show real/all postings")
                  ,renderKey ("z   ", "show nonzero/all amounts")
                  ,renderKey ("U/P/C ", "show unmarked/pending/cleared")
                  ,renderKey ("S-DOWN /S-UP  ", "shrink/grow period")
                  ,renderKey ("S-RIGHT/S-LEFT", "next/previous period")
                  ,renderKey ("T             ", "set period to today")
                  ,renderKey ("DEL ", "reset filters")
                  ,str " "
                  ,withAttr (attrName "help" <> attrName "heading") $ str "Other"
                  ,renderKey ("a   ", "add transaction (hledger add)")
                  ,renderKey ("A   ", "add transaction (hledger-iadd)")
                  ,renderKey ("B   ", "show amounts/costs")
                  ,renderKey ("E   ", "open editor")
                  ,renderKey ("I   ", "toggle balance assertions")
                  ,renderKey ("L   ", "show/hide lot detail")
                  ,renderKey ("V   ", "show amounts/market values")
                  ,renderKey ("g   ", "reload data")
                  ,renderKey ("C-l ", "redraw & recenter")
                  ,renderKey ("C-z ", "suspend")
                  ,renderKey ("q   ", "quit")
                  ,renderKey ("C-c ", "quit, from anywhere")
                ]
             ]
--           ,vBox [
--              str " "
--             ,hCenter $ padLeftRight 1 $
--               hCenter (str "MANUAL")
--               <=>
--               hCenter (hBox [
--                  renderKey ("t", "text")
--                 ,str " "
--                 ,renderKey ("m", "man page")
--                 ,str " "
--                 ,renderKey ("i", "info")
--                 ])
--             ]
          ]
  where
    renderKey :: ([Char], [Char]) -> Widget n
renderKey ([Char]
key,[Char]
desc) = AttrName -> Widget n -> Widget n
forall n. AttrName -> Widget n -> Widget n
withAttr ([Char] -> AttrName
attrName [Char]
"help" AttrName -> AttrName -> AttrName
forall a. Semigroup a => a -> a -> a
<> [Char] -> AttrName
attrName [Char]
"key") ([Char] -> Widget n
forall n. [Char] -> Widget n
str [Char]
key) Widget n -> Widget n -> Widget n
forall n. Widget n -> Widget n -> Widget n
<+> [Char] -> Widget n
forall n. [Char] -> Widget n
str [Char]
" " Widget n -> Widget n -> Widget n
forall n. Widget n -> Widget n -> Widget n
<+> [Char] -> Widget n
forall n. [Char] -> Widget n
str [Char]
desc

-- | Event handler used when help mode is active.
-- May invoke $PAGER, less, man or info, which is likely to fail on MS Windows, TODO.
helpHandle :: BrickEvent Name AppEvent -> EventM Name UIState ()
helpHandle :: BrickEvent Name AppEvent -> EventM Name UIState ()
helpHandle BrickEvent Name AppEvent
ev = do
  ui <- EventM Name UIState UIState
forall s (m :: * -> *). MonadState s m => m s
get
  let ui' = UIState
ui{aMode=Normal}
  case ev of
    VtyEvent Event
e | Event
e Event -> [Event] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Event]
closeHelpEvents -> UIState -> EventM Name UIState ()
put' UIState
ui'
    VtyEvent (EvKey (KChar Char
'p') []) -> IO UIState -> EventM Name UIState ()
forall n s. Ord n => IO s -> EventM n s ()
suspendAndResume ([Char] -> Maybe [Char] -> IO ()
runPagerForTopic [Char]
"hledger-ui" Maybe [Char]
forall a. Maybe a
Nothing IO () -> IO UIState -> IO UIState
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> UIState -> IO UIState
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return UIState
ui')
    VtyEvent (EvKey (KChar Char
'm') []) -> IO UIState -> EventM Name UIState ()
forall n s. Ord n => IO s -> EventM n s ()
suspendAndResume ([Char] -> Maybe [Char] -> IO ()
runManForTopic   [Char]
"hledger-ui" Maybe [Char]
forall a. Maybe a
Nothing IO () -> IO UIState -> IO UIState
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> UIState -> IO UIState
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return UIState
ui')
    VtyEvent (EvKey (KChar Char
'i') []) -> IO UIState -> EventM Name UIState ()
forall n s. Ord n => IO s -> EventM n s ()
suspendAndResume ([Char] -> Maybe [Char] -> IO ()
runInfoForTopic  [Char]
"hledger-ui" Maybe [Char]
forall a. Maybe a
Nothing IO () -> IO UIState -> IO UIState
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> UIState -> IO UIState
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return UIState
ui')
    BrickEvent Name AppEvent
_ -> () -> EventM Name UIState ()
forall a. a -> EventM Name UIState a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
  where
    closeHelpEvents :: [Event]
closeHelpEvents = [Event]
moveLeftEvents [Event] -> [Event] -> [Event]
forall a. [a] -> [a] -> [a]
++ [Key -> [Modifier] -> Event
EvKey Key
KEsc [], Key -> [Modifier] -> Event
EvKey (Char -> Key
KChar Char
'?') [], Key -> [Modifier] -> Event
EvKey (Char -> Key
KChar Char
'q') []]

-- | Draw the minibuffer with the given label.
minibuffer :: T.Text -> Editor String Name -> Widget Name
minibuffer :: AccountName -> Editor [Char] Name -> Widget Name
minibuffer AccountName
string Editor [Char] Name
ed =
  AttrName -> Widget Name -> Widget Name
forall n. AttrName -> Widget n -> Widget n
forceAttr ([Char] -> AttrName
attrName [Char]
"border" AttrName -> AttrName -> AttrName
forall a. Semigroup a => a -> a -> a
<> [Char] -> AttrName
attrName [Char]
"minibuffer") (Widget Name -> Widget Name) -> Widget Name -> Widget Name
forall a b. (a -> b) -> a -> b
$
  [Widget Name] -> Widget Name
forall n. [Widget n] -> Widget n
hBox [AccountName -> Widget Name
forall n. AccountName -> Widget n
txt (AccountName -> Widget Name) -> AccountName -> Widget Name
forall a b. (a -> b) -> a -> b
$ AccountName
string AccountName -> AccountName -> AccountName
forall a. Semigroup a => a -> a -> a
<> AccountName
": ", ([[Char]] -> Widget Name)
-> Bool -> Editor [Char] Name -> Widget Name
forall n t.
(Ord n, Show n, Monoid t, TextWidth t, GenericTextZipper t) =>
([t] -> Widget n) -> Bool -> Editor t n -> Widget n
renderEditor ([Char] -> Widget Name
forall n. [Char] -> Widget n
str ([Char] -> Widget Name)
-> ([[Char]] -> [Char]) -> [[Char]] -> Widget Name
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[Char]] -> [Char]
unlines) Bool
True Editor [Char] Name
ed]

borderQueryStr :: String -> Widget Name
borderQueryStr :: [Char] -> Widget Name
borderQueryStr [Char]
""  = [Char] -> Widget Name
forall n. [Char] -> Widget n
str [Char]
""
borderQueryStr [Char]
qry = [Char] -> Widget Name
forall n. [Char] -> Widget n
str [Char]
" matching " Widget Name -> Widget Name -> Widget Name
forall n. Widget n -> Widget n -> Widget n
<+> AttrName -> Widget Name -> Widget Name
forall n. AttrName -> Widget n -> Widget n
withAttr ([Char] -> AttrName
attrName [Char]
"border" AttrName -> AttrName -> AttrName
forall a. Semigroup a => a -> a -> a
<> [Char] -> AttrName
attrName [Char]
"query") ([Char] -> Widget Name
forall n. [Char] -> Widget n
str [Char]
qry)

borderDepthStr :: Maybe Int -> Widget Name
borderDepthStr :: Maybe Int -> Widget Name
borderDepthStr Maybe Int
Nothing  = [Char] -> Widget Name
forall n. [Char] -> Widget n
str [Char]
""
borderDepthStr (Just Int
d) = [Char] -> Widget Name
forall n. [Char] -> Widget n
str [Char]
" to depth " Widget Name -> Widget Name -> Widget Name
forall n. Widget n -> Widget n -> Widget n
<+> AttrName -> Widget Name -> Widget Name
forall n. AttrName -> Widget n -> Widget n
withAttr ([Char] -> AttrName
attrName [Char]
"border" AttrName -> AttrName -> AttrName
forall a. Semigroup a => a -> a -> a
<> [Char] -> AttrName
attrName [Char]
"query") ([Char] -> Widget Name
forall n. [Char] -> Widget n
str ([Char] -> Widget Name) -> [Char] -> Widget Name
forall a b. (a -> b) -> a -> b
$ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
d)

borderPeriodStr :: String -> Period -> Widget Name
borderPeriodStr :: [Char] -> Period -> Widget Name
borderPeriodStr [Char]
_           Period
PeriodAll = [Char] -> Widget Name
forall n. [Char] -> Widget n
str [Char]
""
borderPeriodStr [Char]
preposition Period
p         = [Char] -> Widget Name
forall n. [Char] -> Widget n
str ([Char]
" "[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++[Char]
preposition[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++[Char]
" ") Widget Name -> Widget Name -> Widget Name
forall n. Widget n -> Widget n -> Widget n
<+> AttrName -> Widget Name -> Widget Name
forall n. AttrName -> Widget n -> Widget n
withAttr ([Char] -> AttrName
attrName [Char]
"border" AttrName -> AttrName -> AttrName
forall a. Semigroup a => a -> a -> a
<> [Char] -> AttrName
attrName [Char]
"query") ([Char] -> Widget Name
forall n. [Char] -> Widget n
str ([Char] -> Widget Name)
-> (AccountName -> [Char]) -> AccountName -> Widget Name
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AccountName -> [Char]
T.unpack (AccountName -> Widget Name) -> AccountName -> Widget Name
forall a b. (a -> b) -> a -> b
$ Period -> AccountName
showPeriod Period
p)

borderKeysStr :: [(String,String)] -> Widget Name
borderKeysStr :: [([Char], [Char])] -> Widget Name
borderKeysStr = [([Char], Widget Name)] -> Widget Name
borderKeysStr' ([([Char], Widget Name)] -> Widget Name)
-> ([([Char], [Char])] -> [([Char], Widget Name)])
-> [([Char], [Char])]
-> Widget Name
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (([Char], [Char]) -> ([Char], Widget Name))
-> [([Char], [Char])] -> [([Char], Widget Name)]
forall a b. (a -> b) -> [a] -> [b]
map (([Char] -> Widget Name)
-> ([Char], [Char]) -> ([Char], Widget Name)
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second [Char] -> Widget Name
forall n. [Char] -> Widget n
str)

borderKeysStr' :: [(String,Widget Name)] -> Widget Name
borderKeysStr' :: [([Char], Widget Name)] -> Widget Name
borderKeysStr' [([Char], Widget Name)]
keydescs =
  [Widget Name] -> Widget Name
forall n. [Widget n] -> Widget n
hBox ([Widget Name] -> Widget Name) -> [Widget Name] -> Widget Name
forall a b. (a -> b) -> a -> b
$
  Widget Name -> [Widget Name] -> [Widget Name]
forall a. a -> [a] -> [a]
intersperse Widget Name
forall {n}. Widget n
sep ([Widget Name] -> [Widget Name]) -> [Widget Name] -> [Widget Name]
forall a b. (a -> b) -> a -> b
$
  [AttrName -> Widget Name -> Widget Name
forall n. AttrName -> Widget n -> Widget n
withAttr ([Char] -> AttrName
attrName [Char]
"border" AttrName -> AttrName -> AttrName
forall a. Semigroup a => a -> a -> a
<> [Char] -> AttrName
attrName [Char]
"key") ([Char] -> Widget Name
forall n. [Char] -> Widget n
str [Char]
keys) Widget Name -> Widget Name -> Widget Name
forall n. Widget n -> Widget n -> Widget n
<+> [Char] -> Widget Name
forall n. [Char] -> Widget n
str [Char]
":" Widget Name -> Widget Name -> Widget Name
forall n. Widget n -> Widget n -> Widget n
<+> Widget Name
desc | ([Char]
keys, Widget Name
desc) <- [([Char], Widget Name)]
keydescs]
  where
    -- sep = str " | "
    sep :: Widget n
sep = [Char] -> Widget n
forall n. [Char] -> Widget n
str [Char]
" "

-- | Show both states of a toggle ("aaa/bbb"), highlighting the active one.
renderToggle :: Bool -> String -> String -> Widget Name
renderToggle :: Bool -> [Char] -> [Char] -> Widget Name
renderToggle Bool
isright [Char]
l [Char]
r =
  let bold :: Widget n -> Widget n
bold = AttrName -> Widget n -> Widget n
forall n. AttrName -> Widget n -> Widget n
withAttr ([Char] -> AttrName
attrName [Char]
"border" AttrName -> AttrName -> AttrName
forall a. Semigroup a => a -> a -> a
<> [Char] -> AttrName
attrName [Char]
"selected") in
  if Bool
isright
  then [Char] -> Widget Name
forall n. [Char] -> Widget n
str ([Char]
l[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++[Char]
"/") Widget Name -> Widget Name -> Widget Name
forall n. Widget n -> Widget n -> Widget n
<+> Widget Name -> Widget Name
forall {n}. Widget n -> Widget n
bold ([Char] -> Widget Name
forall n. [Char] -> Widget n
str [Char]
r)
  else Widget Name -> Widget Name
forall {n}. Widget n -> Widget n
bold ([Char] -> Widget Name
forall n. [Char] -> Widget n
str [Char]
l) Widget Name -> Widget Name -> Widget Name
forall n. Widget n -> Widget n -> Widget n
<+> [Char] -> Widget Name
forall n. [Char] -> Widget n
str ([Char]
"/"[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++[Char]
r)

-- | Show a toggle's label, highlighted (bold) when the toggle is active.
renderToggle1 :: Bool -> String -> Widget Name
renderToggle1 :: Bool -> [Char] -> Widget Name
renderToggle1 Bool
isactive [Char]
l =
  let bold :: Widget n -> Widget n
bold = AttrName -> Widget n -> Widget n
forall n. AttrName -> Widget n -> Widget n
withAttr ([Char] -> AttrName
attrName [Char]
"border" AttrName -> AttrName -> AttrName
forall a. Semigroup a => a -> a -> a
<> [Char] -> AttrName
attrName [Char]
"selected") in
  if Bool
isactive
  then Widget Name -> Widget Name
forall {n}. Widget n -> Widget n
bold ([Char] -> Widget Name
forall n. [Char] -> Widget n
str [Char]
l)
  else [Char] -> Widget Name
forall n. [Char] -> Widget n
str [Char]
l

-- temporary shenanigans:

-- | Replace the special account names "*" and "..." (from balance reports with depth limit 0)
-- to something clearer.
replaceHiddenAccountsNameWith :: AccountName -> AccountName -> AccountName
replaceHiddenAccountsNameWith :: AccountName -> AccountName -> AccountName
replaceHiddenAccountsNameWith AccountName
anew AccountName
a | AccountName
a AccountName -> AccountName -> Bool
forall a. Eq a => a -> a -> Bool
== AccountName
hiddenAccountsName = AccountName
anew
                                     | AccountName
a AccountName -> AccountName -> Bool
forall a. Eq a => a -> a -> Bool
== AccountName
"*"                = AccountName
anew
                                     | Bool
otherwise               = AccountName
a

hiddenAccountsName :: AccountName
hiddenAccountsName = AccountName
"..." -- for now

-- generic

--topBottomBorderWithLabel :: Widget Name -> Widget Name -> Widget Name
--topBottomBorderWithLabel label = \wrapped ->
--  Widget Greedy Greedy $ do
--    c <- getContext
--    let (_w,h) = (c^.availWidthL, c^.availHeightL)
--        h' = h - 2
--        wrapped' = vLimit (h') wrapped
--        debugmsg =
--          ""
--          -- "  debug: "++show (_w,h')
--    render $
--      hBorderWithLabel (label <+> str debugmsg)
--      <=>
--      wrapped'
--      <=>
--      hBorder

topBottomBorderWithLabels :: Widget Name -> Widget Name -> Widget Name -> Widget Name
topBottomBorderWithLabels :: Widget Name -> Widget Name -> Widget Name -> Widget Name
topBottomBorderWithLabels Widget Name
toplabel Widget Name
bottomlabel Widget Name
body =
  Size -> Size -> RenderM Name (Result Name) -> Widget Name
forall n. Size -> Size -> RenderM n (Result n) -> Widget n
Widget Size
Greedy Size
Greedy (RenderM Name (Result Name) -> Widget Name)
-> RenderM Name (Result Name) -> Widget Name
forall a b. (a -> b) -> a -> b
$ do
    c <- RenderM Name (Context Name)
forall n. RenderM n (Context n)
getContext
    let (_w,h) = (c^.availWidthL, c^.availHeightL)
        h' = Int
h Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2
        body' = Int -> Widget Name -> Widget Name
forall n. Int -> Widget n -> Widget n
vLimit (Int
h') Widget Name
body
        debugmsg =
          [Char]
""
          -- "  debug: "++show (_w,h')
    render $
      hBorderWithLabel (withAttr (attrName "border") $ toplabel <+> str debugmsg)
      <=>
      body'
      <=>
      hBorderWithLabel (withAttr (attrName "border") bottomlabel)

---- XXX should be equivalent to the above, but isn't (page down goes offscreen)
--_topBottomBorderWithLabel :: Widget Name -> Widget Name -> Widget Name
--_topBottomBorderWithLabel label = \wrapped ->
-- let debugmsg = ""
-- in hBorderWithLabel (label <+> str debugmsg)
--    <=>
--    wrapped
--    <=>
--    hBorder

-- XXX superseded by pad, in theory
-- | Wrap a widget in a margin with the given horizontal and vertical
-- thickness, using the current background colour or the specified
-- colour.
-- XXX May disrupt border style of inner widgets.
-- XXX Should reduce the available size visible to inner widget, but doesn't seem to (cf rsDraw).
margin :: Int -> Int -> Maybe Color -> Widget Name -> Widget Name
margin :: Int -> Int -> Maybe Color -> Widget Name -> Widget Name
margin Int
h Int
v Maybe Color
mcolour Widget Name
w = Size -> Size -> RenderM Name (Result Name) -> Widget Name
forall n. Size -> Size -> RenderM n (Result n) -> Widget n
Widget Size
Greedy Size
Greedy (RenderM Name (Result Name) -> Widget Name)
-> RenderM Name (Result Name) -> Widget Name
forall a b. (a -> b) -> a -> b
$ do
    ctx <- RenderM Name (Context Name)
forall n. RenderM n (Context n)
getContext
    let w' = Int -> Widget Name -> Widget Name
forall n. Int -> Widget n -> Widget n
vLimit (Context Name
ctxContext Name -> Getting Int (Context Name) Int -> Int
forall s a. s -> Getting a s a -> a
^.Getting Int (Context Name) Int
forall n (f :: * -> *).
Functor f =>
(Int -> f Int) -> Context n -> f (Context n)
availHeightL Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
vInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
2) (Widget Name -> Widget Name) -> Widget Name -> Widget Name
forall a b. (a -> b) -> a -> b
$ Int -> Widget Name -> Widget Name
forall n. Int -> Widget n -> Widget n
hLimit (Context Name
ctxContext Name -> Getting Int (Context Name) Int -> Int
forall s a. s -> Getting a s a -> a
^.Getting Int (Context Name) Int
forall n (f :: * -> *).
Functor f =>
(Int -> f Int) -> Context n -> f (Context n)
availWidthL Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
hInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
2) Widget Name
w
        attr = Attr -> (Color -> Attr) -> Maybe Color -> Attr
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Attr
currentAttr (\Color
c -> Color
c Color -> Color -> Attr
`on` Color
c) Maybe Color
mcolour
    render $
      withBorderAttr attr $
      withBorderStyle (borderStyleFromChar ' ') $
      applyN v (hBorder <=>) $
      applyN h (vBorder <+>) $
      applyN v (<=> hBorder) $
      applyN h (<+> vBorder) $
      w'

   -- withBorderAttr attr .
   -- withBorderStyle (borderStyleFromChar ' ') .
   -- applyN n border

withBorderAttr :: Attr -> Widget Name -> Widget Name
withBorderAttr :: Attr -> Widget Name -> Widget Name
withBorderAttr Attr
attr = (AttrMap -> AttrMap) -> Widget Name -> Widget Name
forall n. (AttrMap -> AttrMap) -> Widget n -> Widget n
updateAttrMap ([(AttrName, Attr)] -> AttrMap -> AttrMap
applyAttrMappings [([Char] -> AttrName
attrName [Char]
"border", Attr
attr)])

---- | Like brick's continue, but first run some action to modify brick's state.
---- This action does not affect the app state, but might eg adjust a widget's scroll position.
--continueWith :: EventM n () -> ui -> EventM n (Next ui)
--continueWith brickaction ui = brickaction >> continue ui

---- | Scroll a list's viewport so that the selected item is at the top
---- of the display area.
--scrollToTop :: List Name e -> EventM Name ()
--scrollToTop list = do
--  let vpname = list^.listNameL
--  setTop (viewportScroll vpname) 0

-- | Scroll a list's viewport so that the selected item is centered in the
-- middle of the display area. When the selected item is near the end of the
-- list, the viewport is capped so that no blank padding is visible below the
-- last real item, and the last real items stay bottom-aligned (so recentering
-- a near-the-end selection doesn't push those items off screen). numitems is
-- the number of non-blank items in the list.
scrollSelectionToMiddle :: Int -> Brick.Widgets.List.List Name item -> EventM Name UIState ()
scrollSelectionToMiddle :: forall item. Int -> List Name item -> EventM Name UIState ()
scrollSelectionToMiddle Int
numitems List Name item
list = do
  case List Name item
listList Name item
-> Getting (Maybe Int) (List Name item) (Maybe Int) -> Maybe Int
forall s a. s -> Getting a s a -> a
^.Getting (Maybe Int) (List Name item) (Maybe Int)
forall n (t :: * -> *) e (f :: * -> *).
Functor f =>
(Maybe Int -> f (Maybe Int))
-> GenericList n t e -> f (GenericList n t e)
listSelectedL of
    Maybe Int
Nothing -> () -> EventM Name UIState ()
forall a. a -> EventM Name UIState a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
    Just Int
selectedrow -> do
      let name :: Name
name = List Name item
listList Name item -> Getting Name (List Name item) Name -> Name
forall s a. s -> Getting a s a -> a
^.Getting Name (List Name item) Name
forall n1 (t :: * -> *) e n2 (f :: * -> *).
Functor f =>
(n1 -> f n2) -> GenericList n1 t e -> f (GenericList n2 t e)
listNameL
      mvp <- Name -> EventM Name UIState (Maybe Viewport)
forall n s. Ord n => n -> EventM n s (Maybe Viewport)
lookupViewport Name
name
      -- Use the list viewport's actual height. Before its first render the
      -- viewport isn't known yet, so fall back to the terminal height.
      pageheight <- dbg4 "pageheight" <$> case mvp of
        Just VP{_vpSize :: Viewport -> (Int, Int)
_vpSize=(Int
_,Int
h)} -> Int -> EventM Name UIState Int
forall a. a -> EventM Name UIState a
forall (m :: * -> *) a. Monad m => a -> m a
return Int
h
        Maybe Viewport
Nothing -> do
          Vty{outputIface} <- EventM Name UIState Vty
forall n s. EventM n s Vty
getVtyHandle
          snd <$> liftIO (displayBounds outputIface)
      let
        itemheight   = [Char] -> Int -> Int
forall a. Show a => [Char] -> a -> a
dbg4 [Char]
"itemheight" (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ List Name item
listList Name item -> Getting Int (List Name item) Int -> Int
forall s a. s -> Getting a s a -> a
^.Getting Int (List Name item) Int
forall n (t :: * -> *) e (f :: * -> *).
Functor f =>
(Int -> f Int) -> GenericList n t e -> f (GenericList n t e)
listItemHeightL
        itemsperpage = [Char] -> Int -> Int
forall a. Show a => [Char] -> a -> a
dbg4 [Char]
"itemsperpage" (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ Int
pageheight Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
itemheight
        centeredtop  = Int
selectedrow Int -> Int -> Int
forall a. Num a => a -> a -> a
- (Int
itemsperpage Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2)
        maxtop       = Int
numitems Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
itemsperpage
        toprow       = [Char] -> Int -> Int
forall a. Show a => [Char] -> a -> a
dbg4 [Char]
"toprow" (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 (Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
centeredtop Int
maxtop) -- assuming ViewportScroll's row offset is measured in list items not screen rows
      setTop (viewportScroll name) toprow

--                 arrow keys       vi keys               emacs keys                 enter key
moveUpEvents :: [Event]
moveUpEvents    = [Key -> [Modifier] -> Event
EvKey Key
KUp []   , Key -> [Modifier] -> Event
EvKey (Char -> Key
KChar Char
'k') [], Key -> [Modifier] -> Event
EvKey (Char -> Key
KChar Char
'p') [Modifier
MCtrl]]
moveDownEvents :: [Event]
moveDownEvents  = [Key -> [Modifier] -> Event
EvKey Key
KDown [] , Key -> [Modifier] -> Event
EvKey (Char -> Key
KChar Char
'j') [], Key -> [Modifier] -> Event
EvKey (Char -> Key
KChar Char
'n') [Modifier
MCtrl]]
moveLeftEvents :: [Event]
moveLeftEvents  = [Key -> [Modifier] -> Event
EvKey Key
KLeft [] , Key -> [Modifier] -> Event
EvKey (Char -> Key
KChar Char
'h') [], Key -> [Modifier] -> Event
EvKey (Char -> Key
KChar Char
'b') [Modifier
MCtrl]]
moveRightEvents :: [Event]
moveRightEvents = [Key -> [Modifier] -> Event
EvKey Key
KRight [], Key -> [Modifier] -> Event
EvKey (Char -> Key
KChar Char
'l') [], Key -> [Modifier] -> Event
EvKey (Char -> Key
KChar Char
'f') [Modifier
MCtrl], Key -> [Modifier] -> Event
EvKey Key
KEnter []]

normaliseMovementKeys :: Event -> Event
normaliseMovementKeys Event
ev
  | Event
ev Event -> [Event] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Event]
moveUpEvents    = Key -> [Modifier] -> Event
EvKey Key
KUp []
  | Event
ev Event -> [Event] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Event]
moveDownEvents  = Key -> [Modifier] -> Event
EvKey Key
KDown []
  | Event
ev Event -> [Event] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Event]
moveLeftEvents  = Key -> [Modifier] -> Event
EvKey Key
KLeft []
  | Event
ev Event -> [Event] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Event]
moveRightEvents = Key -> [Modifier] -> Event
EvKey Key
KRight []
  | Bool
otherwise = Event
ev

-- | Restrict the ReportSpec's query by adding the given additional query.
reportSpecAddQuery :: Query -> ReportSpec -> ReportSpec
reportSpecAddQuery :: Query -> ReportSpec -> ReportSpec
reportSpecAddQuery Query
q ReportSpec
rspec =
    ReportSpec
rspec{_rsQuery=simplifyQuery $ And [_rsQuery rspec, q]}

-- | Update the ReportSpec's query to exclude future transactions (later than its "today" date)
-- and forecast transactions (generated by --forecast), if the given forecast DateSpan is Nothing,
-- and include them otherwise.
reportSpecSetFutureAndForecast :: Maybe DateSpan -> ReportSpec -> ReportSpec
reportSpecSetFutureAndForecast :: Maybe DateSpan -> ReportSpec -> ReportSpec
reportSpecSetFutureAndForecast Maybe DateSpan
fcast ReportSpec
rspec =
    ReportSpec
rspec{_rsQuery=simplifyQuery $ And [_rsQuery rspec, periodq, excludeforecastq fcast]}
  where
    periodq :: Query
periodq = DateSpan -> Query
Date (DateSpan -> Query)
-> (ReportOpts -> DateSpan) -> ReportOpts -> Query
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Period -> DateSpan
periodAsDateSpan (Period -> DateSpan)
-> (ReportOpts -> Period) -> ReportOpts -> DateSpan
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ReportOpts -> Period
period_ (ReportOpts -> Query) -> ReportOpts -> Query
forall a b. (a -> b) -> a -> b
$ ReportSpec -> ReportOpts
_rsReportOpts ReportSpec
rspec
    -- Except in forecast mode, exclude future/forecast transactions.
    excludeforecastq :: Maybe a -> Query
excludeforecastq (Just a
_) = Query
Any
    excludeforecastq Maybe a
Nothing  =  -- not:date:tomorrow- not:tag:generated-transaction
      [Query] -> Query
And [
         Query -> Query
Not (DateSpan -> Query
Date (DateSpan -> Query) -> DateSpan -> Query
forall a b. (a -> b) -> a -> b
$ Maybe EFDay -> Maybe EFDay -> DateSpan
DateSpan (EFDay -> Maybe EFDay
forall a. a -> Maybe a
Just (EFDay -> Maybe EFDay) -> EFDay -> Maybe EFDay
forall a b. (a -> b) -> a -> b
$ Day -> EFDay
Exact (Day -> EFDay) -> Day -> EFDay
forall a b. (a -> b) -> a -> b
$ Integer -> Day -> Day
addDays Integer
1 (Day -> Day) -> Day -> Day
forall a b. (a -> b) -> a -> b
$ ReportSpec -> Day
_rsDay ReportSpec
rspec) Maybe EFDay
forall a. Maybe a
Nothing)
        ,Query -> Query
Not Query
generatedTransactionTag
      ]

-- Vertically scroll the named list's viewport, which shows the given number of
-- non-blank items, by the given positive or negative number of items (usually 1 or -1).
-- Scrolling stops with the first item at the top or the last item at the bottom,
-- ignoring the blank items that pad the list out to the window height.
-- The selection is pushed along when necessary to keep it within the viewport;
-- without this, brick's list would keep pulling the viewport back to the selection,
-- stopping the scroll as soon as the selection reached the viewport's edge.
-- The (possibly moved) selection is left in the event handler's state.
listScrollPushingSelection :: Name -> Int -> Int -> EventM Name (Brick.Widgets.List.List Name item) ()
listScrollPushingSelection :: forall item. Name -> Int -> Int -> EventM Name (List Name item) ()
listScrollPushingSelection Name
name Int
listheight Int
scrollamt =
  Name -> EventM Name (List Name item) (Maybe Viewport)
forall n s. Ord n => n -> EventM n s (Maybe Viewport)
lookupViewport Name
name EventM Name (List Name item) (Maybe Viewport)
-> (Maybe Viewport -> EventM Name (List Name item) ())
-> EventM Name (List Name item) ()
forall a b.
EventM Name (List Name item) a
-> (a -> EventM Name (List Name item) b)
-> EventM Name (List Name item) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    Maybe Viewport
Nothing -> () -> EventM Name (List Name item) ()
forall a. a -> EventM Name (List Name item) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
    Just VP{Int
_vpTop :: Int
_vpTop :: Viewport -> Int
_vpTop, _vpSize :: Viewport -> (Int, Int)
_vpSize=(Int
_,Int
vpheight)} -> do
      let
        lastitem :: Int
lastitem = Int
listheight Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
        top :: Int
top    = Int -> Int -> Int
forall a. Ord a => a -> a -> a
min (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ Int
listheight Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
vpheight) (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ Int
_vpTop Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
scrollamt  -- new first visible item
        bottom :: Int
bottom = Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
lastitem (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ Int
top Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
vpheight Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1                                 -- new last visible item
      Name -> ViewportScroll Name
forall n. n -> ViewportScroll n
viewportScroll Name
name ViewportScroll Name -> forall s. Int -> EventM Name s ()
forall n. ViewportScroll n -> forall s. Int -> EventM n s ()
`vScrollBy` (Int
top Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
_vpTop)
      (List Name item -> List Name item)
-> EventM Name (List Name item) ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((List Name item -> List Name item)
 -> EventM Name (List Name item) ())
-> (List Name item -> List Name item)
-> EventM Name (List Name item) ()
forall a b. (a -> b) -> a -> b
$ \List Name item
list -> case List Name item -> Maybe Int
forall n (t :: * -> *) e. GenericList n t e -> Maybe Int
listSelected List Name item
list of
        Just Int
selidx | Int
selidx Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
top    -> Int -> List Name item -> List Name item
forall (t :: * -> *) n e.
(Foldable t, Splittable t) =>
Int -> GenericList n t e -> GenericList n t e
listMoveTo Int
top List Name item
list
                    | Int
selidx Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
bottom -> Int -> List Name item -> List Name item
forall (t :: * -> *) n e.
(Foldable t, Splittable t) =>
Int -> GenericList n t e -> GenericList n t e
listMoveTo Int
bottom List Name item
list
        Maybe Int
_ -> List Name item
list

-- | A debug logging helper for hledger-ui code: at any debug level >= 1,
-- logs the string to hledger-ui.log before returning the second argument.
-- Uses unsafePerformIO.
dbgui :: String -> a -> a
dbgui :: forall a. [Char] -> a -> a
dbgui = [Char] -> a -> a
forall a. [Char] -> a -> a
dbg1Msg

-- | Like dbgui, but convenient to use in IO.
dbguiIO :: String -> IO ()
dbguiIO :: [Char] -> IO ()
dbguiIO = [Char] -> IO ()
forall (m :: * -> *). MonadIO m => [Char] -> m ()
dbg1MsgIO

-- | Was this journal's main file read from standard input ?
-- If so it can't be re-read, edited, or watched for changes.
journalIsFromStdin :: Journal -> Bool
journalIsFromStdin :: Journal -> Bool
journalIsFromStdin Journal
j = Journal -> [Char]
journalFilePath Journal
j [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
"-"

-- | Like dbgui, but convenient to use in EventM handlers.
dbguiEv :: String -> EventM Name s ()
dbguiEv :: forall s. [Char] -> EventM Name s ()
dbguiEv [Char]
s = [Char] -> EventM Name s () -> EventM Name s ()
forall a. [Char] -> a -> a
dbg1Msg [Char]
s (EventM Name s () -> EventM Name s ())
-> EventM Name s () -> EventM Name s ()
forall a b. (a -> b) -> a -> b
$ () -> EventM Name s ()
forall a. a -> EventM Name s a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

-- | Like dbguiEv, but log a compact view of the current screen stack.
-- See showScreenStack.
-- To just log the stack: @dbguiScreensEv "" showScreenId ui@
dbguiScreensEv :: String -> (Screen -> String) -> UIState -> EventM Name UIState ()
dbguiScreensEv :: [Char] -> (Screen -> [Char]) -> UIState -> EventM Name UIState ()
dbguiScreensEv [Char]
postfix Screen -> [Char]
showscr UIState
ui = [Char] -> EventM Name UIState ()
forall s. [Char] -> EventM Name s ()
dbguiEv ([Char] -> EventM Name UIState ())
-> [Char] -> EventM Name UIState ()
forall a b. (a -> b) -> a -> b
$ [Char] -> (Screen -> [Char]) -> UIState -> [Char]
showScreenStack [Char]
postfix Screen -> [Char]
showscr UIState
ui

-- Render a compact labelled view of the current screen stack,
-- adding the given postfix to the label (can be empty),
-- from the topmost screen to the currently-viewed screen,
-- with each screen rendered by the given rendering function.
-- Useful for inspecting states across the whole screen stack.
-- Some screen rendering functions are 
-- @showScreenId@, @showScreenSelection@, @showScreenRegisterDescriptions@.
--
-- Eg to just show the stack: @showScreenStack "" showScreenId ui@
--
-- To to show the stack plus selected item indexes: @showScreenStack "" showScreenSelection ui@
--
showScreenStack :: String -> (Screen -> String) -> UIState -> String
showScreenStack :: [Char] -> (Screen -> [Char]) -> UIState -> [Char]
showScreenStack [Char]
postfix Screen -> [Char]
showscr UIState
ui = [[Char]] -> [Char]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [
    [Char]
"screen stack"
  ,if [Char] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Char]
postfix then [Char]
"" else [Char]
", " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
postfix
  ,[Char]
": "
  ,[[Char]] -> [Char]
unwords ([[Char]] -> [Char]) -> [[Char]] -> [Char]
forall a b. (a -> b) -> a -> b
$ (Screen -> [Char]) -> UIState -> [[Char]]
forall a. (Screen -> a) -> UIState -> [a]
mapScreens Screen -> [Char]
showscr UIState
ui
  ]

-- | Run a function on each screen in a UIState's screen "stack",
-- from topmost screen down to currently-viewed screen.
mapScreens :: (Screen -> a) -> UIState -> [a]
mapScreens :: forall a. (Screen -> a) -> UIState -> [a]
mapScreens Screen -> a
f UIState{[Screen]
aPrevScreens :: [Screen]
aPrevScreens :: UIState -> [Screen]
aPrevScreens, Screen
aScreen :: Screen
aScreen :: UIState -> Screen
aScreen} = (Screen -> a) -> [Screen] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map Screen -> a
f ([Screen] -> [a]) -> [Screen] -> [a]
forall a b. (a -> b) -> a -> b
$ [Screen] -> [Screen]
forall a. [a] -> [a]
reverse ([Screen] -> [Screen]) -> [Screen] -> [Screen]
forall a b. (a -> b) -> a -> b
$ Screen
aScreen Screen -> [Screen] -> [Screen]
forall a. a -> [a] -> [a]
: [Screen]
aPrevScreens

-- Show a screen's compact id (first letter of its constructor).
showScreenId :: Screen -> String
showScreenId :: Screen -> [Char]
showScreenId = \case
  MS MenuScreenState
_             -> [Char]
"M"  -- menu
  AS ASS{AccountsScreenKind
_assKind :: AccountsScreenKind
_assKind :: AccountsScreenState -> AccountsScreenKind
_assKind} -> AccountsScreenKind -> [Char]
accountsScreenKindId AccountsScreenKind
_assKind
  RS RegisterScreenState
_             -> [Char]
"R"  -- register
  TS TransactionScreenState
_             -> [Char]
"T"  -- transaction
  ES ErrorScreenState
_             -> [Char]
"E"  -- error

-- | The compact id letter for an accounts-like screen of the given kind.
accountsScreenKindId :: AccountsScreenKind -> String
accountsScreenKindId :: AccountsScreenKind -> [Char]
accountsScreenKindId = \case
  AccountsScreenKind
AllAccounts             -> [Char]
"A"  -- all accounts
  AccountsScreenKind
CashAccounts            -> [Char]
"C"  -- cash accounts
  AccountsScreenKind
BalancesheetAccounts    -> [Char]
"B"  -- balance sheet accounts
  AccountsScreenKind
IncomestatementAccounts -> [Char]
"I"  -- income statement accounts

-- Show a screen's compact id, plus for register screens, the transaction descriptions.
showScreenRegisterDescriptions :: Screen -> String
showScreenRegisterDescriptions :: Screen -> [Char]
showScreenRegisterDescriptions Screen
scr = case Screen
scr of
  RS RegisterScreenState
sst -> ((Screen -> [Char]
showScreenId Screen
scr [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
":") [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++) ([Char] -> [Char]) -> [Char] -> [Char]
forall a b. (a -> b) -> a -> b
$ -- menu
    [Char] -> [[Char]] -> [Char]
forall a. [a] -> [[a]] -> [a]
intercalate [Char]
"," ([[Char]] -> [Char]) -> [[Char]] -> [Char]
forall a b. (a -> b) -> a -> b
$ (RegisterScreenItem -> [Char]) -> [RegisterScreenItem] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map (AccountName -> [Char]
T.unpack (AccountName -> [Char])
-> (RegisterScreenItem -> AccountName)
-> RegisterScreenItem
-> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RegisterScreenItem -> AccountName
rsItemDescription) ([RegisterScreenItem] -> [[Char]])
-> [RegisterScreenItem] -> [[Char]]
forall a b. (a -> b) -> a -> b
$
    (RegisterScreenItem -> Bool)
-> [RegisterScreenItem] -> [RegisterScreenItem]
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (Bool -> Bool
not (Bool -> Bool)
-> (RegisterScreenItem -> Bool) -> RegisterScreenItem -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AccountName -> Bool
T.null (AccountName -> Bool)
-> (RegisterScreenItem -> AccountName)
-> RegisterScreenItem
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RegisterScreenItem -> AccountName
rsItemDate) ([RegisterScreenItem] -> [RegisterScreenItem])
-> [RegisterScreenItem] -> [RegisterScreenItem]
forall a b. (a -> b) -> a -> b
$ Vector RegisterScreenItem -> [RegisterScreenItem]
forall a. Vector a -> [a]
V.toList (Vector RegisterScreenItem -> [RegisterScreenItem])
-> Vector RegisterScreenItem -> [RegisterScreenItem]
forall a b. (a -> b) -> a -> b
$ GenericList Name Vector RegisterScreenItem
-> Vector RegisterScreenItem
forall n (t :: * -> *) e. GenericList n t e -> t e
listElements (GenericList Name Vector RegisterScreenItem
 -> Vector RegisterScreenItem)
-> GenericList Name Vector RegisterScreenItem
-> Vector RegisterScreenItem
forall a b. (a -> b) -> a -> b
$ RegisterScreenState -> GenericList Name Vector RegisterScreenItem
_rssList RegisterScreenState
sst
  Screen
_ -> Screen -> [Char]
showScreenId Screen
scr

-- Show a screen's compact id, plus index of its selected list item if any.
showScreenSelection :: Screen -> String
showScreenSelection :: Screen -> [Char]
showScreenSelection = \case
  MS MSS{List Name MenuScreenItem
_mssList :: List Name MenuScreenItem
_mssList :: MenuScreenState -> List Name MenuScreenItem
_mssList}          -> [Char]
"M" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ ([Char] -> (Int -> [Char]) -> Maybe Int -> [Char]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [Char]
"" Int -> [Char]
forall a. Show a => a -> [Char]
show (Maybe Int -> [Char]) -> Maybe Int -> [Char]
forall a b. (a -> b) -> a -> b
$ List Name MenuScreenItem -> Maybe Int
forall n (t :: * -> *) e. GenericList n t e -> Maybe Int
listSelected List Name MenuScreenItem
_mssList)  -- menu
  AS ASS{AccountsScreenKind
_assKind :: AccountsScreenState -> AccountsScreenKind
_assKind :: AccountsScreenKind
_assKind,List Name AccountsScreenItem
_assList :: List Name AccountsScreenItem
_assList :: AccountsScreenState -> List Name AccountsScreenItem
_assList} -> AccountsScreenKind -> [Char]
accountsScreenKindId AccountsScreenKind
_assKind [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ ([Char] -> (Int -> [Char]) -> Maybe Int -> [Char]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [Char]
"" Int -> [Char]
forall a. Show a => a -> [Char]
show (Maybe Int -> [Char]) -> Maybe Int -> [Char]
forall a b. (a -> b) -> a -> b
$ List Name AccountsScreenItem -> Maybe Int
forall n (t :: * -> *) e. GenericList n t e -> Maybe Int
listSelected List Name AccountsScreenItem
_assList)
  RS RSS{GenericList Name Vector RegisterScreenItem
_rssList :: RegisterScreenState -> GenericList Name Vector RegisterScreenItem
_rssList :: GenericList Name Vector RegisterScreenItem
_rssList}          -> [Char]
"R" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ ([Char] -> (Int -> [Char]) -> Maybe Int -> [Char]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [Char]
"" Int -> [Char]
forall a. Show a => a -> [Char]
show (Maybe Int -> [Char]) -> Maybe Int -> [Char]
forall a b. (a -> b) -> a -> b
$ GenericList Name Vector RegisterScreenItem -> Maybe Int
forall n (t :: * -> *) e. GenericList n t e -> Maybe Int
listSelected GenericList Name Vector RegisterScreenItem
_rssList)  -- register
  TS TransactionScreenState
_ -> [Char]
"T"  -- transaction
  ES ErrorScreenState
_ -> [Char]
"E"  -- error

-- | How many blank items to add to lists to fill the full window height.
uiNumBlankItems :: Int
uiNumBlankItems :: Int
uiNumBlankItems
  -- | debugLevel >= uiDebugLevel = 0    -- suppress to improve debug output.
  -- | otherwise 
  = Int
100  -- 100 ought to be enough for anyone

-- Send some events to vty, atomically so they won't have other events interleaved.
-- (But there may be events already in the channel ahead of them.)
sendVtyEvents :: [Event] -> EventM n s ()
sendVtyEvents :: forall n s. [Event] -> EventM n s ()
sendVtyEvents [Event]
evs = do
  input <- Input -> TChan InternalEvent
eventChannel (Input -> TChan InternalEvent)
-> (Vty -> Input) -> Vty -> TChan InternalEvent
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vty -> Input
inputIface (Vty -> TChan InternalEvent)
-> EventM n s Vty -> EventM n s (TChan InternalEvent)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> EventM n s Vty
forall n s. EventM n s Vty
getVtyHandle
  liftIO $ atomically $ mapM_ (writeTChan input . InputEvent) evs

-- | Recognise the click that brings a background terminal window forward, so that it
-- only activates the window, without also selecting or entering something.
-- With focus reporting on, such a click arrives as a focus gain followed, a moment later,
-- by a left button press; we ignore that press, any repeats of it, and its release.
-- A click made more than 'activatingClickTime' after the window gained focus (eg after switching
-- to it with the keyboard) is handled as usual. Focus events themselves are not passed on.
-- Given the current time, an event and the current state, returns the new state and
-- whether to ignore the event.
clickFocusStep :: UTCTime -> BrickEvent n e -> ClickFocus -> (ClickFocus, Bool)
clickFocusStep :: forall n e.
UTCTime -> BrickEvent n e -> ClickFocus -> (ClickFocus, Bool)
clickFocusStep UTCTime
now BrickEvent n e
ev ClickFocus
cf = case BrickEvent n e
ev of
  VtyEvent Event
EvGainedFocus -> (UTCTime -> ClickFocus
FocusGainedAt UTCTime
now, Bool
True)
  VtyEvent Event
EvLostFocus   -> (ClickFocus
Unfocused, Bool
True)
  BrickEvent n e
_ | BrickEvent n e -> Bool
forall {n} {e}. BrickEvent n e -> Bool
isLeftPress BrickEvent n e
ev -> case ClickFocus
cf of
        FocusGainedAt UTCTime
t | UTCTime -> UTCTime -> NominalDiffTime
diffUTCTime UTCTime
now UTCTime
t NominalDiffTime -> NominalDiffTime -> Bool
forall a. Ord a => a -> a -> Bool
< NominalDiffTime
activatingClickTime -> (ClickFocus
IgnoringClick, Bool
True)
        ClickFocus
IgnoringClick -> (ClickFocus
IgnoringClick, Bool
True)
        ClickFocus
_             -> (ClickFocus
Unfocused, Bool
False)
    | BrickEvent n e -> Bool
forall {n} {e}. BrickEvent n e -> Bool
isRelease BrickEvent n e
ev -> (ClickFocus
Unfocused, ClickFocus
cf ClickFocus -> ClickFocus -> Bool
forall a. Eq a => a -> a -> Bool
== ClickFocus
IgnoringClick)
    | Bool
otherwise -> (ClickFocus
cf, Bool
False)
  where
    isLeftPress :: BrickEvent n e -> Bool
isLeftPress = \case
      MouseDown n
_ Button
BLeft [Modifier]
_ Location
_                -> Bool
True
      VtyEvent (EvMouseDown Int
_ Int
_ Button
BLeft [Modifier]
_)   -> Bool
True
      BrickEvent n e
_                                    -> Bool
False
    isRelease :: BrickEvent n e -> Bool
isRelease = \case
      MouseUp{}            -> Bool
True
      VtyEvent EvMouseUp{} -> Bool
True
      BrickEvent n e
_                    -> Bool
False

-- | How soon after the window gains focus a left button press is taken to be the
-- activating click. Terminals send the two together, so this needs to be only long enough
-- to absorb event handling delays, and shorter than a deliberate switch-then-click.
activatingClickTime :: NominalDiffTime
activatingClickTime :: NominalDiffTime
activatingClickTime = NominalDiffTime
0.2