play.pl: -fmt added, -stop fixed

This commit is contained in:
wowaname 2016-10-21 20:10:17 +00:00
parent 2f1d8d7064
commit c163b514cc

View File

@ -13,10 +13,11 @@ our (%queue, %timer);
if (weechat::register($SCRIPT_NAME, $SCRIPT_AUTHOR, $SCRIPT_VERSION, if (weechat::register($SCRIPT_NAME, $SCRIPT_AUTHOR, $SCRIPT_VERSION,
$SCRIPT_LICENCE, $SCRIPT_DESC, '', '')) { $SCRIPT_LICENCE, $SCRIPT_DESC, '', '')) {
weechat::hook_command('play', 'Play ASCII art', 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". "-delay: delay in milliseconds between lines\n".
"-find: list matching files, don't play\n". "-find: list matching files, don't play\n".
"-pipe: pipe output into command\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". "filename: file to play. Supports wildcards. By default, searches\n".
" subdirectories as well unless '/' is found in the filename\n". " subdirectories as well unless '/' is found in the filename\n".
"-stop: stop currently playing file in buffer", "-stop: stop currently playing file in buffer",
@ -36,8 +37,8 @@ if (weechat::register($SCRIPT_NAME, $SCRIPT_AUTHOR, $SCRIPT_VERSION,
} }
sub parse { sub parse {
my ($input, $delay, $pipe, $find, $repeat) = my ($input, $delay, $pipe, $find, $repeat, $fmt) =
(shift, weechat::config_get_plugin('delay'), '/msg *', 0, 1); (shift, weechat::config_get_plugin('delay'), '/msg *', 0, 1, '');
if ($input =~ / *-delay +([0-9]+) /) { if ($input =~ / *-delay +([0-9]+) /) {
$delay = $1; $delay = $1;
@ -47,19 +48,22 @@ sub parse {
$find = 1; $find = 1;
$input =~ s/-find//; $input =~ s/-find//;
} }
if ($input =~ / *-fmt +("(?:[^"\\]|\\.)+"|[^ ]+) /) {
$fmt = $1;
$fmt =~ s/^"(.+)"$/$1/ if $fmt =~ /^".+"$/;
$input =~ s/-fmt +("(?:[^"\\]|\\.)+"|[^ ]+)//;
}
if ($input =~ / *-repeat +([0-9]+) /) { if ($input =~ / *-repeat +([0-9]+) /) {
$repeat = $1; $repeat = $1;
$input =~ s/-repeat +[0-9]+//; $input =~ s/-repeat +[0-9]+//;
} }
# greedy match within quotes so we don't need to escape quotes. if ($input =~ / *-pipe +("(?:[^"\\]|\\.)+"|[^ ]+) /) {
# i don't think we will be affected negatively by this
if ($input =~ / *-pipe +(".+"|[^ ]+) /) {
$pipe = $1; $pipe = $1;
$pipe =~ s/^"(.+)"$/$1/ if $pipe =~ /^".+"$/; $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 { sub play {
@ -74,13 +78,15 @@ sub play {
sub cmd_play { sub cmd_play {
my $buffer = $_[1]; my $buffer = $_[1];
if ($_[2] eq '-stop' and exists $timer{$buffer}) { if ($_[2] eq '-stop') {
if (exists $timer{$buffer}) {
weechat::unhook($timer{$buffer}); weechat::unhook($timer{$buffer});
delete $queue{$buffer}; delete $queue{$buffer};
}
return weechat::WEECHAT_RC_OK; 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 $server = weechat::info_get($buffer, 'localvar_server');
my ($prio_s, $prio_d) = ( my ($prio_s, $prio_d) = (
weechat::config_get("irc.server.$server.anti_flood_prio_high"), weechat::config_get("irc.server.$server.anti_flood_prio_high"),
@ -107,7 +113,10 @@ sub cmd_play {
if ($path and open FH, "<", $path) { if ($path and open FH, "<", $path) {
my @lines; my @lines;
push @lines, s/[\r\n]*$//r while <FH>; while (<FH>) {
$_ = sprintf $_, split ' ', $fmt if $fmt;
push @lines, s/[\r\n]*$//r
}
close FH; close FH;
for (1 .. $repeat) { for (1 .. $repeat) {
push @{ $queue{$buffer} }, "$pipe \x0f$_\x0f" for @lines; push @{ $queue{$buffer} }, "$pipe \x0f$_\x0f" for @lines;