From 0cb5703ad1a133efa2c7c6da4b77e97d3e918949 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Tue, 2 Aug 2016 07:09:08 -0700 Subject: [PATCH] ui: refactor draw functions --- hledger-ui/Hledger/UI/AccountsScreen.hs | 99 +++++++++++----------- hledger-ui/Hledger/UI/ErrorScreen.hs | 23 ++--- hledger-ui/Hledger/UI/RegisterScreen.hs | 82 +++++++++--------- hledger-ui/Hledger/UI/TransactionScreen.hs | 64 +++++++------- 4 files changed, 138 insertions(+), 130 deletions(-) diff --git a/hledger-ui/Hledger/UI/AccountsScreen.hs b/hledger-ui/Hledger/UI/AccountsScreen.hs index dce86dcee..6ec5cb91b 100644 --- a/hledger-ui/Hledger/UI/AccountsScreen.hs +++ b/hledger-ui/Hledger/UI/AccountsScreen.hs @@ -110,42 +110,6 @@ asDraw UIState{aopts=UIOpts{cliopts_=copts@CliOpts{reportopts_=ropts}} -- Minibuffer e -> [minibuffer e, maincontent] _ -> [maincontent] where - toplabel = - files - <+> nonzero - <+> str " accounts" - <+> withAttr (borderAttr <> "query") (str (if flat_ ropts then " (flat)" else "")) - <+> borderQueryStr querystr - <+> togglefilters - <+> borderDepthStr mdepth - <+> str " (" - <+> cur - <+> str "/" - <+> total - <+> str ")" - <+> (if ignore_assertions_ copts - then withAttr (borderAttr <> "query") (str " ignoring balance assertions") - else str "") - files = case journalFilePaths j of - [] -> str "" - f:_ -> withAttr ("border" <> "bold") $ str $ takeFileName f - -- [f,_:[]] -> (withAttr ("border" <> "bold") $ str $ takeFileName f) <+> str " (& 1 included file)" - -- f:fs -> (withAttr ("border" <> "bold") $ str $ takeFileName f) <+> str (" (& " ++ show (length fs) ++ " included files)") - querystr = query_ ropts - mdepth = depth_ ropts - togglefilters = - case concat [ - uiShowClearedStatus $ clearedstatus_ ropts - ,if real_ ropts then ["real"] else [] - ] of - [] -> str "" - fs -> str " with " <+> withAttr (borderAttr <> "query") (str $ intercalate ", " fs) <+> str " txns" - nonzero | empty_ ropts = str "" - | otherwise = withAttr (borderAttr <> "query") (str " nonzero") - cur = str (case _asList s ^. listSelectedL of - Nothing -> "-" - Just i -> show (i + 1)) - total = str $ show $ V.length $ s ^. asList . listElementsL maincontent = Widget Greedy Greedy $ do c <- getContext let @@ -184,21 +148,60 @@ asDraw UIState{aopts=UIOpts{cliopts_=copts@CliOpts{reportopts_=ropts}} render $ defaultLayout toplabel bottomlabel $ renderList (asDrawItem colwidths) True (_asList s) where + toplabel = + files + <+> nonzero + <+> str " accounts" + <+> withAttr (borderAttr <> "query") (str (if flat_ ropts then " (flat)" else "")) + <+> borderQueryStr querystr + <+> togglefilters + <+> borderDepthStr mdepth + <+> str " (" + <+> cur + <+> str "/" + <+> total + <+> str ")" + <+> (if ignore_assertions_ copts + then withAttr (borderAttr <> "query") (str " ignoring balance assertions") + else str "") + where + files = case journalFilePaths j of + [] -> str "" + f:_ -> withAttr ("border" <> "bold") $ str $ takeFileName f + -- [f,_:[]] -> (withAttr ("border" <> "bold") $ str $ takeFileName f) <+> str " (& 1 included file)" + -- f:fs -> (withAttr ("border" <> "bold") $ str $ takeFileName f) <+> str (" (& " ++ show (length fs) ++ " included files)") + querystr = query_ ropts + mdepth = depth_ ropts + togglefilters = + case concat [ + uiShowClearedStatus $ clearedstatus_ ropts + ,if real_ ropts then ["real"] else [] + ] of + [] -> str "" + fs -> str " with " <+> withAttr (borderAttr <> "query") (str $ intercalate ", " fs) <+> str " txns" + nonzero | empty_ ropts = str "" + | otherwise = withAttr (borderAttr <> "query") (str " nonzero") + cur = str (case _asList s ^. listSelectedL of + Nothing -> "-" + Just i -> show (i + 1)) + total = str $ show $ V.length $ s ^. asList . listElementsL + bottomlabel = case mode of Minibuffer ed -> minibuffer ed _ -> quickhelp - quickhelp = borderKeysStr [ - ("?", "help") - ,("right", "register") - ,("F", "flat?") - ,("-+0123456789", "depth") - --,("/", "filter") - --,("DEL", "unfilter") - --,("ESC", "cancel/top") - ,("a", "add") - ,("g", "reload") - ,("q", "quit") - ] + where + quickhelp = borderKeysStr [ + ("?", "help") + ,("right", "register") + ,("F", "flat?") + ,("-+0123456789", "depth") + --,("/", "filter") + --,("DEL", "unfilter") + --,("ESC", "cancel/top") + ,("a", "add") + ,("g", "reload") + ,("q", "quit") + ] asDraw _ = error "draw function called with wrong screen type, should not happen" diff --git a/hledger-ui/Hledger/UI/ErrorScreen.hs b/hledger-ui/Hledger/UI/ErrorScreen.hs index 66362c54d..b0cea4fda 100644 --- a/hledger-ui/Hledger/UI/ErrorScreen.hs +++ b/hledger-ui/Hledger/UI/ErrorScreen.hs @@ -47,23 +47,24 @@ esDraw UIState{ --aopts=UIOpts{cliopts_=copts@CliOpts{}} -- Minibuffer e -> [minibuffer e, maincontent] _ -> [maincontent] where - toplabel = - withAttr ("border" <> "bold") (str "Oops. Please fix this problem then press g to reload") --- <+> (if ignore_assertions_ copts then withAttr (borderAttr <> "query") (str " ignoring") else str " not ignoring") - maincontent = Widget Greedy Greedy $ do render $ defaultLayout toplabel bottomlabel $ withAttr "error" $ str $ esError where + toplabel = + withAttr ("border" <> "bold") (str "Oops. Please fix this problem then press g to reload") + -- <+> (if ignore_assertions_ copts then withAttr (borderAttr <> "query") (str " ignoring") else str " not ignoring") + bottomlabel = case mode of -- Minibuffer ed -> minibuffer ed _ -> quickhelp - quickhelp = borderKeysStr [ - ("h", "help") - ,("ESC", "cancel/top") - ,("E", "editor") - ,("g", "reload") - ,("q", "quit") - ] + where + quickhelp = borderKeysStr [ + ("h", "help") + ,("ESC", "cancel/top") + ,("E", "editor") + ,("g", "reload") + ,("q", "quit") + ] esDraw _ = error "draw function called with wrong screen type, should not happen" diff --git a/hledger-ui/Hledger/UI/RegisterScreen.hs b/hledger-ui/Hledger/UI/RegisterScreen.hs index d921b4f62..fa5acfcc7 100644 --- a/hledger-ui/Hledger/UI/RegisterScreen.hs +++ b/hledger-ui/Hledger/UI/RegisterScreen.hs @@ -113,36 +113,7 @@ rsDraw UIState{aopts=UIOpts{cliopts_=copts@CliOpts{reportopts_=ropts}} -- Minibuffer e -> [minibuffer e, maincontent] _ -> [maincontent] where - inclusive = not (flat_ ropts) || rsForceInclusive - toplabel = - withAttr ("border" <> "bold") (str $ T.unpack $ replaceHiddenAccountsNameWith "All" rsAccount) - <+> withAttr (borderAttr <> "query") (str $ if inclusive then "" else " (exclusive)") - <+> togglefilters - <+> str " transactions" - <+> borderQueryStr (query_ ropts) - -- <+> str " and subs" - <+> str " (" - <+> cur - <+> str "/" - <+> total - <+> str ")" - <+> (if ignore_assertions_ copts then withAttr (borderAttr <> "query") (str " ignoring balance assertions") else str "") - togglefilters = - case concat [ - uiShowClearedStatus $ clearedstatus_ ropts - ,if real_ ropts then ["real"] else [] - ,if empty_ ropts then [] else ["nonzero"] - ] of - [] -> str "" - fs -> withAttr (borderAttr <> "query") (str $ " " ++ intercalate ", " fs) - cur = str $ case rsList ^. listSelectedL of - Nothing -> "-" - Just i -> show (i + 1) - total = str $ show $ length displayitems displayitems = V.toList $ rsList ^. listElementsL - - -- query = query_ $ reportopts_ $ cliopts_ opts - maincontent = Widget Greedy Greedy $ do -- calculate column widths, based on current available width c <- getContext @@ -187,20 +158,51 @@ rsDraw UIState{aopts=UIOpts{cliopts_=copts@CliOpts{reportopts_=ropts}} render $ defaultLayout toplabel bottomlabel $ renderList (rsDrawItem colwidths) True rsList where + toplabel = + withAttr ("border" <> "bold") (str $ T.unpack $ replaceHiddenAccountsNameWith "All" rsAccount) + <+> withAttr (borderAttr <> "query") (str $ if inclusive then "" else " (exclusive)") + <+> togglefilters + <+> str " transactions" + <+> borderQueryStr (query_ ropts) + -- <+> str " and subs" + <+> str " (" + <+> cur + <+> str "/" + <+> total + <+> str ")" + <+> (if ignore_assertions_ copts then withAttr (borderAttr <> "query") (str " ignoring balance assertions") else str "") + where + inclusive = not (flat_ ropts) || rsForceInclusive + togglefilters = + case concat [ + uiShowClearedStatus $ clearedstatus_ ropts + ,if real_ ropts then ["real"] else [] + ,if empty_ ropts then [] else ["nonzero"] + ] of + [] -> str "" + fs -> withAttr (borderAttr <> "query") (str $ " " ++ intercalate ", " fs) + cur = str $ case rsList ^. listSelectedL of + Nothing -> "-" + Just i -> show (i + 1) + total = str $ show $ length displayitems + + -- query = query_ $ reportopts_ $ cliopts_ opts + bottomlabel = case mode of Minibuffer ed -> minibuffer ed _ -> quickhelp - quickhelp = borderKeysStr [ - ("?", "help") - ,("left", "back") - ,("right", "transaction") - ,("/", "filter") - ,("DEL", "unfilter") - --,("ESC", "reset") - ,("a", "add") - ,("g", "reload") - ,("q", "quit") - ] + where + quickhelp = borderKeysStr [ + ("?", "help") + ,("left", "back") + ,("right", "transaction") + ,("/", "filter") + ,("DEL", "unfilter") + --,("ESC", "reset") + ,("a", "add") + ,("g", "reload") + ,("q", "quit") + ] rsDraw _ = error "draw function called with wrong screen type, should not happen" diff --git a/hledger-ui/Hledger/UI/TransactionScreen.hs b/hledger-ui/Hledger/UI/TransactionScreen.hs index ff8315b37..f0c810146 100644 --- a/hledger-ui/Hledger/UI/TransactionScreen.hs +++ b/hledger-ui/Hledger/UI/TransactionScreen.hs @@ -57,46 +57,48 @@ tsDraw UIState{aopts=UIOpts{cliopts_=copts@CliOpts{reportopts_=ropts}} -- Minibuffer e -> [minibuffer e, maincontent] _ -> [maincontent] where - -- datedesc = show (tdate t) ++ " " ++ tdescription t - toplabel = - str "Transaction " - -- <+> withAttr ("border" <> "bold") (str $ "#" ++ show (tindex t)) - -- <+> str (" ("++show i++" of "++show (length nts)++" in "++acct++")") - <+> (str $ "#" ++ show (tindex t)) - <+> str " (" - <+> withAttr ("border" <> "bold") (str $ show i) - <+> str (" of "++show (length nts)) - <+> togglefilters - <+> borderQueryStr (query_ ropts) - <+> str (" in "++T.unpack (replaceHiddenAccountsNameWith "All" acct)++")") - <+> (if ignore_assertions_ copts then withAttr (borderAttr <> "query") (str " ignoring balance assertions") else str "") - togglefilters = - case concat [ - uiShowClearedStatus $ clearedstatus_ ropts - ,if real_ ropts then ["real"] else [] - ,if empty_ ropts then [] else ["nonzero"] - ] of - [] -> str "" - fs -> withAttr (borderAttr <> "query") (str $ " " ++ intercalate ", " fs) maincontent = Widget Greedy Greedy $ do render $ defaultLayout toplabel bottomlabel $ str $ showTransactionUnelidedOneLineAmounts $ -- (if real_ ropts then filterTransactionPostings (Real True) else id) -- filter postings by --real t where + toplabel = + str "Transaction " + -- <+> withAttr ("border" <> "bold") (str $ "#" ++ show (tindex t)) + -- <+> str (" ("++show i++" of "++show (length nts)++" in "++acct++")") + <+> (str $ "#" ++ show (tindex t)) + <+> str " (" + <+> withAttr ("border" <> "bold") (str $ show i) + <+> str (" of "++show (length nts)) + <+> togglefilters + <+> borderQueryStr (query_ ropts) + <+> str (" in "++T.unpack (replaceHiddenAccountsNameWith "All" acct)++")") + <+> (if ignore_assertions_ copts then withAttr (borderAttr <> "query") (str " ignoring balance assertions") else str "") + where + togglefilters = + case concat [ + uiShowClearedStatus $ clearedstatus_ ropts + ,if real_ ropts then ["real"] else [] + ,if empty_ ropts then [] else ["nonzero"] + ] of + [] -> str "" + fs -> withAttr (borderAttr <> "query") (str $ " " ++ intercalate ", " fs) + bottomlabel = case mode of -- Minibuffer ed -> minibuffer ed _ -> quickhelp - quickhelp = borderKeysStr [ - ("?", "help") - ,("left", "back") - ,("up/down", "prev/next") - --,("ESC", "cancel/top") - -- ,("a", "add") - ,("E", "editor") - ,("g", "reload") - ,("q", "quit") - ] + where + quickhelp = borderKeysStr [ + ("?", "help") + ,("left", "back") + ,("up/down", "prev/next") + --,("ESC", "cancel/top") + -- ,("a", "add") + ,("E", "editor") + ,("g", "reload") + ,("q", "quit") + ] tsDraw _ = error "draw function called with wrong screen type, should not happen"