From 50efcfe359e8a39fe8529e630608eb87a86cb805 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Fri, 30 Jun 2017 19:45:21 +0100 Subject: [PATCH] ui: accounts: reduce "warping", select alphabetically preceding account ..when selected account disappears, eg when toggling zero accounts. --- hledger-ui/Hledger/UI/AccountsScreen.hs | 21 +++++++++++++++------ hledger-ui/Hledger/UI/RegisterScreen.hs | 10 +++++----- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/hledger-ui/Hledger/UI/AccountsScreen.hs b/hledger-ui/Hledger/UI/AccountsScreen.hs index c5326cdc7..9e367c622 100644 --- a/hledger-ui/Hledger/UI/AccountsScreen.hs +++ b/hledger-ui/Hledger/UI/AccountsScreen.hs @@ -24,6 +24,7 @@ import Data.Time.Calendar (Day) import qualified Data.Vector as V import Graphics.Vty (Event(..),Key(..),Modifier(..)) import Lens.Micro.Platform +import Safe import System.Console.ANSI import System.FilePath (takeFileName) @@ -56,17 +57,25 @@ asInit d reset ui@UIState{ where newitems = list AccountsList (V.fromList $ displayitems ++ blankitems) 1 - -- keep the selection near the last selected account - -- (may need to move to the next leaf account when entering flat mode) + -- decide which account is selected: + -- if reset is true, the first account; + -- otherwise, the previously selected account if possible; + -- otherwise, the first account with the same prefix (eg first leaf account when entering flat mode); + -- otherwise, the alphabetically preceding account. newitems' = listMoveTo selidx newitems where selidx = case (reset, listSelectedElement $ _asList s) of (True, _) -> 0 (_, Nothing) -> 0 - (_, Just (_,AccountsScreenItem{asItemAccountName=a})) -> fromMaybe (fromMaybe 0 mprefixmatch) mexactmatch - where - mexactmatch = findIndex ((a ==) . asItemAccountName) displayitems - mprefixmatch = findIndex ((a `isAccountNamePrefixOf`) . asItemAccountName) displayitems + (_, Just (_,AccountsScreenItem{asItemAccountName=a})) -> + headDef 0 $ catMaybes [ + findIndex (a ==) as + ,findIndex (a `isAccountNamePrefixOf`) as + ,Just $ max 0 (length (filter (< a) as) - 1) + ] + where + as = map asItemAccountName displayitems + uopts' = uopts{cliopts_=copts{reportopts_=ropts'}} ropts' = ropts{accountlistmode_=if flat_ ropts then ALFlat else ALTree} diff --git a/hledger-ui/Hledger/UI/RegisterScreen.hs b/hledger-ui/Hledger/UI/RegisterScreen.hs index 88fcd65d8..da47e566f 100644 --- a/hledger-ui/Hledger/UI/RegisterScreen.hs +++ b/hledger-ui/Hledger/UI/RegisterScreen.hs @@ -102,12 +102,12 @@ rsInit d reset ui@UIState{aopts=UIOpts{cliopts_=CliOpts{reportopts_=ropts}}, ajo -- build the List newitems = list RegisterList (V.fromList $ displayitems ++ blankitems) 1 - -- decide which transaction is selected. - -- if reset is true, select the last (latest) transaction; - -- otherwise, select the previously selected transaction if possible; - -- otherwise, select the transaction nearest in date to it; + -- decide which transaction is selected: + -- if reset is true, the last (latest) transaction; + -- otherwise, the previously selected transaction if possible; + -- otherwise, the transaction nearest in date to it; -- or if there's several with the same date, the nearest in journal order; - -- otherwise, select the last (latest) transaction. + -- otherwise, the last (latest) transaction. newitems' = listMoveTo newselidx newitems where newselidx =