Support control character escapes, add :AllChars attribute
Also: - fix warning in RevBank::Plugin->Tab when there are attrs but no :Tab - reconstruct quotes and escapes in prompt on retry
This commit is contained in:
parent
573731cb61
commit
0cd178d950
2 changed files with 34 additions and 5 deletions
|
@ -22,7 +22,7 @@ sub Tab($self, $method) {
|
||||||
ref $method ? $method : $self->can($method)
|
ref $method ? $method : $self->can($method)
|
||||||
) or return;
|
) or return;
|
||||||
|
|
||||||
my ($tab) = $attr =~ /Tab \( (.*?) \)/x;
|
my ($tab) = $attr =~ /Tab \( (.*?) \)/x or return;
|
||||||
for my $keyword (split /\s*,\s*/, $tab) {
|
for my $keyword (split /\s*,\s*/, $tab) {
|
||||||
if ($keyword =~ /^&(.*)/) {
|
if ($keyword =~ /^&(.*)/) {
|
||||||
my $method = $1;
|
my $method = $1;
|
||||||
|
@ -44,6 +44,14 @@ sub Tab($self, $method) {
|
||||||
return keys %completions;
|
return keys %completions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub AllChars($self, $method) {
|
||||||
|
my $attr = attributes::get(
|
||||||
|
ref $method ? $method : $self->can($method)
|
||||||
|
) or return;
|
||||||
|
|
||||||
|
return !!($attr =~ /AllChars/);
|
||||||
|
}
|
||||||
|
|
||||||
1;
|
1;
|
||||||
|
|
||||||
__END__
|
__END__
|
||||||
|
|
29
revbank
29
revbank
|
@ -17,7 +17,7 @@ use RevBank::Global;
|
||||||
use RevBank::Messages;
|
use RevBank::Messages;
|
||||||
use RevBank::Cart;
|
use RevBank::Cart;
|
||||||
|
|
||||||
our $VERSION = "5.0.1";
|
our $VERSION = "5.1.0";
|
||||||
our %HELP1 = (
|
our %HELP1 = (
|
||||||
"abort" => "Abort the current transaction",
|
"abort" => "Abort the current transaction",
|
||||||
);
|
);
|
||||||
|
@ -28,6 +28,9 @@ my @retry; # (@accepted, $rejected, [@trailing])
|
||||||
|
|
||||||
my $one_off = 0;
|
my $one_off = 0;
|
||||||
|
|
||||||
|
my %escapes = (a => "\a", r => "\r", n => "\n", t => "\t", 0 => "\0");
|
||||||
|
my %unescapes = reverse %escapes;
|
||||||
|
|
||||||
sub split_input($input) {
|
sub split_input($input) {
|
||||||
$input =~ s/\s+$//;
|
$input =~ s/\s+$//;
|
||||||
|
|
||||||
|
@ -57,7 +60,7 @@ sub split_input($input) {
|
||||||
return \$pos if $pos < length($input);
|
return \$pos if $pos < length($input);
|
||||||
|
|
||||||
# End of string reached
|
# End of string reached
|
||||||
s/\\(.)/$1/g for @terms;
|
s[\\(.)]{ $escapes{$1} // $1 }ge for @terms;
|
||||||
return @terms;
|
return @terms;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,7 +122,19 @@ sub prompt($prompt, $plugins, $completions) {
|
||||||
my @trailing = $word_based ? @{ pop @retry } : ();
|
my @trailing = $word_based ? @{ pop @retry } : ();
|
||||||
my @rejected = pop @retry;
|
my @rejected = pop @retry;
|
||||||
my @accepted = @retry;
|
my @accepted = @retry;
|
||||||
s/\0SEPARATOR/;/ for @accepted, @rejected, @trailing;
|
|
||||||
|
if ($word_based) {
|
||||||
|
# Reconstruct quotes and escapes
|
||||||
|
for (@accepted, @rejected, @trailing) {
|
||||||
|
s/([;'"\\])/\\$1/g;
|
||||||
|
s/\0SEPARATOR/;/;
|
||||||
|
|
||||||
|
my $unescapes = join "", keys %unescapes;
|
||||||
|
s/([$unescapes])/\\$unescapes{$1}/g;
|
||||||
|
$_ = "'$_'" if /\s/ or $_ eq "abort";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$readline->insert_text(
|
$readline->insert_text(
|
||||||
$word_based
|
$word_based
|
||||||
? join(" ", @accepted, @rejected, @trailing)
|
? join(" ", @accepted, @rejected, @trailing)
|
||||||
|
@ -251,7 +266,13 @@ OUTER: for (;;) {
|
||||||
"unexpected trailing input (use ';' to separate transactions)."
|
"unexpected trailing input (use ';' to separate transactions)."
|
||||||
);
|
);
|
||||||
|
|
||||||
my ($rv, @rvargs) = eval { $plugin->$method($cart, $word) };
|
my ($rv, @rvargs) =
|
||||||
|
|
||||||
|
($word =~ /[^\x20-\x7f]/ and $method eq 'command' || !$plugin->AllChars($method))
|
||||||
|
|
||||||
|
? (REJECT, "Unexpected control character in input.")
|
||||||
|
: eval { $plugin->$method($cart, $word) };
|
||||||
|
|
||||||
if ($@ isa 'RevBank::Cart::CheckoutProhibited') {
|
if ($@ isa 'RevBank::Cart::CheckoutProhibited') {
|
||||||
@words or die "Internal inconsistency"; # other cause than trailing input
|
@words or die "Internal inconsistency"; # other cause than trailing input
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue