FQStat/Drawing.pm untested

filter: Statementstmt 0.0% 0 / 261 Branchbran 0.0% 0 / 136 Conditioncond 0.0% 0 / 56 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 512 CC 69 SCAR 51.7
CC
69
Cov
0%
draw_job_display
506.0
draw_title_line
132.0
draw_summary
132.0
CRAP 175.6
1  
2 package App::FQStat::Drawing;
3 # App::FQStat is (c) 2007-2009 Steffen Mueller
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the same terms as Perl itself.
7  
8 use strict;
9 use warnings;
10 use Time::HiRes qw/sleep time/;
11 use Term::ANSIScreen qw/RESET locate clline cls/;
12 use App::FQStat::Debug;
13 use App::FQStat::Colors qw(get_color);
14  
15 use base 'Exporter';
16 our %EXPORT_TAGS = (
17 'all' => [qw(
18 printline
19 update_display
20 )],
21 );
22 our @EXPORT_OK = @{$EXPORT_TAGS{'all'}};
23  
24 # print line and buffer with space to line length
25 sub printline {
26 warnenter if ::DEBUG > 2;
27 my $line = shift;
28 my $offset = shift || 0;
29 $line .= ' ' x ($::Termsize[0]-length($line)-$offset);
30 print $line, "\n";
31 }
32  
33  
34 # draws the first, title line
35 sub draw_title_line {
36 warnenter if ::DEBUG > 1;
37 locate(1,1);
38 my $line;
39  
40 my $summary_mode = $::SummaryMode;
41  
42 if ($::MenuMode) {
43 print get_color("menu_normal");
44 $line = App::FQStat::Menu::get_menu_title_line();
45 }
46 elsif ($summary_mode) {
47 lock($::Interval);
48 my $progress = ::PROGRESS_INDICATORS()->[$::ProgressIndicator];
49 $progress = ' ' if not defined $progress;
50 $line = sprintf(
51 'fqstat v%.1f %s Jobs:%i Upd:%.1fs [S]witch [F10] Menu ' . get_color('header_highlight') . 'Summary Mode' . RESET . ', Nodes:%i',
52 $App::FQStat::VERSION||0,
53 $progress,
54 scalar(@{$::Records})||0,
55 $::Interval||0,
56 $::NoActiveNodes||0,
57 );
58 }
59 else {
60 lock($::RecordsReversed);
61 lock($::Interval);
62 lock($::User);
63  
64 # reversed list indicator
65 my $status = get_color('reverse_indicator').'[';
66 if ($::RecordsReversed) { $status .= 'R' }
67 if ($status =~ /\[$/) { $status = '' }
68 else { $status .= ']'.RESET.' ' }
69  
70 my $progress = ::PROGRESS_INDICATORS()->[$::ProgressIndicator];
71 $progress = ' ' if not defined $progress;
72 $line = sprintf(
73 'fqstat v%.1f %s %s%sJobs:%i Upd:%.1fs [h]elp [F10] Menu (c) S. Mueller, Nodes:%i',
74 $App::FQStat::VERSION||0,
75 $progress,
76 $status||'',
77 (defined($::User) ? "User:$::User " : ""),
78 scalar(@{$::Records})||0,
79 $::Interval||0,
80 $::NoActiveNodes||0,
81 );
82 }
83 ::GetTermSize();
84 $line = substr($line, 0, $::Termsize[0]) if $::Termsize[0] < length($line);
85 printline($line);
86 print RESET if $::MenuMode;
87 }
88  
89 # draws the column header line
90 sub draw_header_line {
91 warnenter if ::DEBUG > 1;
92 my @highlight = @_;
93 my %highlight = map {($_ => 1)} @highlight;
94  
95 # Header Line
96 locate(2,1);
97 my ($line, $width);
98  
99 my $high = get_color("header_highlight");
100 my $norm = get_color("header_normal");
101  
102 my $summary_mode = $::SummaryMode;
103 my $summary_clustering = App::FQStat::Config::get("summary_clustering");
104  
105 print $norm;
106 if (not $summary_mode) {
107 if (exists $highlight{1}) { $line = $high.'Stat'.$norm }
108 else { $line = 'Stat' }
109 }
110  
111 $width = 4;
112 my $colno = 1;
113 my $columns_list = $summary_mode ? \@::SummaryColumns : \@::Columns;
114 my $columns_hash = $summary_mode ? \%::SummaryColumns : \%::Columns;
115 foreach my $col (@$columns_list) {
116 my $c = $columns_hash->{$col};
117 $colno++;
118 next if $summary_mode and $c->{key} eq 'name' and not $summary_clustering;
119 $line .= (' 'x($::Termsize[0]-$width-1)).'>', last if $width+2+$c->{width} > $::Termsize[0];
120 $width += 1+$c->{width};
121 $line .= ' ' unless $colno == 2 and $summary_mode;
122 if (exists $highlight{$colno}) { $line .= $high.sprintf("\%-".$c->{width}."s", $c->{name}).$norm }
123 else { $line .= sprintf("\%-".$c->{width}."s", $c->{name}) }
124 }
125 printline($line);
126 print RESET;
127 }
128  
129  
130 my $last_update;
131 sub update_display {
132 warnenter if ::DEBUG;
133 my $force = shift;
134 $last_update ||= time();
135 my $time = time();
136  
137 if ($force or $time-$last_update > $::Interval) {
138 $last_update = $time;
139 $::ProgressIndicator++;
140 $::ProgressIndicator = 0
141 if $::ProgressIndicator >= scalar(@{::PROGRESS_INDICATORS()});
142 App::FQStat::Scanner::run_qstat($force);
143 }
144  
145 cls();
146 ::GetTermSize();
147  
148 draw_title_line(); # first line
149 draw_header_line(); # second line
150 my $summary_mode = $::SummaryMode;
151  
152 if ($summary_mode) {
153 draw_summary();
154 } else {
155 draw_job_display(); # list of jobs
156 }
157  
158 if ($::MenuMode) {
159 App::FQStat::Menu::draw_menu();
160 }
161  
162 locate(1,1);
163 }
164  
165 # Draws the job summary
166 sub draw_summary {
167 warnenter if ::DEBUG;
168  
169 # before there's any jobs, warn the user that the
170 # queue isn't actually empty.
171 if (not $::Initialized) {
172 draw_initializing_sign();
173 locate(1,1);
174 return;
175 }
176  
177 App::FQStat::Scanner::calculate_summary()
178 if not defined $::Summary or @$::Summary == 0;
179  
180 my $summary = $::Summary;
181 my $summary_clustering = App::FQStat::Config::get("summary_clustering");
182  
183 my $maxno_lines = space_for_jobs();
184  
185 my %status_color = (
186 nrun => get_color("status_running"),
187 nerr => get_color("status_error"),
188 nhold => get_color("status_hold"),
189 nwait => get_color("status_queued"),
190 );
191  
192 locate(3,1);
193 my $summary_color = get_color("summary");
194  
195 my $no = 0;
196  
197 foreach my $summaryLine (@$summary) {
198 $no++;
199 last if $no >= $maxno_lines;
200  
201 clline();
202 print $summary_color;
203  
204 my $width = 0;
205 my $first = 1;
206 my $too_short = 0;
207 foreach my $col (@::SummaryColumns) {
208 my $c = $::SummaryColumns{$col};
209  
210 # skip name column if not defined
211 next if $c->{key} eq 'name' and not $summary_clustering;
212  
213 $too_short = 1, last if $width+3+$c->{width} > $::Termsize[0];
214 $width += 1+$c->{width};
215 print ' ' unless $first;
216 $first = 0;
217 print $status_color{ $c->{key} } if exists $status_color{ $c->{key} };
218 printf( $c->{format}, $summaryLine->[ $c->{index} ] );
219 print $summary_color if exists $status_color{ $c->{key} };
220 }
221  
222 # padding for "position bar" of scroll bar
223 print ' 'x($::Termsize[0]-$width-1);
224  
225 if ($too_short) {
226 # not enough space
227 print '>';
228 }
229
230 print RESET;
231  
232 print "\n";
233 }
234  
235 locate(1,1);
236 }
237  
238 # Draws the list of jobs
239 # Optional argument: hash reference of items to highlight
240 sub draw_job_display {
241 warnenter if ::DEBUG;
242 my $highlight = shift || {};
243 my $mark = shift || {};
244  
245 # before there's any jobs, warn the user that the
246 # queue isn't actually empty.
247 if (not $::Initialized) {
248 draw_initializing_sign();
249 locate(1,1);
250 return;
251 }
252  
253 my $jobs = $::Records;
254 check_display_offset();
255 lock($::DisplayOffset);
256  
257 my $drawUpperScrollbar = need_upper_scrollbar();
258 draw_upper_scrollbar() if $drawUpperScrollbar;
259  
260 my $drawLowerScrollbar = need_lower_scrollbar();
261  
262
263 my $max_job_index = space_for_jobs() - $drawUpperScrollbar - $drawLowerScrollbar;
264  
265 locate(3+$drawUpperScrollbar,1);
266 my $last = (@$jobs-$::DisplayOffset <= $max_job_index ? @$jobs-1 : $::DisplayOffset + $max_job_index);
267  
268 # Calculate "scroll bar" marker
269 # XXX this is only necessary if there is enough space.
270 my $marker_start = 0;
271 my $marker_end = 1e99;
272 if (@$jobs) {
273 my $jobs_per_display_line = int(@$jobs / $max_job_index)||1;
274 my $display_lines_before_marker = int($::DisplayOffset / $jobs_per_display_line);
275 my $display_lines_of_marker = int(($last-$::DisplayOffset) / $jobs_per_display_line);
276 my $display_lines_after_marker = (@$jobs-$last) / $jobs_per_display_line;
277 $marker_start = int $display_lines_before_marker;
278 $marker_end = int($display_lines_before_marker+$display_lines_of_marker);
279 $marker_end += 1 if $marker_start == $marker_end;
280 }
281  
282 my $no = 0;
283 my %status_color = (
284 running => get_color("status_running"),
285 hold => get_color("status_hold"),
286 error => get_color("status_error"),
287 queued => get_color("status_queued"),
288 fallback => get_color("status_fallback"),
289 );
290  
291 foreach my $jobIndex ($::DisplayOffset .. $last) {
292 clline();
293 my $job = $jobs->[$jobIndex];
294 last if $no >= $max_job_index;
295  
296 # band-aid fix for uninit... bug
297 if (not defined $job) {
298 next;
299 }
300  
301 $no++;
302  
303 my $highlightcolor = $highlight->{$jobIndex};
304  
305 # highlight selected user name
306 $highlightcolor = get_color("user_highlight")
307 if not defined $highlightcolor
308 and defined $::HighlightUser
309 and $job->[::F_user] =~ $::HighlightUser;
310  
311 # Print STATUS
312 my $status = $job->[::F_status];
313 if ($status =~ /^[rt]$/) { print $status_color{running} }
314 elsif ($status =~ /(?:^d|E)/) { print $status_color{error} }
315 elsif ($status =~ /h(?:qw|r)/) { print $status_color{hold} }
316 elsif ($status =~ /w/) { print $status_color{queued} }
317 else { print $status_color{fallback} }
318  
319 printf('%-4s', $status);
320 print RESET;
321  
322 print $highlightcolor if defined $highlightcolor;
323  
324 # print marking if applicable
325 if (exists($mark->{$jobIndex})) {
326 my $markstring = $mark->{$jobIndex};
327 $markstring = "*" if not defined $markstring;
328 printf("%1s", $markstring);
329 }
330 else { print " " }
331
332 my $width = 4;
333 my $first = 1;
334 my $too_short = 0;
335 foreach my $col (@::Columns) {
336 my $c = $::Columns{$col};
337 $too_short = 1, last if $width+3+$c->{width} > $::Termsize[0];
338 $width += 1+$c->{width};
339 print ' ' unless $first;
340 $first = 0;
341 printf( $c->{format}, $job->[ $c->{index} ] );
342 }
343  
344 # padding for "position bar" of scroll bar
345 print ' 'x($::Termsize[0]-$width-1);
346  
347 if ($too_short) {
348 # not enough space
349 print '>';
350 }
351 else {
352 # draw "position bar" if there is enough space
353 my $marker;
354 my $bgcolor = get_color("scrollbar_bg");
355 my $fgcolor = get_color("scrollbar_fg");
356 if ($no >= $marker_start and $no <= $marker_end) {
357 $marker = $fgcolor . " " . RESET();
358 }
359 else {
360 $marker = $bgcolor . " " . RESET();
361 }
362 print $marker;
363 }
364
365 print RESET;
366  
367  
368 print "\n";
369 }
370  
371 draw_lower_scrollbar() if $drawLowerScrollbar;
372 locate(1,1);
373 }
374  
375 sub draw_upper_scrollbar {
376 warnenter if ::DEBUG > 1;
377 locate(3,1);
378 printline("^" x ($::Termsize[0]-2));
379 }
380  
381 sub draw_lower_scrollbar {
382 warnenter if ::DEBUG > 1;
383 locate($::Termsize[1]-1, 1);
384 printline("v" x ($::Termsize[0]-2));
385 }
386  
387 sub need_upper_scrollbar {
388 warnenter if ::DEBUG > 2;
389 return 1 if $::DisplayOffset;
390 return 0;
391 }
392  
393 sub need_lower_scrollbar {
394 warnenter if ::DEBUG > 2;
395 my $jobs = $::Records;
396 my $drawLowerScrollbar = (@$jobs-$::DisplayOffset > space_for_jobs()-need_upper_scrollbar() ? 1 : 0);
397 return $drawLowerScrollbar;
398 }
399  
400 sub space_for_jobs {
401 warnenter if ::DEBUG > 1;
402 my $space = $::Termsize[1]-3;
403 return $space;
404 }
405  
406 sub show_warning {
407 warnenter if ::DEBUG > 1;
408 my $text = shift;
409 # Header Line
410 locate(2,1);
411 my $color = get_color('header_warning');
412 printline($color.$text);
413 print RESET;
414 sleep 2;
415 return;
416 }
417  
418 sub check_display_offset {
419 lock($::DisplayOffset);
420 return if not $::DisplayOffset;
421 if (@$::Records <= $::DisplayOffset) {
422 $::DisplayOffset = $#{$::Records};
423 }
424 $::DisplayOffset = 0 if $::DisplayOffset < 0;
425 }
426  
427 sub draw_initializing_sign {
428 warnenter if ::DEBUG > 1;
429 locate(3,1);
430 my $color = get_color('initializing');
431 print $color, <<'HERE';
432
433
434 Initial Job Scan...
435
436
437 HERE
438 print RESET;
439  
440 }
441  
442 1;
443  
444