App-FQStat
view release on metacpan or search on metacpan
lib/App/FQStat/Drawing.pm view on Meta::CPAN
package App::FQStat::Drawing;
# App::FQStat is (c) 2007-2009 Steffen Mueller
#
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
use strict;
use warnings;
use Time::HiRes qw/sleep time/;
use Term::ANSIScreen qw/RESET locate clline cls/;
use App::FQStat::Debug;
use App::FQStat::Colors qw(get_color);
use base 'Exporter';
our %EXPORT_TAGS = (
'all' => [qw(
printline
update_display
)],
);
our @EXPORT_OK = @{$EXPORT_TAGS{'all'}};
# print line and buffer with space to line length
sub printline {
warnenter if ::DEBUG > 2;
my $line = shift;
my $offset = shift || 0;
$line .= ' ' x ($::Termsize[0]-length($line)-$offset);
print $line, "\n";
}
# draws the first, title line
sub draw_title_line {
warnenter if ::DEBUG > 1;
locate(1,1);
my $line;
my $summary_mode = $::SummaryMode;
if ($::MenuMode) {
print get_color("menu_normal");
$line = App::FQStat::Menu::get_menu_title_line();
}
elsif ($summary_mode) {
lock($::Interval);
my $progress = ::PROGRESS_INDICATORS()->[$::ProgressIndicator];
$progress = ' ' if not defined $progress;
$line = sprintf(
'fqstat v%.1f %s Jobs:%i Upd:%.1fs [S]witch [F10] Menu ' . get_color('header_highlight') . 'Summary Mode' . RESET . ', Nodes:%i',
$App::FQStat::VERSION||0,
$progress,
scalar(@{$::Records})||0,
$::Interval||0,
$::NoActiveNodes||0,
);
}
else {
lock($::RecordsReversed);
lock($::Interval);
lock($::User);
# reversed list indicator
my $status = get_color('reverse_indicator').'[';
if ($::RecordsReversed) { $status .= 'R' }
if ($status =~ /\[$/) { $status = '' }
else { $status .= ']'.RESET.' ' }
my $progress = ::PROGRESS_INDICATORS()->[$::ProgressIndicator];
$progress = ' ' if not defined $progress;
$line = sprintf(
'fqstat v%.1f %s %s%sJobs:%i Upd:%.1fs [h]elp [F10] Menu (c) S. Mueller, Nodes:%i',
$App::FQStat::VERSION||0,
$progress,
$status||'',
(defined($::User) ? "User:$::User " : ""),
scalar(@{$::Records})||0,
$::Interval||0,
$::NoActiveNodes||0,
);
}
::GetTermSize();
$line = substr($line, 0, $::Termsize[0]) if $::Termsize[0] < length($line);
printline($line);
print RESET if $::MenuMode;
}
# draws the column header line
sub draw_header_line {
warnenter if ::DEBUG > 1;
my @highlight = @_;
my %highlight = map {($_ => 1)} @highlight;
# Header Line
locate(2,1);
my ($line, $width);
my $high = get_color("header_highlight");
my $norm = get_color("header_normal");
my $summary_mode = $::SummaryMode;
my $summary_clustering = App::FQStat::Config::get("summary_clustering");
print $norm;
if (not $summary_mode) {
if (exists $highlight{1}) { $line = $high.'Stat'.$norm }
else { $line = 'Stat' }
}
$width = 4;
my $colno = 1;
my $columns_list = $summary_mode ? \@::SummaryColumns : \@::Columns;
my $columns_hash = $summary_mode ? \%::SummaryColumns : \%::Columns;
foreach my $col (@$columns_list) {
my $c = $columns_hash->{$col};
$colno++;
next if $summary_mode and $c->{key} eq 'name' and not $summary_clustering;
$line .= (' 'x($::Termsize[0]-$width-1)).'>', last if $width+2+$c->{width} > $::Termsize[0];
$width += 1+$c->{width};
$line .= ' ' unless $colno == 2 and $summary_mode;
if (exists $highlight{$colno}) { $line .= $high.sprintf("\%-".$c->{width}."s", $c->{name}).$norm }
else { $line .= sprintf("\%-".$c->{width}."s", $c->{name}) }
}
printline($line);
print RESET;
}
my $last_update;
sub update_display {
warnenter if ::DEBUG;
my $force = shift;
$last_update ||= time();
my $time = time();
if ($force or $time-$last_update > $::Interval) {
$last_update = $time;
$::ProgressIndicator++;
$::ProgressIndicator = 0
if $::ProgressIndicator >= scalar(@{::PROGRESS_INDICATORS()});
App::FQStat::Scanner::run_qstat($force);
}
cls();
::GetTermSize();
draw_title_line(); # first line
draw_header_line(); # second line
my $summary_mode = $::SummaryMode;
if ($summary_mode) {
draw_summary();
} else {
draw_job_display(); # list of jobs
}
if ($::MenuMode) {
App::FQStat::Menu::draw_menu();
}
locate(1,1);
}
# Draws the job summary
sub draw_summary {
warnenter if ::DEBUG;
# before there's any jobs, warn the user that the
# queue isn't actually empty.
if (not $::Initialized) {
draw_initializing_sign();
locate(1,1);
return;
}
App::FQStat::Scanner::calculate_summary()
if not defined $::Summary or @$::Summary == 0;
my $summary = $::Summary;
my $summary_clustering = App::FQStat::Config::get("summary_clustering");
my $maxno_lines = space_for_jobs();
my %status_color = (
nrun => get_color("status_running"),
nerr => get_color("status_error"),
nhold => get_color("status_hold"),
nwait => get_color("status_queued"),
);
locate(3,1);
my $summary_color = get_color("summary");
my $no = 0;
foreach my $summaryLine (@$summary) {
$no++;
last if $no >= $maxno_lines;
clline();
print $summary_color;
my $width = 0;
my $first = 1;
my $too_short = 0;
foreach my $col (@::SummaryColumns) {
my $c = $::SummaryColumns{$col};
# skip name column if not defined
next if $c->{key} eq 'name' and not $summary_clustering;
$too_short = 1, last if $width+3+$c->{width} > $::Termsize[0];
$width += 1+$c->{width};
print ' ' unless $first;
$first = 0;
print $status_color{ $c->{key} } if exists $status_color{ $c->{key} };
printf( $c->{format}, $summaryLine->[ $c->{index} ] );
print $summary_color if exists $status_color{ $c->{key} };
( run in 0.765 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )