Debian Codegraph @

bin/pmat-counts

raw
1 #!/usr/bin/perl
2
3 use v5.14;
4 use warnings;
5
6 use Devel::MAT;
7 use Devel::MAT::Cmd::Terminal;
8 use Commandable::Invocation 0.03;
9
10 use Devel::MAT::Tool::Count;
11
12 use constant TAG_INCR => fgindex => 2; # green
13 use constant TAG_DECR => fgindex => 1; # red
14
15 my $progress = ( -t STDERR ) ?
16 sub { print STDERR "\r\e[K";
17 print STDERR "$_[0]" if @_; } :
18 undef;
19
20 my $cinv = Commandable::Invocation->new_from_tokens( @ARGV );
21 my @args = Devel::MAT::Tool::Count->parse_cmd( $cinv );
22 ref $args[0] eq "HASH" or die "ARGH! Expected HASH as first parsed result\n";
23 my %opts = %{ +shift @args };
24
25 my %lastcount;
26
27 $opts{emit_count} = sub {
28 my ( $kind, $blessed, $count ) = @_;
29
30 my $lastcount = $lastcount{"$kind/$blessed"};
31 $lastcount{"$kind/$blessed"} = $count;
32
33 return "", "" unless $count || $lastcount;
34
35 my @ret = sprintf "%d", $count;
36
37 if( !defined $lastcount or $count == $lastcount ) {
38 push @ret, "";
39 }
40 elsif( $count > $lastcount ) {
41 push @ret,
42 String::Tagged->new_tagged( sprintf( "(+%d)", $count - $lastcount ), TAG_INCR );
43 }
44 else {
45 push @ret,
46 String::Tagged->new_tagged( sprintf( "(-%d)", $lastcount - $count ), TAG_DECR );
47 }
48
49 return @ret;
50 };
51
52 my %lastbytes;
53
54 $opts{emit_bytes} = sub {
55 my ( $kind, $blessed, $bytes ) = @_;
56
57 my $lastbytes = $lastbytes{"$kind/$blessed"};
58 $lastbytes{"$kind/$blessed"} = $bytes;
59
60 return "", "" unless $bytes || $lastbytes;
61
62 my @ret = Devel::MAT::Cmd->format_bytes( $bytes );
63
64 if( !defined $lastbytes or $bytes == $lastbytes ) {
65 push @ret, "";
66 }
67 elsif( $bytes > $lastbytes ) {
68 push @ret,
69 String::Tagged->from_sprintf( "(+%s)", Devel::MAT::Cmd->format_bytes( $bytes - $lastbytes ) )
70 ->apply_tag( 0, -1, TAG_INCR );
71 }
72 else {
73 push @ret,
74 String::Tagged->from_sprintf( "(-%s)", Devel::MAT::Cmd->format_bytes( $lastbytes - $bytes ) )
75 ->apply_tag( 0, -1, TAG_DECR );
76 }
77
78 return @ret;
79 };
80
81 $opts{table_args} = {
82 headings => [ "Kind", "Count","", "(blessed)","", "Bytes","", "(blessed)","" ],
83 sep => [ " ", ""," ","", " ", ""," ","" ],
84 align => [ undef, "right","left", "right","left", "right","left", "right","left" ],
85 };
86
87 while( my $file = $cinv->pull_token ) {
88 Devel::MAT::Cmd->printf( "%s\n",
89 Devel::MAT::Cmd->format_heading( $file, 2 )
90 );
91
92 my $pmat = Devel::MAT->load( $file, progress => $progress );
93 $progress->() if $progress;
94
95 $pmat->load_tool( "Count", progress => $progress )
96 ->count_svs( %opts );
97 }