App-Prolix
view release on metacpan or search on metacpan
lib/App/Prolix.pm view on Meta::CPAN
# isa => "Term::ReadLine|Term::ReadLine::Perl|Term::ReadLine::Gnu");
has_rw "_snippet" => (isa => "ArrayRef", "default" => sub { [] });
has_rw "_ignore_re" => (isa => "ArrayRef", "default" => sub { [] });
has_counter "_suppressed";
has_counter "_output_lines";
sub run {
my($self) = @_;
if ($self->verbose) {
$SIG{USR1} = \&_dump_stack;
}
$self->open_log;
$self->import_re($_) for @{$self->ignore_re};
$self->import_snippet($_) for @{$self->snippet};
if ($self->need_pipe) {
$self->run_pipe;
} else {
$self->run_spawn;
}
if ($self->verbose) {
say "Done. " . $self->stats;
}
$self->close_log;
}
sub need_pipe {
my($self) = @_;
return $self->pipe || @{$self->_cmd} == 0;
}
sub open_log {
my($self) = @_;
return if not defined $self->log;
my $now = $self->now_stamp;
my $filename = $self->log;
$filename = ($self->need_pipe ? "prolix.%d" : ($self->_cmd->[0] . ".%d")) if
$filename eq "auto";
$filename = File::Spec->catfile(File::Spec->tmpdir, $filename) if
$filename !~ m{[/\\]}; # Put in /tmp/ or similar unless we got a path.
$filename =~ s/%d/$now/; # TODO(gaal): implement incrementing %n.
say "Logging output to $filename" if $self->verbose;
my $fh = IO::File->new($filename, "w") or die "open: $filename: $!";
$self->_log($fh);
}
sub close_log {
my($self) = @_;
$self->_log->close if $self->_log;
}
# Like: (DateTime->new->iso8601 =~ s/[-:]//g), but I didn't want to add
# a big dependency.
sub now_stamp {
my($self) = @_;
my(@t) = localtime; # Should this be gmtime?
return sprintf "%4d%02d%02dT%02d%02d%02d",
$t[5] + 1900, $t[4] + 1, $t[3], $t[2], $t[1], $t[0]; # Ahh, UNIX.
}
sub stats {
my($self) = @_;
return "Suppressed " . $self->_suppressed . "/" .
$self->_output_lines . " lines.";
}
# returns a fresh reference to a string.
sub _strref {
return \(my $throwaway = "");
}
sub run_pipe {
my($self) = @_;
say "Running in pipe mode" if $self->verbose;
while (<STDIN>) {
chomp;
$self->on_out($_)
}
}
sub run_spawn {
my($self) = @_;
say "Running: " .
String::ShellQuote::shell_quote_best_effort(@{$self->_cmd})
if $self->verbose;
Term::ReadKey::ReadMode("noecho");
END { Term::ReadKey::ReadMode("normal"); }
$self->_term(Term::ReadLine->new("prolix"));
my $attribs = $self->_term->Attribs;
$attribs->{completion_entry_function} =
$attribs->{list_completion_function};
$attribs->{completion_word} = [qw(
help
ignore_line
ignore_re
ignore_substring
pats
quit
snippet
stats
)];
my $t = IPC::Run::timer(0.3);
my $ipc = IPC::Run::start $self->_cmd,
\undef, # no stdin
$self->_out,
$self->_err,
( run in 1.883 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )