revbank/plugins/products
Juerd Waalboer 7b088cb175 Tab completion improvements
A plugin can now specify &subroutine in the :Tab attribute; this sub will
be called as a method and can return a list of possible completions.
2013-02-28 16:04:36 +01:00

48 lines
1 KiB
Perl
Executable file

#!perl
HELP "<productID>" => "Look up products from database";
HELP "edit" => "Edit product list";
my $filename = 'revbank.products';
sub _read_products {
open my $fh, '<', $filename or die "$filename: $!";
my %products;
while (readline $fh) {
/^\s*#/ and next;
/\S/ or next;
chomp;
my ($ids, $p, $d) = split " ", $_, 3;
my @ids = split /,/, $ids;
$products{ $_ } = { id => $ids[0], price => $p, description => $d}
for @ids;
}
return \%products;
}
sub command :Tab(edit,&tab) {
my ($self, $cart, $command) = @_;
if ($command eq 'edit') {
system $ENV{EDITOR} || 'vi', $filename;
return ACCEPT;
}
my $product = _read_products->{ $command } or return NEXT;
my $price = parse_amount( $product->{price} ) or return NEXT;
$cart->add(
undef,
-$price,
$product->{description},
{ product_id => $product->{id} }
);
return ACCEPT;
}
sub tab {
return grep /\D/, keys %{ _read_products() };
}