From d54428b0920b8821d7466c0e3618e3b8d593aec4 Mon Sep 17 00:00:00 2001 From: Juerd Waalboer Date: Thu, 2 Nov 2023 05:22:56 +0100 Subject: [PATCH] Add support for user-accessible accounts that are excluded from grandtotal --- lib/RevBank/Plugin.pm | 8 ++++++-- lib/RevBank/Users.pm | 24 ++++++++++++++++++++++-- plugins/grandtotal | 2 +- revbank | 2 +- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/lib/RevBank/Plugin.pm b/lib/RevBank/Plugin.pm index f356a5a..a7e7c05 100644 --- a/lib/RevBank/Plugin.pm +++ b/lib/RevBank/Plugin.pm @@ -33,8 +33,12 @@ sub Tab($self, $method) { } if (delete $completions{USERS}) { - $completions{$_}++ for grep !RevBank::Users::is_hidden($_), - RevBank::Users::names(); + for my $name (RevBank::Users::names()) { + next if RevBank::Users::is_hidden($name); + + $completions{ $name }++; + $completions{ $1 }++ if $name =~ /^\*(.*)/; + } } return keys %completions; diff --git a/lib/RevBank/Users.pm b/lib/RevBank/Users.pm index 663c8a8..19e22b7 100644 --- a/lib/RevBank/Users.pm +++ b/lib/RevBank/Users.pm @@ -7,17 +7,33 @@ use experimental 'signatures'; # stable since v5.36 use RevBank::Global; use RevBank::Plugins; use Carp (); +use List::Util (); my $filename = "revbank.accounts"; sub _read() { my @users; /\S/ and push @users, [split " "] for slurp $filename; - return { map { lc($_->[0]) => $_ } @users }; + + my %users; + for (@users) { + my $name = $_->[0]; + if ($name =~ /^\*/) { + # user-accessible special account: support without * prefix + $users{ lc($name) =~ s/^\*//r } = $_; + + # also support literal account name with * prefix + $users{ lc($name) } = $_; + } else { + # hidden or normal account + $users{ lc($name) } = $_; + } + } + return \%users; } sub names() { - return map $_->[0], values %{ _read() }; + return List::Util::uniqstr map $_->[0], values %{ _read() }; } sub balance($username) { @@ -73,6 +89,10 @@ sub is_hidden($username) { return $username =~ /^[-+]/; } +sub is_special($username) { + return $username =~ /^[-+*]/; +} + sub parse_user($username) { return undef if is_hidden($username); diff --git a/plugins/grandtotal b/plugins/grandtotal index 1e4092c..04ddc22 100644 --- a/plugins/grandtotal +++ b/plugins/grandtotal @@ -10,7 +10,7 @@ sub command :Tab(grandtotal) ($self, $cart, $command, @) { for my $line (slurp 'revbank.accounts') { my ($username, $balance) = split " ", $line; - next if RevBank::Users::is_hidden($username); + next if RevBank::Users::is_special($username); my $credit = $balance; $neg += $credit if $credit < 0; diff --git a/revbank b/revbank index 655282f..f2d292a 100755 --- a/revbank +++ b/revbank @@ -17,7 +17,7 @@ use RevBank::Global; use RevBank::Messages; use RevBank::Cart; -our $VERSION = "4.1.1"; +our $VERSION = "4.2.0"; our %HELP1 = ( "abort" => "Abort the current transaction", );