Merge branch 'master' of github.com:Juerd/revbank

This commit is contained in:
RevBar 2011-06-10 00:44:46 +02:00
commit 976c0e43da

43
revbank
View file

@ -3,6 +3,8 @@ use strict;
no warnings 'exiting'; # We'll exit subs with 'next' no warnings 'exiting'; # We'll exit subs with 'next'
use POSIX qw(strftime); use POSIX qw(strftime);
use List::Util qw(sum); use List::Util qw(sum);
require Term::ReadLine::Gnu;
use Term::ReadLine;
$SIG{INT} = 'IGNORE'; $SIG{INT} = 'IGNORE';
@ -47,10 +49,26 @@ select *TEE;
print "\e[0m\n\n\nWelcome to the RevBank Shell"; print "\e[0m\n\n\nWelcome to the RevBank Shell";
my $at = "ABORTING TRANSACTION."; my $at = "ABORTING TRANSACTION.";
my $readline = Term::ReadLine->new($0);
$readline->ornaments(0);
# For tab completion
my @commands = qw/
help wtf omgwtfbbq examples deposit take steal give undo
list ls edit restart
/;
sub prompt { sub prompt {
print "@_\e[1;4m"; my ($prompt, $completions) = @_;
my $input = readline *STDIN; $completions ||= [];
my @matches;
$readline->Attribs->{completion_entry_function} = sub {
my ($word, $state) = @_;
@matches = grep /^\Q$word\E/i, @$completions if $state == 0;
return shift @matches;
};
my $input = $readline->readline($prompt. "\e[1;4m");
logline(defined($input) ? $input : "\e[1;5mX\e[0m"); logline(defined($input) ? $input : "\e[1;5mX\e[0m");
@ -93,6 +111,7 @@ Valid commands:
examples Show some usage examples examples Show some usage examples
abort Abort the current transaction abort Abort the current transaction
edit Edit product list edit Edit product list
restart Attempt to restart the revbank shell
END END
return ['noop']; return ['noop'];
} }
@ -150,6 +169,10 @@ sub read_users {
return { map { lc($_->[0]) => $_ } @users }; return { map { lc($_->[0]) => $_ } @users };
} }
sub users {
map $_->[0], values %{ read_users() }
}
sub create_account { sub create_account {
my ($username) = @_; my ($username) = @_;
open my $fh, '>>revbank.accounts' or die $!; open my $fh, '>>revbank.accounts' or die $!;
@ -237,6 +260,9 @@ sub parse_command {
return shame(); return shame();
} elsif ($command =~ /^(?:edit)$/) { } elsif ($command =~ /^(?:edit)$/) {
return edit(); return edit();
} elsif ($command =~ /^(?:restart)$/) {
exec $0;
die "exec() failed. You'll have to restart revbank yourself :P\n";
} }
return; return;
} }
@ -274,7 +300,7 @@ sub give {
} }
if (not $user) { if (not $user) {
for (;;) { for (;;) {
my $input = prompt "Benificiary: "; my $input = prompt "Benificiary: ", [ users() ];
if ($p = parse_user($input)) { if ($p = parse_user($input)) {
$user = $p->[2]; $user = $p->[2];
last; last;
@ -326,8 +352,10 @@ sub take {
} }
while (not @users or not defined $amount) { while (not @users or not defined $amount) {
PROMPT: for (;;) { PROMPT: for (;;) {
my $input = prompt my $input = prompt(
"User to take from, or total amount to finish: "; "User to take from, or total amount to finish: ",
[ users() ]
);
if ($p = parse_user($input)) { if ($p = parse_user($input)) {
push @users, $p->[2]; push @users, $p->[2];
next PROMPT; next PROMPT;
@ -472,7 +500,10 @@ LINE: for (;;) {
summary(" ", $user, @todo); summary(" ", $user, @todo);
print "\nEnter username to pay/finish or 'abort' to abort.\n" print "\nEnter username to pay/finish or 'abort' to abort.\n"
} }
my $line = prompt "Product ID, amount or command: "; my $line = prompt(
"Product ID, amount or command: ",
[ @commands, users() ]
);
defined $line or exec $0; defined $line or exec $0;
my @line = split " ", $line; my @line = split " ", $line;
ELEMENT: while (@line) { ELEMENT: while (@line) {