| 1 | package Log::Progress::RenderTTY; | ||||||||||||||||||||||||||||||||
| 2 | 1 | $Log::Progress::RenderTTY::VERSION = '0.13'; | |||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Moo 2; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use IO::Handle; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Log::Progress::Parser; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Term::Cap; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Scalar::Util; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | # ABSTRACT: Render progress state on a terminal | ||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | 1 | has listen_resize => ( is => 'ro' ); | |||||||||||||||||||||||||||||||
| 15 | 1 | has tty_metrics => ( is => 'lazy', clearer => 1 ); | |||||||||||||||||||||||||||||||
| 16 | 1 | has termcap => ( is => 'lazy' ); | |||||||||||||||||||||||||||||||
| 17 | 1 | has parser => ( is => 'rw' ); | |||||||||||||||||||||||||||||||
| 18 | 1 | has out => ( is => 'rw' ); | |||||||||||||||||||||||||||||||
| 19 | 1 | has _prev_output => ( is => 'rw' ); | |||||||||||||||||||||||||||||||
| 20 | 1 | has _winch_handler => ( is => 'rw' ); | |||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | 1 | ▶ | sub _build_tty_metrics { | ||||||||||||||||||||||||||||||
|
Subroutine
_build_tty_metrics: called CC=7 SCAR=22.1
| |||||||||||||||||||||||||||||||||
| 23 | 1 | my $self= shift; | |||||||||||||||||||||||||||||||
| 24 | 1 | ▶ | my $stty= `stty -a` or warn "unable to run 'stty -a' to fetch terminal size\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 25 | 1 | my ($speed)= ($stty =~ /speed[ =]+(\d+)/); | |||||||||||||||||||||||||||||||
| 26 | 1 | my ($cols)= ($stty =~ /columns[ =]+(\d+)/); | |||||||||||||||||||||||||||||||
| 27 | 1 | my ($rows)= ($stty =~ /rows[ =]+(\d+)/); | |||||||||||||||||||||||||||||||
| 28 | 1 | ▶ | $self->_init_window_change_watch() if $self->listen_resize and $cols; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->listen_resize and $cols
MC/DC0%0/2
$self->listen_resize and $cols
$self->listen_resize
$cols
Truth table33%1/3
A and B
A= $self->listen_resizeB= $cols
| |||||||||||||||||||||||||||||||||
| 29 | 1 | ▶ | return { speed => $speed || 9600, cols => $cols || 80, rows => $rows || 25 }; | ||||||||||||||||||||||||||||||
|
Condition50%3/6
$speed || 9600
$cols || 80
$rows || 25
MC/DC0%0/1
$speed || 9600
$speed
MC/DC0%0/1
$cols || 80
$cols
MC/DC0%0/1
$rows || 25
$rows
Truth table50%1/2
A
A= $speed
Truth table50%1/2
A
A= $cols
Truth table50%1/2
A
A= $rows
| |||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | 1 | ▶ | sub _build_termcap { | ||||||||||||||||||||||||||||||
|
Subroutine
_build_termcap: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||
| 33 | 1 | my $self= shift; | |||||||||||||||||||||||||||||||
| 34 | 1 | ▶ | my $speed= $self->tty_metrics->{speed} || 9600; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->tty_metrics->{'speed'} || 9600
MC/DC0%0/1
$self->tty_metrics->{'speed'} || 9600
$self->tty_metrics->{'speed'}
Truth table50%1/2
A
A= $self->tty_metrics->{'speed'}
| |||||||||||||||||||||||||||||||||
| 35 | 1 | return Tgetent Term::Cap { TERM => '', OSPEED => $speed }; | |||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | sub _init_window_change_watch { | ||||||||||||||||||||||||||||||
|
Subroutine
_init_window_change_watch: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 39 | 0 | my $self= shift; | |||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | return if defined $self->_winch_handler; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | try { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 42 | 0 | my $existing= $SIG{WINCH}; | |||||||||||||||||||||||||||||||
| 43 | 0 | Scalar::Util::weaken($self); | |||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | my $handler= sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | $self->clear_tty_metrics if defined $self; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | goto $existing if defined $existing; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 47 | 0 | }; | |||||||||||||||||||||||||||||||
| 48 | 0 | $self->_winch_handler([ $handler, $existing ]); | |||||||||||||||||||||||||||||||
| 49 | 0 | $SIG{WINCH}= $handler; | |||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | catch { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 52 | 0 | warn "Can't install SIGWINCH handler\n"; | |||||||||||||||||||||||||||||||
| 53 | 0 | }; | |||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | 4 | ▶ | sub format { | ||||||||||||||||||||||||||||||
|
Subroutine
format: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 58 | 4 | my ($self, $state, $dims)= @_; | |||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | # Build the new string of progress ascii art, but without terminal escapes | ||||||||||||||||||||||||||||||||
| 61 | 4 | my $str= ''; | |||||||||||||||||||||||||||||||
| 62 | 4 | $dims->{message_margin}= $dims->{cols} * .5; | |||||||||||||||||||||||||||||||
| 63 | 4 | ▶ | if ($state->{step}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 64 | 3 | $dims->{title_width}= 10; | |||||||||||||||||||||||||||||||
| 65 | 3 | for (values %{ $state->{step} }) { | |||||||||||||||||||||||||||||||
| 66 | $dims->{title_width}= length($_->{title}) | ||||||||||||||||||||||||||||||||
| 67 | 11 | ▶ | if length($_->{title} || '') > $dims->{title_width}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$_->{'title'} || ''
MC/DC0%0/1
$_->{'title'} || ''
$_->{'title'}
Truth table50%1/2
A
A= $_->{'title'}
| |||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||
| 69 | 3 | for (sort { $a->{idx} <=> $b->{idx} } values %{ $state->{step} }) { | |||||||||||||||||||||||||||||||
| 70 | 11 | $str .= $self->_format_step_progress_line($_, $dims); | |||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||
| 72 | 3 | $str .= "\n"; | |||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||
| 74 | 4 | $str .= $self->_format_main_progress_line($state, $dims); | |||||||||||||||||||||||||||||||
| 75 | 4 | return $str; | |||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | 1 | ▶ | sub render { | ||||||||||||||||||||||||||||||
|
Subroutine
render: called CC=8 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 80 | 1 | my $self= shift; | |||||||||||||||||||||||||||||||
| 81 | 1 | my ($cols, $rows)= @{ $self->tty_metrics }{'cols','rows'}; | |||||||||||||||||||||||||||||||
| 82 | 1 | my $output= $self->format($self->parser->parse, { | |||||||||||||||||||||||||||||||
| 83 | cols => $cols, | ||||||||||||||||||||||||||||||||
| 84 | rows => $rows | ||||||||||||||||||||||||||||||||
| 85 | }); | ||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | # Now the fun part. Diff vs. previous output to figure out which lines (if any) | ||||||||||||||||||||||||||||||||
| 88 | # have changed, then move the cursor to those lines and repaint. | ||||||||||||||||||||||||||||||||
| 89 | # To make things extra interesting, the old output might have scrolled off the | ||||||||||||||||||||||||||||||||
| 90 | # screen, and if the new output also scrolls off the screen then we want to | ||||||||||||||||||||||||||||||||
| 91 | # let it happen naturally so that the scroll-back buffer is consistent. | ||||||||||||||||||||||||||||||||
| 92 | 1 | ▶ | my @prev= defined $self->_prev_output? (split /\n/, $self->_prev_output, -1) : (); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 93 | 1 | my @next= split /\n/, $output, -1; | |||||||||||||||||||||||||||||||
| 94 | # we leave last line blank, so all calculations are rows-1 | ||||||||||||||||||||||||||||||||
| 95 | 1 | ▶ | my $first_vis_line= @prev > ($rows-1)? @prev - ($rows-1) : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 96 | 1 | ▶ | my $starting_row= @prev > ($rows-1)? 0 : ($rows-1) - @prev; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 97 | 1 | my $up= $self->termcap->Tputs('up'); | |||||||||||||||||||||||||||||||
| 98 | 1 | my $down= $self->termcap->Tputs('do'); | |||||||||||||||||||||||||||||||
| 99 | 1 | my $clear_eol= $self->termcap->Tputs('ce'); | |||||||||||||||||||||||||||||||
| 100 | 1 | my $str= ''; | |||||||||||||||||||||||||||||||
| 101 | 1 | my $cursor_row= $rows-1; | |||||||||||||||||||||||||||||||
| 102 | 1 | ▶ | my $cursor_seek= sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=13.3
| |||||||||||||||||||||||||||||||||
| 103 | 1 | my $dest_row= shift; | |||||||||||||||||||||||||||||||
| 104 | 1 | ▶ | if ($cursor_row > $dest_row) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 105 | 0 | $str .= $up x ($cursor_row - $dest_row); | |||||||||||||||||||||||||||||||
| 106 | } elsif ($dest_row > $cursor_row) { | ||||||||||||||||||||||||||||||||
| 107 | 0 | $str .= $down x ($dest_row - $cursor_row); | |||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||
| 109 | 1 | $cursor_row= $dest_row; | |||||||||||||||||||||||||||||||
| 110 | 1 | }; | |||||||||||||||||||||||||||||||
| 111 | 1 | my $i; | |||||||||||||||||||||||||||||||
| 112 | 1 | ▶ | for ($i= $first_vis_line; $i < @prev; $i++) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | if ($prev[$i] ne $next[$i]) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 114 | # Seek to row | ||||||||||||||||||||||||||||||||
| 115 | 0 | $cursor_seek->($i - $first_vis_line + $starting_row); | |||||||||||||||||||||||||||||||
| 116 | # clear line and replace | ||||||||||||||||||||||||||||||||
| 117 | 0 | $str .= $clear_eol . $next[$i] . "\n"; | |||||||||||||||||||||||||||||||
| 118 | 0 | $cursor_row++; | |||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||
| 121 | 1 | $cursor_seek->($rows-1); | |||||||||||||||||||||||||||||||
| 122 | # Now, print any new rows in @next, letting them scroll the screen as needed | ||||||||||||||||||||||||||||||||
| 123 | 1 | ▶ | while ($i < @next) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 124 | 8 | $str .= $next[$i++] . "\n"; | |||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||
| 126 | 1 | $self->_prev_output($output); | |||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||
| 128 | 1 | ▶ | ($self->out || \*STDOUT)->print($str); | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->out || \*STDOUT
MC/DC0%0/1
$self->out || \*STDOUT
$self->out
Truth table50%1/2
A
A= $self->out
| |||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||
| 131 | 4 | ▶ | sub _format_main_progress_line { | ||||||||||||||||||||||||||||||
|
Subroutine
_format_main_progress_line: called CC=6 SCAR=18.2
| |||||||||||||||||||||||||||||||||
| 132 | 4 | my ($self, $state, $dims)= @_; | |||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | 4 | my $message= $state->{message}; | |||||||||||||||||||||||||||||||
| 135 | 4 | ▶ | $message= '' unless defined $message; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 136 | $message= sprintf("(%d/%d) %s", $state->{current}, $state->{total}, $message) | ||||||||||||||||||||||||||||||||
| 137 | 4 | ▶ | if defined $state->{total} and defined $state->{current}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $state->{"total"} and defined $state->{"current"}
MC/DC50%1/2
defined $state->{"total"} and defined $state->{"current"}
defined $state->{"total"}
defined $state->{"current"}
Truth table66%2/3
A and B
A= defined $state->{"total"}B= defined $state->{"current"}
| |||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||
| 139 | 4 | my $max_chars= $dims->{cols} - 8; | |||||||||||||||||||||||||||||||
| 140 | return sprintf "[%-*s] %3d%%\n %.*s", | ||||||||||||||||||||||||||||||||
| 141 | $max_chars, '=' x int( ($state->{progress}||0) * $max_chars + .000001 ), | ||||||||||||||||||||||||||||||||
| 142 | int( ($state->{progress}||0) * 100 + .0000001 ), | ||||||||||||||||||||||||||||||||
| 143 | 4 | ▶ | $dims->{cols}, $message; | ||||||||||||||||||||||||||||||
|
Condition50%2/4
$state->{'progress'} || 0
$state->{'progress'} || 0
MC/DC0%0/1
$state->{'progress'} || 0
$state->{'progress'}
MC/DC0%0/1
$state->{'progress'} || 0
$state->{'progress'}
Truth table50%1/2
A
A= $state->{'progress'}
Truth table50%1/2
A
A= $state->{'progress'}
| |||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||
| 146 | 11 | ▶ | sub _format_step_progress_line { | ||||||||||||||||||||||||||||||
|
Subroutine
_format_step_progress_line: called CC=6 SCAR=18.7
| |||||||||||||||||||||||||||||||||
| 147 | 11 | my ($self, $state, $dims)= @_; | |||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | 11 | my $message= $state->{message}; | |||||||||||||||||||||||||||||||
| 150 | 11 | ▶ | $message= '' unless defined $message; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 151 | $message= sprintf("(%d/%d) %s", $state->{current}, $state->{total}, $message) | ||||||||||||||||||||||||||||||||
| 152 | 11 | ▶ | if defined $state->{total} and defined $state->{current}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $state->{"total"} and defined $state->{"current"}
MC/DC50%1/2
defined $state->{"total"} and defined $state->{"current"}
defined $state->{"total"}
defined $state->{"current"}
Truth table66%2/3
A and B
A= defined $state->{"total"}B= defined $state->{"current"}
| |||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||
| 154 | 11 | my $max_chars= $dims->{cols} - $dims->{message_margin} - $dims->{title_width} - 11; | |||||||||||||||||||||||||||||||
| 155 | return sprintf " %-*.*s [%-*s] %3d%% %.*s\n", | ||||||||||||||||||||||||||||||||
| 156 | $dims->{title_width}, $dims->{title_width}, $_->{title}, | ||||||||||||||||||||||||||||||||
| 157 | $max_chars, '=' x int( ($state->{progress}||0) * $max_chars + .000001 ), | ||||||||||||||||||||||||||||||||
| 158 | int( ($state->{progress}||0) * 100 + .000001 ), | ||||||||||||||||||||||||||||||||
| 159 | 11 | ▶ | $dims->{message_margin}, $message; | ||||||||||||||||||||||||||||||
|
Condition50%2/4
$state->{'progress'} || 0
$state->{'progress'} || 0
MC/DC0%0/1
$state->{'progress'} || 0
$state->{'progress'}
MC/DC0%0/1
$state->{'progress'} || 0
$state->{'progress'}
Truth table50%1/2
A
A= $state->{'progress'}
Truth table50%1/2
A
A= $state->{'progress'}
| |||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||
| 162 | 1 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=3 SCAR=18.1
| |||||||||||||||||||||||||||||||||
| 163 | 1 | my $self= shift; | |||||||||||||||||||||||||||||||
| 164 | 1 | ▶ | if ($self->_winch_handler) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 165 | 0 | ▶ | if ($SIG{WINCH} eq $self->_winch_handler->[0]) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 166 | 0 | $SIG{WINCH}= $self->_winch_handler->[1]; | |||||||||||||||||||||||||||||||
| 167 | } else { | ||||||||||||||||||||||||||||||||
| 168 | 0 | warn "Can't uninstall SIGWINCH handler\n"; | |||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||
| 170 | 0 | $self->_winch_handler(undef); | |||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||
| 174 | 1 | 1; | |||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||
| 176 | __END__ | ||||||||||||||||||||||||||||||||