From a7a0b72546eb1510600e8d3c28b1c35e5ebfb338 Mon Sep 17 00:00:00 2001 From: wowaname Date: Sun, 6 Nov 2016 22:50:52 +0000 Subject: [PATCH] play.pl: -find limit and detect empty files --- scripts/play.pl | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/scripts/play.pl b/scripts/play.pl index 402878e..b01b4ac 100644 --- a/scripts/play.pl +++ b/scripts/play.pl @@ -5,7 +5,7 @@ no strict 'subs'; my $SCRIPT_NAME = 'play'; my $SCRIPT_AUTHOR = 'The Krusty Krab '; -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 () { no warnings; # sprintf barks if there's nothing to replace