Skip to content

Commit 37c4578

Browse files
committed
imp:areg: respect display order when picking the first-matched account
This is more consistent with the accounts and balance reports, and allows more customisation of what 'areg SHORTNAME' matches.
1 parent db368ed commit 37c4578

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

hledger/Hledger/Cli/Commands/Aregister.hs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,13 @@ aregister opts@CliOpts{rawopts_=rawopts,reportspec_=rspec} j = do
8888
[] -> error' $ help <> ".\nPlease provide an account name or a (case-insensitive, infix, regexp) pattern."
8989
(a:as) -> return (a, map T.pack as)
9090
let
91-
-- keep synced with findMatchedByArgument's matching
91+
-- related: findMatchedByArgument
92+
-- Here, we select the first matched according to display order -
93+
-- ie the first declared if the accounts are declaraed, otherwise the alphanumerically first.
9294
acct = fromMaybe (error' $ help <> ",\nbut " ++ show apat++" did not match any account.") -- PARTIAL:
93-
. firstMatch $ journalAccountNamesDeclaredOrImplied j
95+
. firstMatch $
96+
sortAccountNamesByDeclaration j False $
97+
journalAccountNamesDeclaredOrImplied j
9498
firstMatch = case toRegexCI $ T.pack apat of
9599
Right re -> find (regexMatchText re)
96100
Left _ -> const Nothing

hledger/Hledger/Cli/Commands/Aregister.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ As a quick rule of thumb:
4242

4343
Note this command's non-standard, and required, first argument; it specifies the account whose register will be shown.
4444
You can write the account's name, or (to save typing) a case-insensitive infix regular expression matching the name,
45-
which selects the alphabetically first matched account.
46-
(For example, if you have `assets:personal checking` and `assets:business checking`,
47-
`hledger areg checking` would select `assets:business checking`.)
45+
which selects the first matched account (in [display order](hledger.md#account-display-order) if any, otherwise alphabetically).
46+
For example, if you have `assets:personal checking` and `assets:business checking`,
47+
`hledger areg checking` will select `assets:business checking`
48+
(unless you used `account` directives to change their display order).
4849

4950
Transactions involving subaccounts of this account will also be shown.
5051
`aregister` ignores depth limits, so its final total will always match

0 commit comments

Comments
 (0)