Skip to content

Commit

Permalink
lib,cli,ui: Refactor to simplify code by using lenses.
Browse files Browse the repository at this point in the history
  • Loading branch information
Xitian9 committed Jun 8, 2021
1 parent 1fab4f0 commit 9693f6b
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 17 deletions.
3 changes: 2 additions & 1 deletion hledger-lib/Hledger/Read/CsvReader.hs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import Text.Megaparsec hiding (match, parse)
import Text.Megaparsec.Char (char, newline, string)
import Text.Megaparsec.Custom (customErrorBundlePretty, parseErrorAt)
import Text.Printf (printf)
import Lens.Micro (set)

import Hledger.Data
import Hledger.Utils
Expand Down Expand Up @@ -116,7 +117,7 @@ parse iopts f t = do
-- apply any command line account aliases. Can fail with a bad replacement pattern.
in case journalApplyAliases (aliasesFromOpts iopts) pj' of
Left e -> throwError e
Right pj'' -> journalFinalise iopts{balancingopts_=(balancingopts_ iopts){ignore_assertions_=True}} f t pj''
Right pj'' -> journalFinalise (set ignore_assertions True iopts) f t pj''

--- ** reading rules files
--- *** rules utilities
Expand Down
4 changes: 1 addition & 3 deletions hledger-ui/Hledger/UI/AccountsScreen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,7 @@ asDraw UIState{_aopts=_uopts@UIOpts{cliopts_=copts@CliOpts{reportspec_=rspec}}
<+> borderQueryStr (T.unpack . T.unwords . map textQuoteIfNeeded $ querystring_ ropts)
<+> borderDepthStr mdepth
<+> str (" ("++curidx++"/"++totidx++")")
<+> (if ignore_assertions_ . balancingopts_ $ inputopts_ copts
then withAttr ("border" <> "query") (str " ignoring balance assertions")
else str "")
<+> (if copts ^. ignore_assertions then withAttr ("border" <> "query") (str " ignoring balance assertions") else str "")
where
files = case journalFilePaths j of
[] -> str ""
Expand Down
2 changes: 1 addition & 1 deletion hledger-ui/Hledger/UI/RegisterScreen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ rsDraw UIState{_aopts=UIOpts{cliopts_=copts},aScreen=RegisterScreen{..},aMode=mo
<+> str "/"
<+> total
<+> str ")"
<+> (if ignore_assertions_ . balancingopts_ $ inputopts_ copts then withAttr ("border" <> "query") (str " ignoring balance assertions") else str "")
<+> (if copts ^. ignore_assertions then withAttr ("border" <> "query") (str " ignoring balance assertions") else str "")
where
togglefilters =
case concat [
Expand Down
3 changes: 2 additions & 1 deletion hledger-ui/Hledger/UI/TransactionScreen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Data.Maybe
import qualified Data.Text as T
import Data.Time.Calendar (Day)
import Graphics.Vty (Event(..),Key(..),Modifier(..))
import Lens.Micro ((^.))
import Brick
import Brick.Widgets.List (listMoveTo)

Expand Down Expand Up @@ -98,7 +99,7 @@ tsDraw UIState{_aopts=UIOpts{cliopts_=copts@CliOpts{reportspec_=rspec@ReportSpec
<+> togglefilters
<+> borderQueryStr (unwords . map (quoteIfNeeded . T.unpack) $ querystring_ ropts)
<+> str (" in "++T.unpack (replaceHiddenAccountsNameWith "All" acct)++")")
<+> (if ignore_assertions_ . balancingopts_ $ inputopts_ copts then withAttr ("border" <> "query") (str " ignoring balance assertions") else str "")
<+> (if copts ^. ignore_assertions then withAttr ("border" <> "query") (str " ignoring balance assertions") else str "")
where
togglefilters =
case concat [
Expand Down
20 changes: 11 additions & 9 deletions hledger/Hledger/Cli/Commands/Balance.hs
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,9 @@ import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Builder as TB
import Data.Time (fromGregorian)
import System.Console.CmdArgs.Explicit as C
import Lens.Micro ((^.))
import Lucid as L
import System.Console.CmdArgs.Explicit as C
import Text.Tabular.AsciiWide as Tab

import Hledger
Expand Down Expand Up @@ -310,16 +311,16 @@ balancemode = hledgerCommandMode

-- | The balance command, prints a balance report.
balance :: CliOpts -> Journal -> IO ()
balance opts@CliOpts{reportspec_=rspec} j = case reporttype_ of
balance copts j = case reporttype_ ropts of
BudgetReport -> do -- single or multi period budget report
let reportspan = reportSpan j rspec
budgetreport = budgetReport rspec (balancingopts_ $ inputopts_ opts) reportspan j
budgetreport = budgetReport rspec (copts ^. balancingOpts) reportspan j
render = case fmt of
"txt" -> budgetReportAsText ropts
"json" -> (<>"\n") . toJsonText
"csv" -> printCSV . budgetReportAsCsv ropts
_ -> error' $ unsupportedOutputFormatError fmt
writeOutputLazyText opts $ render budgetreport
writeOutputLazyText copts $ render budgetreport

_ | multiperiod -> do -- multi period balance report
let report = multiBalanceReport rspec j
Expand All @@ -329,7 +330,7 @@ balance opts@CliOpts{reportspec_=rspec} j = case reporttype_ of
"html" -> (<>"\n") . L.renderText . multiBalanceReportAsHtml ropts
"json" -> (<>"\n") . toJsonText
_ -> const $ error' $ unsupportedOutputFormatError fmt -- PARTIAL:
writeOutputLazyText opts $ render report
writeOutputLazyText copts $ render report

_ -> do -- single period simple balance report
let report = balanceReport rspec j -- simple Ledger-style balance report
Expand All @@ -339,11 +340,12 @@ balance opts@CliOpts{reportspec_=rspec} j = case reporttype_ of
-- "html" -> \ropts -> (<>"\n") . L.renderText . multiBalanceReportAsHtml ropts . balanceReportAsMultiBalanceReport ropts
"json" -> const $ (<>"\n") . toJsonText
_ -> error' $ unsupportedOutputFormatError fmt -- PARTIAL:
writeOutputLazyText opts $ render ropts report
writeOutputLazyText copts $ render ropts report
where
ropts@ReportOpts{..} = reportopts_ rspec
multiperiod = interval_ /= NoInterval
fmt = outputFormatFromOpts opts
rspec = copts ^. reportSpec
ropts = reportopts_ rspec
multiperiod = interval_ ropts /= NoInterval
fmt = outputFormatFromOpts copts

-- XXX this allows rough HTML rendering of a flat BalanceReport, but it can't handle tree mode etc.
-- -- | Convert a BalanceReport to a MultiBalanceReport.
Expand Down
4 changes: 2 additions & 2 deletions hledger/Hledger/Cli/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ journalAddForecast CliOpts{inputopts_=iopts, reportspec_=rspec} j =
(if auto_ iopts then either error' id . modifyTransactions today (jtxnmodifiers j) else id) -- PARTIAL:
forecasttxns

journalBalanceTransactions' iopts j =
either error' id $ journalBalanceTransactions (balancingopts_ iopts) j -- PARTIAL:
journalBalanceTransactions' iopts =
either error' id . journalBalanceTransactions (balancingopts_ iopts) -- PARTIAL:

-- | Write some output to stdout or to a file selected by --output-file.
-- If the file exists it will be overwritten.
Expand Down

0 comments on commit 9693f6b

Please sign in to comment.