From 8f84891b0c3d906c3949b19d948c8dbf3872fed7 Mon Sep 17 00:00:00 2001 From: Juerd Waalboer Date: Fri, 10 Jan 2014 21:17:43 +0100 Subject: [PATCH] Warn when someone leaves without paying. --- plugins/idle | 28 ++++++++++++++++++++++++++++ revbank | 40 ++++++++++++++++++++++++++++++++++------ revbank.plugins | 7 ++++--- 3 files changed, 66 insertions(+), 9 deletions(-) create mode 100755 plugins/idle diff --git a/plugins/idle b/plugins/idle new file mode 100755 index 0000000..2360fe3 --- /dev/null +++ b/plugins/idle @@ -0,0 +1,28 @@ +#!perl + +my $timeout = 10; + +sub command { NEXT } + +sub hook_prompt_idle { + my ($class, $cart, $plugin, $seconds, $readline) = @_; + if ($seconds >= $timeout and $cart->size and not $plugin) { + $readline->ding; + + return if $seconds > $timeout; # text only once + + my $text = $readline->copy_text; + my $point = $readline->{point}; + + $readline->save_prompt; + $readline->replace_line(""); + $readline->redisplay; + + print "\e[33;2;1mTransaction incomplete.\e[0m Enter username to pay/finish or 'abort' to abort.\n"; + + $readline->restore_prompt; + $readline->replace_line($text); + $readline->{point} = $point; + $readline->redisplay; + } +} diff --git a/revbank b/revbank index 7094f8b..7c8bf9b 100755 --- a/revbank +++ b/revbank @@ -2,8 +2,9 @@ use strict; use attributes; -use Term::ReadLine; +use IO::Select; use List::Util (); +use Term::ReadLine; use RevBank::Plugins; use RevBank::Global; use RevBank::Messages; @@ -20,19 +21,47 @@ $| = 1; my $readline = Term::ReadLine->new($0); $readline->ornaments('me,md,,'); +my $select = IO::Select->new; +$select->add(\*STDIN); + +my $cart = RevBank::Cart->new; + sub prompt { - my ($prompt, @completions) = @_; + my ($prompt, $plugins, @completions) = @_; $prompt =~ s/$/: /; $prompt =~ s/\?: $/? /; my @matches; $readline->Attribs->{completion_entry_function} = sub { - my ($word, $state) = @_; + my ($word, $state) = @_; @matches = grep /^\Q$word\E/i, @completions if $state == 0; return shift @matches; }; - my $input = $readline->readline($prompt); + + my $done; + my $input; + + $readline->callback_handler_install($prompt, sub { + $done = 1; + $input = shift; + $readline->callback_handler_remove; + }); + + my $begin = my $time = time; + while (not $done) { + $readline->callback_read_char if $select->can_read(.05); + if (time > $time) { + $time = time; + call_hooks( + "prompt_idle", + $cart, + (@$plugins > 1 ? undef : $plugins->[0]), # >1 plugin = main loop + $time - $begin, + $readline, + ); + } + } print "\e[0m"; defined $input or return; @@ -47,7 +76,6 @@ RevBank::Plugins->load; call_hooks("startup"); -my $cart = RevBank::Cart->new; my $old_cart_size = 0; my @words; @@ -99,7 +127,7 @@ OUTER: for (;;) { delete $completions{abort}; } - my $input = prompt $prompt, keys %completions; + my $input = prompt $prompt, \@plugins, keys %completions; call_hooks "input", $cart, $input, $split_input; diff --git a/revbank.plugins b/revbank.plugins index 8e21528..64566da 100644 --- a/revbank.plugins +++ b/revbank.plugins @@ -6,12 +6,13 @@ log # first, so that the registrations of other plugins are logged sigint restart help +idle undo give take -#nyan # fun, but not compatible with dumb terminals -#pfand # makes little sense in a self service environment -#stock +nyan # fun, but not compatible with dumb terminals +pfand # makes little sense in a self service environment +stock # Then, plugins that apply heuristics