-
Notifications
You must be signed in to change notification settings - Fork 13
/
watchdiff
executable file
·143 lines (114 loc) · 3.03 KB
/
watchdiff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/perl
#
# watchdiff - like watch(1), but output in diff format
#
# Copyright (c) 2010 Adam Spiers <adam@spiers.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# See also dwatch, also by me, which does a very similar thing but
# caches in order to allow diffs to span invocations.
use strict;
use warnings;
use File::Copy;
use File::Spec;
use File::Temp qw(tempfile);
use Getopt::Long;
use IPC::Open2;
use Time::HiRes qw(sleep);
$| = 1;
my $DEFAULT_INTERVAL = 2.0;
my $tmpdir = File::Spec->tmpdir;
my ($old_fh, $old_fn) = tempfile("watchdiff-$$-old-XXXXXXXXXXXXXXX", DIR => $tmpdir);
my ($new_fh, $new_fn) = tempfile("watchdiff-$$-new-XXXXXXXXXXXXXXX", DIR => $tmpdir);
$SIG{INT} = \&clean_up;
my $columns = $ENV{COLUMNS} || (`stty -a -F /dev/stderr` =~ /columns (\d+)/)[0];
sub usage {
warn @_, "\n" if @_;
(my $ME = $0) =~ s,.*/,,;
die <<EOUSAGE;
Usage: $ME [OPTIONS] CMD [ARGS]
Options:
-h, --help Show this help
-n, --interval=N Run command every N seconds (default $DEFAULT_INTERVAL)
-d, --clear Clear screen before displaying each diff
-c, --cumulative Show cumulative differences
EOUSAGE
}
sub parse_opts {
Getopt::Long::Configure('require_order');
my %opts = ( interval => $DEFAULT_INTERVAL );
GetOptions(
\%opts,
'help|h',
'interval|n=f',
'clear|d',
) or usage();
usage() if @ARGV == 0 or $opts{help};
return %opts;
}
sub run_command {
my @args = @_;
my ($stdin, $stdout);
my $pid = open2($stdout, $stdin, @args);
seek $new_fh, 0, 0;
truncate $new_fh, 0;
print $new_fh $_ while <$stdout>;
waitpid $pid, 0;
}
sub delimiter {
print "=" x ($columns-1), "\n";
}
{
my $diff_count = 0;
sub do_comparison {
my ($opts) = @_;
my $cmd = "diff -u $old_fn $new_fn";
my $diff = `$cmd`;
my $status = $? >> 8;
die "$cmd failed; aborting.\n" if $status == 2;
my $changed = $status == 1;
return unless $changed;
if ($opts->{clear}) {
system 'clear';
}
else {
delimiter() if $diff_count++;
}
print $diff;
}
}
sub main {
my %opts = parse_opts();
my $count = 0;
while (1) {
run_command(@ARGV);
if ($count > 0) {
do_comparison(\%opts);
}
else {
system 'cat', $new_fn;
delimiter();
}
copy($new_fn, $old_fn) if ! $opts{cumulative} or $count == 0;
$count++;
sleep $opts{interval};
}
}
sub clean_up {
close $old_fh;
close $new_fh;
unlink $old_fn, $new_fn;
exit;
}
main();