play.pl: -find limit and detect empty files

This commit is contained in:
wowaname 2016-11-06 22:50:52 +00:00
parent 2ae017aba6
commit a7a0b72546
1 changed files with 15 additions and 4 deletions

View File

@ -5,7 +5,7 @@ no strict 'subs';
my $SCRIPT_NAME = 'play';
my $SCRIPT_AUTHOR = 'The Krusty Krab <wowaname@volatile.ch>';
my $SCRIPT_VERSION = '1.1';
my $SCRIPT_VERSION = '1.2';
my $SCRIPT_LICENCE = 'Public domain';
my $SCRIPT_DESC = 'Play ASCII art';
our (%queue, %timer);
@ -28,6 +28,8 @@ if (weechat::register($SCRIPT_NAME, $SCRIPT_AUTHOR, $SCRIPT_VERSION,
delay => ['Default delay between lines', 0],
dir => ['Art directory',
weechat::info_get('weechat_dir', '').'/ascii'],
find_limit => ['Maximum number of results returned by -find. '.
'-1 = unlimited (may lock up WeeChat with many results!)', 32],
);
for my $option (keys %OPTIONS) {
@ -99,11 +101,17 @@ sub cmd_play {
: weechat::config_integer($prio_s)
) or 10);
my $rule = File::Find::Rule->file->name($file)
my $rule = File::Find::Rule
->file
->name($file)
->start(weechat::config_get_plugin('dir'));
if ($find) {
weechat::print($buffer, " \t$_") while defined( $_ = $rule->match );
my $i = weechat::config_get_plugin('find_limit');
weechat::print($buffer, " \t$_")
while defined( $_ = $rule->match ) and --$i;
weechat::print($buffer, weechat::prefix('error').
"Too many results; please narrow your search") unless $i;
weechat::print($buffer, " \tEnd of file listing for '$file'");
return weechat::WEECHAT_RC_OK;
}
@ -112,7 +120,10 @@ sub cmd_play {
if ($file =~ m"/") { $path = weechat::config_get_plugin('dir')."/$file" }
else { $path = $rule->match }
if ($path and open FH, "<", $path) {
if ($path and -z $path) {
weechat::print($buffer, weechat::prefix('error').
"File '$file' is empty");
} elsif ($path and open FH, "<", $path) {
my @lines;
while (<FH>) {
no warnings; # sprintf barks if there's nothing to replace