App-sdif

 view release on metacpan or  search on metacpan

lib/App/watchdiff.pm  view on Meta::CPAN

	  decrc {DECRC}
     );

sub run {
    $app = my $opt = shift;
    local @ARGV = @_;

    use Getopt::EX::Long;
    Getopt::Long::Configure(qw(bundling require_order));
    $opt->getopt or usage({status => 1});

    if ($opt->context and $opt->context < 100) {
	$opt->verbose //= 1;
    }

    use Getopt::EX::Colormap;
    my $cm = Getopt::EX::Colormap
	->new(HASH => \%colormap)
	->load_params(@{$opt->colormap});

    if (@ARGV) {
	push @{$opt->exec}, [ @ARGV ];
    } else {
	@{$opt->exec} or pod2usage();
    }

    setup_terminal();
    $SIG{INT} = sub { exit };
    $opt->do_loop();
}

END {
    reset_terminal();
}

sub control_scroll {
    my $opt = shift;
    $opt->scroll && $opt->date && $opt->refresh == 1;
}

sub setup_terminal {
    if ($app and $app->control_scroll) {
	STDOUT->printflush(csi_code(STBM => 3, 999));
    }
}

sub reset_terminal {
    if ($app and $app->control_scroll) {
	STDOUT->printflush($termcap{decsc},
			   csi_code(STBM =>),
			   $termcap{decrc});
    }
}

##
## Run all commands given by --exec and concatenate their output.
##
package App::watchdiff::Command {
    use parent 'Command::Run';
    use Time::localtime;
    sub update {
	my $obj = shift;
	$obj->data(join "\n",
		   map { $obj->execute($_)->{data} // '' } $obj->command);
	$obj->date(ctime());
	$obj;
    }
}

sub do_loop {
    my $opt = shift;

    my $old = App::watchdiff::Command->new(command => $opt->exec);
    my $new = App::watchdiff::Command->new(command => $opt->exec);

    my @default_diff = (
			qw(cdif --no-unknown),
			map { ('--cm', "$_=$colormap{$_}") } sort keys %colormap
		       );

    my @diffcmd = do {
	if ($opt->diff) {
	    use Text::ParseWords;
	    shellwords $opt->diff;
	} else {
	    ( @default_diff,
	      map  { ref $_->[1] eq 'CODE' ? $_->[1]->() : $_->[1] }
	      grep { $_->[0] }
	      [ defined $opt->unit    => sub { '--unit=' . $opt->unit } ],
	      [ defined $opt->context => sub { '-U' . $opt->context } ],
	      [ ! $opt->verbose       => '--no-command' ],
	      [ ! $opt->mark          => '--no-mark' ],
	      [ ! $opt->old           => '--no-old' ],
	    );
	}
    };

    print $termcap{clear} if $opt->refresh;
    my $count = 0;
    my $refresh_count = 0;
    while (1) {
	$old->rewind;
	$new->update;
	my $data = execute(@diffcmd, $old->path, $new->path) // die "diff: $!\n";
	if ($data eq '') {
	    if ($opt->silent) {
		flush($new->date, "\r");
		next;
	    }
	    $data = $new->data;
	    $data =~ s/^/ /mg if $opt->mark;
	}
	$data .= "\n" if $opt->newline;
	if ($opt->refresh) {
	    $data =~ s/^/$termcap{el}/mg;
	    if ($refresh_count++ % $opt->refresh == 0) {
		print $termcap{clear};
	    }
	}
	print $new->date, "\n\n" if $opt->date;
	print $data;



( run in 2.619 seconds using v1.01-cache-2.11-cpan-364913b4093 )