From fefa371e18b268a788dd0373029e00793b6e9817 Mon Sep 17 00:00:00 2001 From: Juerd Waalboer Date: Sun, 29 Jan 2023 23:05:52 +0100 Subject: [PATCH] Move code around With the weird hack gone (see previous commit), the code could be written in a more straight-forward order, with some duplication removed. --- plugins/repeat | 48 +++++++++++++++++++++--------------------------- 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/plugins/repeat b/plugins/repeat index 8a13853..5588bed 100644 --- a/plugins/repeat +++ b/plugins/repeat @@ -19,11 +19,24 @@ sub command($self, $cart, $command, @) { my $last = ($cart->entries)[-1]; - return NEXT if $lhs and $rhs; # 123x123 -> invalid syntax + return NEXT if $lhs and $rhs; # 123x123 -> invalid, likely user or product + + if ($lhs) { + return REJECT, $err_postfix if $op eq '+' or $op eq '-'; + + $lhs = abs $lhs; # withdrawal is negative + + return REJECT, $err_limit if $lhs > $limit; + $cart + ->add(0, "? (The next thing you add will be multiplied.)", { _repeat => 1, refuse_checkout => 1 }) + ->quantity($lhs); + return ACCEPT; + } + + return ABORT, "Can't modify an empty transaction." if not $cart->size; + return REJECT, $err_nope if $last->attribute('no_repeat'); if ($rhs) { - return ABORT, "Can't modify an empty transaction." if not $cart->size; - return REJECT, $err_nope if $last->attribute('no_repeat'); return REJECT, $err_limit if $rhs > $limit; if ($op eq '+') { @@ -43,39 +56,20 @@ sub command($self, $cart, $command, @) { } return ACCEPT; } + # $op is not + or -, so it must be * (or x). - return REJECT, $err_stacked if $last->multiplied; - $last->quantity($rhs); return ACCEPT; } - if (not $lhs and not $rhs) { - # Lone operator. Convert withdrawal into repetition. - - return ABORT, "Can't modify an empty transaction." if not $cart->size; - - if ($op eq '+' or $op eq '-') { - $self->{op} = $op; - return "$op how many?", \&plusminus; - } - } - - if ($lhs) { - return REJECT, $err_postfix if $op eq '+' or $op eq '-'; - - $lhs = abs $lhs; # withdrawal is negative - - return REJECT, $err_limit if $lhs > $limit; - $cart - ->add(0, "? (The next thing you add will be multiplied.)", { _repeat => 1, refuse_checkout => 1 }) - ->quantity($lhs); - return ACCEPT; + if ($op eq '+' or $op eq '-') { + $self->{op} = $op; + return "$op how many?", \&plusminus; } + # $op is not + or -, so it must be * (or x). return REJECT, $err_stacked if $last->multiplied; - return REJECT, $err_nope if $last->attribute('no_repeat'); return "Multiply previous product by", \&repeat; }