From 3ad313d8faae04c50c5975e0078d0d16c918cbc8 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Tue, 7 Jul 2020 13:38:06 -0700 Subject: [PATCH] codes: new command for listing transaction codes --- hledger/Hledger/Cli/Commands.hs | 4 +++ hledger/Hledger/Cli/Commands/Codes.hs | 42 ++++++++++++++++++++++ hledger/Hledger/Cli/Commands/Codes.md | 48 ++++++++++++++++++++++++++ hledger/Hledger/Cli/Commands/Codes.txt | 41 ++++++++++++++++++++++ hledger/hledger.m4.md | 4 +++ 5 files changed, 139 insertions(+) create mode 100644 hledger/Hledger/Cli/Commands/Codes.hs create mode 100644 hledger/Hledger/Cli/Commands/Codes.md create mode 100644 hledger/Hledger/Cli/Commands/Codes.txt diff --git a/hledger/Hledger/Cli/Commands.hs b/hledger/Hledger/Cli/Commands.hs index e15d38af5..cfdfe279e 100644 --- a/hledger/Hledger/Cli/Commands.hs +++ b/hledger/Hledger/Cli/Commands.hs @@ -27,6 +27,7 @@ module Hledger.Cli.Commands ( ,module Hledger.Cli.Commands.Checkdates ,module Hledger.Cli.Commands.Checkdupes ,module Hledger.Cli.Commands.Close + ,module Hledger.Cli.Commands.Codes ,module Hledger.Cli.Commands.Commodities ,module Hledger.Cli.Commands.Descriptions ,module Hledger.Cli.Commands.Diff @@ -72,6 +73,7 @@ import Hledger.Cli.Commands.Cashflow import Hledger.Cli.Commands.Checkdates import Hledger.Cli.Commands.Checkdupes import Hledger.Cli.Commands.Close +import Hledger.Cli.Commands.Codes import Hledger.Cli.Commands.Commodities import Hledger.Cli.Commands.Descriptions import Hledger.Cli.Commands.Diff @@ -107,6 +109,7 @@ builtinCommands = [ ,(checkdatesmode , checkdates) ,(checkdupesmode , checkdupes) ,(closemode , close) + ,(codesmode , codes) ,(commoditiesmode , commodities) ,(descriptionsmode , descriptions) ,(diffmode , diff) @@ -180,6 +183,7 @@ commandsList = unlines [ ," accounts (a) show account names" ," activity show postings-per-interval bar charts" ," balance (b, bal) show balance changes/end balances/budgets in accounts" + ," codes show transaction codes" ," commodities show commodity/currency symbols" ," descriptions show unique transaction descriptions" ," files show input file paths" diff --git a/hledger/Hledger/Cli/Commands/Codes.hs b/hledger/Hledger/Cli/Commands/Codes.hs new file mode 100644 index 000000000..11b4d6e0c --- /dev/null +++ b/hledger/Hledger/Cli/Commands/Codes.hs @@ -0,0 +1,42 @@ +{-| + +The @codes@ command lists the codes seen in transactions, in the order parsed. + +-} + +{-# LANGUAGE MultiWayIf #-} +{-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE TemplateHaskell #-} + +module Hledger.Cli.Commands.Codes ( + codesmode + ,codes +) where + +import qualified Data.Text as T +import qualified Data.Text.IO as T + +import Hledger +import Hledger.Cli.CliOptions + + +-- | Command line options for this command. +codesmode = hledgerCommandMode + $(embedFileRelative "Hledger/Cli/Commands/Codes.txt") + [] + [generalflagsgroup1] + hiddenflags + ([], Just $ argsFlag "[QUERY]") + +-- | The codes command. +codes :: CliOpts -> Journal -> IO () +codes CliOpts{reportopts_=ropts@ReportOpts{empty_}} j = do + d <- getCurrentDay + let q = queryFromOpts d ropts + ts = entriesReport ropts q j + codes = (if empty_ then id else filter (not . T.null)) $ + map tcode ts + + mapM_ T.putStrLn codes diff --git a/hledger/Hledger/Cli/Commands/Codes.md b/hledger/Hledger/Cli/Commands/Codes.md new file mode 100644 index 000000000..2005f6f71 --- /dev/null +++ b/hledger/Hledger/Cli/Commands/Codes.md @@ -0,0 +1,48 @@ +codes\ +List the codes seen in transactions, in the order parsed. + +_FLAGS + +This command prints the value of each transaction's code field, in the +order transactions were parsed. The transaction code is an optional +value written in parentheses between the date and description, often +used to store a cheque number, order number or similar. + +Transactions aren't required to have a code, and missing or empty codes +will not be shown by default. With the `-E`/`--empty` flag, they will +be printed as blank lines. + +You can add a query to select a subset of transactions. + +Examples: + +```journal +1/1 (123) + (a) 1 + +1/1 () + (a) 1 + +1/1 + (a) 1 + +1/1 (126) + (a) 1 +``` + +```shell +$ hledger codes +123 +124 +126 +``` + +```shell +$ hledger codes -E +123 +124 + + +126 +``` + diff --git a/hledger/Hledger/Cli/Commands/Codes.txt b/hledger/Hledger/Cli/Commands/Codes.txt new file mode 100644 index 000000000..ddc698414 --- /dev/null +++ b/hledger/Hledger/Cli/Commands/Codes.txt @@ -0,0 +1,41 @@ +codes +List the codes seen in transactions, in the order parsed. + +_FLAGS + +This command prints the value of each transaction's code field, in the +order transactions were parsed. The transaction code is an optional +value written in parentheses between the date and description, often +used to store a cheque number, order number or similar. + +Transactions aren't required to have a code, and missing or empty codes +will not be shown by default. With the -E/--empty flag, they will be +printed as blank lines. + +You can add a query to select a subset of transactions. + +Examples: + +1/1 (123) + (a) 1 + +1/1 () + (a) 1 + +1/1 + (a) 1 + +1/1 (126) + (a) 1 + +$ hledger codes +123 +124 +126 + +$ hledger codes -E +123 +124 + + +126 diff --git a/hledger/hledger.m4.md b/hledger/hledger.m4.md index 9831189eb..0739bb7a6 100644 --- a/hledger/hledger.m4.md +++ b/hledger/hledger.m4.md @@ -1581,6 +1581,10 @@ _include_({{Hledger/Cli/Commands/Checkdupes.md}}) _include_({{Hledger/Cli/Commands/Close.md}}) +## codes + +_include_({{Hledger/Cli/Commands/Codes.md}}) + ## commodities _include_({{Hledger/Cli/Commands/Commodities.md}})