From c163b514cc803fe707b339ef324bf8b3dfce58ee Mon Sep 17 00:00:00 2001 From: wowaname Date: Fri, 21 Oct 2016 20:10:17 +0000 Subject: [PATCH] play.pl: -fmt added, -stop fixed --- scripts/play.pl | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/scripts/play.pl b/scripts/play.pl index df0d8b5..961bfd7 100644 --- a/scripts/play.pl +++ b/scripts/play.pl @@ -13,10 +13,11 @@ our (%queue, %timer); if (weechat::register($SCRIPT_NAME, $SCRIPT_AUTHOR, $SCRIPT_VERSION, $SCRIPT_LICENCE, $SCRIPT_DESC, '', '')) { weechat::hook_command('play', 'Play ASCII art', - "[-delay ms] [-find] [-pipe \"command\"] filename\n-stop", + qq([-delay ms] [-find] [-pipe "command"] [-fmt "text"] filename\n-stop), "-delay: delay in milliseconds between lines\n". "-find: list matching files, don't play\n". "-pipe: pipe output into command\n". + "-fmt: treat file as a format string and replace with text\n". "filename: file to play. Supports wildcards. By default, searches\n". " subdirectories as well unless '/' is found in the filename\n". "-stop: stop currently playing file in buffer", @@ -36,8 +37,8 @@ if (weechat::register($SCRIPT_NAME, $SCRIPT_AUTHOR, $SCRIPT_VERSION, } sub parse { - my ($input, $delay, $pipe, $find, $repeat) = - (shift, weechat::config_get_plugin('delay'), '/msg *', 0, 1); + my ($input, $delay, $pipe, $find, $repeat, $fmt) = + (shift, weechat::config_get_plugin('delay'), '/msg *', 0, 1, ''); if ($input =~ / *-delay +([0-9]+) /) { $delay = $1; @@ -47,19 +48,22 @@ sub parse { $find = 1; $input =~ s/-find//; } + if ($input =~ / *-fmt +("(?:[^"\\]|\\.)+"|[^ ]+) /) { + $fmt = $1; + $fmt =~ s/^"(.+)"$/$1/ if $fmt =~ /^".+"$/; + $input =~ s/-fmt +("(?:[^"\\]|\\.)+"|[^ ]+)//; + } if ($input =~ / *-repeat +([0-9]+) /) { $repeat = $1; $input =~ s/-repeat +[0-9]+//; } - # greedy match within quotes so we don't need to escape quotes. - # i don't think we will be affected negatively by this - if ($input =~ / *-pipe +(".+"|[^ ]+) /) { + if ($input =~ / *-pipe +("(?:[^"\\]|\\.)+"|[^ ]+) /) { $pipe = $1; $pipe =~ s/^"(.+)"$/$1/ if $pipe =~ /^".+"$/; - $input =~ s/-pipe +(?:".+"|[^ ]+)//; + $input =~ s/-pipe +("(?:[^"\\]|\\.)+"|[^ ]+)//; } - return ($delay, $pipe, $find, $repeat, $input =~ s/^ +| +$//r); + return ($delay, $pipe, $find, $repeat, $fmt, $input =~ s/^ +| +$//r); } sub play { @@ -74,13 +78,15 @@ sub play { sub cmd_play { my $buffer = $_[1]; - if ($_[2] eq '-stop' and exists $timer{$buffer}) { - weechat::unhook($timer{$buffer}); - delete $queue{$buffer}; + if ($_[2] eq '-stop') { + if (exists $timer{$buffer}) { + weechat::unhook($timer{$buffer}); + delete $queue{$buffer}; + } return weechat::WEECHAT_RC_OK; } - my ($delay, $pipe, $find, $repeat, $file) = parse($_[2]); + my ($delay, $pipe, $find, $repeat, $fmt, $file) = parse($_[2]); my $server = weechat::info_get($buffer, 'localvar_server'); my ($prio_s, $prio_d) = ( weechat::config_get("irc.server.$server.anti_flood_prio_high"), @@ -107,7 +113,10 @@ sub cmd_play { if ($path and open FH, "<", $path) { my @lines; - push @lines, s/[\r\n]*$//r while ; + while () { + $_ = sprintf $_, split ' ', $fmt if $fmt; + push @lines, s/[\r\n]*$//r + } close FH; for (1 .. $repeat) { push @{ $queue{$buffer} }, "$pipe \x0f$_\x0f" for @lines;