Indexer/Interface/Curses.pm untested

filter: Statementstmt 0.0% 0 / 117 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 154 CC 4 SCAR 11.5
CC
4
Cov
0%
BEGIN
6.0
do_interface
6.0
_update_values
6.0
CRAP 3.1
1 package MyCPAN::Indexer::Interface::Curses;
2 use strict;
3 use warnings;
4  
5 BEGIN {
6 my $rc = eval { require Curses; 1 };
7  
8 die "You need to install the Curses module to use MyCPAN::Indexer::Interface::Curses\n"
9 unless $rc;
10 Curses->import;
11 }
12  
13 use vars qw($VERSION $logger);
14 $VERSION = '1.282';
15  
16 =encoding utf8
17  
18 =head1 NAME
19  
20 MyCPAN::Indexer::Interface::Curses - Present the run info in a terminal
21  
22 =head1 SYNOPSIS
23  
24 Use this in backpan_indexer.pl by specifying it as the interface class:
25  
26 # in backpan_indexer.config
27 interface_class MyCPAN::Indexer::Interface::Curses
28  
29 =head1 DESCRIPTION
30  
31 This class presents the information as the indexer runs, using Curses.
32  
33 =head2 Methods
34  
35 =over 4
36  
37 =item do_interface( $Notes )
38  
39  
40 =cut
41  
42 BEGIN { $SIG{INT} = sub { exit } }
43  
44 BEGIN {
45 use Log::Log4perl;
46 $logger = Log::Log4perl->get_logger( 'Interface' );
47 }
48  
49 sub component_type { $_[0]->interface_type }
50  
51 sub do_interface
52 {
53 my( $self ) = @_;
54 $logger->debug( "Calling do_interface" );
55  
56 initscr();
57 noecho();
58 raw();
59  
60 $self->set_note( 'curses', {} );
61 my $curses = $self->get_note( 'curses' );
62  
63 $curses->{rows} = LINES();
64 $curses->{cols} = COLS();
65  
66 addstr( 0, 0, join " ", $Notes->{Config}{indexer_class}, $Notes->{Config}{indexer_class}->VERSION );
67 refresh();
68  
69 $curses->{windows}{progress} = newwin( 3, COLS(), 1, 0 );
70 $curses->{windows}{left_tracker} = newwin( 6, 20, 4, 0 );
71 $curses->{windows}{right_tracker} = newwin( 6, COLS() - 21, 4, 21 );
72 $curses->{windows}{PID} = newwin( 7, COLS(), 10, 0 );
73 $curses->{windows}{Errors} = newwin( 7, COLS(), 17, 0 );
74  
75 foreach my $value ( values %{ $curses->{windows} } )
76 {
77 box( $value, 0, 0 );
78 refresh( $value );
79 }
80  
81 my $count = 0;
82 while( 1 )
83 {
84 $self->get_note( 'interface_callback' )->();
85  
86 $self->_update_screen( $Notes );
87  
88 last if $self->get_note( 'Finished' );
89 }
90  
91 }
92  
93 {
94 no warnings;
95 my $labels = {
96 # Label, win, row, column, key, key length, value length
97 Total => [ qw(left_tracker 1 1 Total 6 6) ],
98 Done => [ qw(left_tracker 2 1 Done 6 6) ],
99 Left => [ qw(left_tracker 3 1 Left 6 6) ],
100 Errors => [ qw(left_tracker 4 1 Errors 6 6) ],
101  
102 UUID => [ qw(right_tracker 1 1 UUID 8 30) ],
103 Started => [ qw(right_tracker 2 1 Started 8 -30) ],
104 Elapsed => [ qw(right_tracker 3 1 Elapsed 8 -30) ],
105 Rate => [ qw(right_tracker 4 1 Rate 8 -30) ],
106 };
107  
108 my $headers = {
109 '##' => [ qw(PID 1 1 ## 2 0) ],
110 PID => [ qw(PID 1 5 PID 6 0) ],
111 Processing => [ qw(PID 1 13 Processing -40 0) ],
112  
113 ErrorList => [ qw(Errors 0 1 Errors 7 0) ],
114 };
115  
116 my $values = {};
117  
118 sub _update_screen
119 {
120 $_[0]->_update_labels;
121 $_[0]->_update_progress;
122 $_[0]->_update_values;
123 }
124  
125 sub _update_labels
126 {
127 my( $self ) = @_;
128  
129 #print "Calling _update_screen\n";
130  
131 my $curses = $self->get_note( 'curses' );
132  
133 foreach my $key ( keys %$labels )
134 {
135 my $tuple = $labels->{$key};
136  
137 eval { addstr(
138 $curses->{windows}{ $tuple->[0] },
139 @$tuple[1,2,3]
140 );
141 refresh( $curses->{windows}{ $tuple->[0] } );
142 };
143 }
144  
145 foreach my $key ( keys %$headers )
146 {
147 my $tuple = $headers->{$key};
148  
149 eval {
150  
151 my $width = $tuple->[4];
152 addstr(
153 $curses->{windows}{ $tuple->[0] },
154 @$tuple[1,2],
155 sprintf "%${width}s", $tuple->[3]
156 );
157 refresh( $curses->{windows}{ $tuple->[0] } );
158 };
159 }
160  
161  
162 foreach my $i ( 1 .. $Notes->{Threads} )
163 {
164 no warnings;
165 my $width = $headers->{'##'}[4];
166 addstr(
167 $curses->{windows}{PID},
168 $i + 1,
169 $headers->{'##'}[2] + 1,
170 sprintf "%${width}s", $i );
171 refresh( $curses->{windows}{PID} );
172 }
173  
174 refresh( $curses->{windows}{PID} );
175 }
176  
177 sub _update_progress
178 {
179 my( $self ) = @_;
180  
181 my $curses = $self->get_note( 'curses' );
182  
183 my $progress = eval { ( COLS() - 2 ) / $Notes->{Total} * $Notes->{Done} } || 0;
184  
185 addstr(
186 $curses->{windows}{progress},
187 1, 1,
188 '*' x $progress
189 );
190 refresh( $curses->{windows}{progress} );
191 }
192  
193 sub _update_values
194 {
195 my( $self ) = @_;
196  
197 my $curses = $self->get_note( 'curses' );
198  
199 no warnings;
200 foreach my $key ( qw(Total Done Left Errors UUID Started Elapsed Rate) )
201 {
202 my $tuple = $labels->{$key};
203  
204 addstr(
205 $curses->{windows}{ $tuple->[0] },
206 $tuple->[1],
207 $tuple->[2] + $tuple->[4] + 2,
208 sprintf "%" . $tuple->[5] . "s", $self->get_notes( $tuple->[3] );
209 );
210 refresh( $curses->{windows}{ $tuple->[0] } );
211 }
212  
213 foreach my $i ( 1 .. $self->get_note( 'Threads' ) )
214 {
215 my $width = $headers->{PID}[4];
216 addstr(
217 $curses->{windows}{PID},
218 $i + 1, $headers->{PID}[2],
219 sprintf "%${width}s", $self->get_note('PID')->[$i-1]
220 );
221  
222 $width = COLS() - $headers->{Processing}[2] - 1;
223 addstr(
224 $curses->{windows}{PID},
225 $i + 1,
226 $headers->{Processing}[2],
227 ' ' x ( COLS() - $headers->{Processing}[2] - 1 )
228 );
229  
230 my $recent = $self->get_note( 'recent' );
231  
232 addstr(
233 $curses->{windows}{PID},
234 $i + 1,
235 $headers->{Processing}[2],
236 sprintf "%-${width}s", substr(
237 (defined $recent->[$i-1] ? $recent->[$i-1] : ''), 0, $width )
238 );
239  
240 refresh( $curses->{windows}{PID} );
241 }
242  
243 }
244 }
245  
246 END { endwin() }
247  
248 =back
249  
250  
251 =head1 SEE ALSO
252  
253 MyCPAN::Indexer
254  
255 =head1 SOURCE AVAILABILITY
256  
257 This code is in Github:
258  
259 git://github.com/briandfoy/mycpan-indexer.git
260  
261 =head1 AUTHOR
262  
263 brian d foy, C<< <bdfoy@cpan.org> >>
264  
265 =head1 COPYRIGHT AND LICENSE
266  
267 Copyright © 2008-2018, brian d foy <bdfoy@cpan.org>. All rights reserved.
268  
269 You may redistribute this under the terms of the Artistic License 2.0.
270  
271 =cut
272  
273 1;