revbank/plugins/warnings
Juerd Waalboer fa60e1081a chmod 644 plugins/*
Undoes 714b337 because github seems to no longer require chmod +x
for syntax highlighting extensionless files.
2019-08-07 15:42:16 +02:00

58 lines
1.5 KiB
Perl

#!perl
# The file format for 'revbank.warnings' is simply two whitespace separated
# columns: product id (or /regex/ for descriptions) and the warning text.
use Time::HiRes qw(sleep);
sub _read_warnings {
open my $fh, 'revbank.warnings' or die $!;
return map {
my ($regex, $products, $text) = m[^
(?:
/((?>[^/\\]++|\\.)*+)/ # /regex with support for \/escaped\/ slashes/
| (\S+) # products IDs, comma separated
)
\s+
(.*)
]x;
$regex
? sub {
my ($id, $desc) = @_;
$desc =~ /$regex/i ? $text : ();
}
: sub {
my ($id, $desc) = @_;
(grep { $_ eq $id } split /,/, $products) ? $text : ();
}
} grep /\S/, grep !/^\s*#/, readline $fh;
}
sub command { NEXT }
sub hook_add {
my ($class, $cart, $user, $item) = @_;
return if defined $user; # skip market items
return if not exists $item->{product_id}; # skip unlisted, deposit, give, take
my @warnings = map {
$_->( $item->{product_id}, $item->{description} )
} _read_warnings;
return if not @warnings;
my $text = join "\n", map ">> $_ <<", @warnings;
my $delay = 1.5 / length($text);
system "stty -echo 2>/dev/null";
print "\e[7m"; # reverse video
for my $c (split //, $text) {
print $c eq "\n" ? "\e[0m\n\e[7m" : $c;
sleep $delay;
}
print "\e[0m\n";
system "stty echo 2>/dev/null";
sleep .5;
}