| 1 | package App::cpm::Logger::Terminal; | ||
| 2 | use v5.24; | ||
| 3 | use warnings; | ||
| 4 | use experimental qw(lexical_subs signatures); | ||
| 5 | |||
| 6 | package App::cpm::Logger::Terminal::_Lines { | ||
| 7 | use v5.24; | ||
| 8 | use warnings; | ||
| 9 | use experimental qw(lexical_subs signatures); | ||
| 10 | |||
| 11 | my %ING = ( | ||
| 12 | resolve => "resolving", | ||
| 13 | fetch => "fetching", | ||
| 14 | configure => "configuring", | ||
| 15 | build => "building", | ||
| 16 | test => "testing", | ||
| 17 | install => "installing", | ||
| 18 | ); | ||
| 19 | |||
| 20 | sub new ($class, $pids, $progress, $use_color, $width) { | ||
| 21 | my $num = keys $pids->%*; | ||
| 22 | bless { | ||
| 23 | pids => $pids, | ||
| 24 | progress => $progress, | ||
| 25 | num => $num, | ||
| 26 | use_color => $use_color, | ||
| 27 | width => $width, | ||
| 28 | _lines => [], | ||
| 29 | }, $class; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub _truncate ($self, $text, $width) { | ||
| 33 | return "" if $width <= 0; | ||
| 34 | return $text if length($text) <= $width; | ||
| 35 | return "." x $width if $width <= 3; | ||
| 36 | substr($text, 0, $width - 3) . "..."; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub set_worker ($self, $pid, $task = undef) { | ||
| 40 | my ($progress, $ing, $name) = $task | ||
| 41 | ? ($self->{progress}, $ING{$task->type} || $task->type, $task->distvname) | ||
| 42 | : (" ", "idle", ""); | ||
| 43 | if ($self->{width}) { | ||
| 44 | my $available = $self->{width} - 14; | ||
| 45 | $name = $self->_truncate($name, $available); | ||
| 46 | } | ||
| 47 | $ing = sprintf "%-11s", $ing; | ||
| 48 | $ing = "\e[1;30m$ing\e[m" if $self->{use_color}; | ||
| 49 | $self->{_lines}[ $self->{pids}{$pid} ] = sprintf "%s %-11s %s\n", | ||
| 50 | $progress, $ing, $name; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub set_summary ($self, $done, $all) { | ||
| 54 | $self->{_lines}[ $self->{num} ] = sprintf "--- %d/%d ---\n", $done, $all; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub lines ($self) { | ||
| 58 | $self->{_lines}->@*; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | sub new ($class, %argv) { | ||
| 63 | my @pid = $argv{pids}->@*; | ||
| 64 | my %pid = map { ($pid[$_], $_) } 0 .. $#pid; | ||
| 65 | bless { | ||
| 66 | first => 1, | ||
| 67 | pids => \%pid, | ||
| 68 | lines => 1 + (keys %pid), | ||
| 69 | fh => \*STDERR, | ||
| 70 | progress_index => 0, | ||
| 71 | use_color => 1, | ||
| 72 | width => $argv{width} || 0, | ||
| 73 | }, $class; | ||
| 74 | } | ||
| 75 | |||
| 76 | my @PROGRESS = qw(\\ | / -); | ||
| 77 | |||
| 78 | sub use_color ($self, $value = undef) { | ||
| 79 | $self->{use_color} = $value ? 1 : 0 if defined $value; | ||
| 80 | $self->{use_color}; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub progress ($self) { | ||
| 84 | $self->{progress_index} = ($self->{progress_index} + 1) % @PROGRESS; | ||
| 85 | $PROGRESS[ $self->{progress_index} ]; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub new_lines ($self) { | ||
| 89 | App::cpm::Logger::Terminal::_Lines->new($self->{pids}, $self->progress, $self->{use_color}, $self->{width}); | ||
| 90 | } | ||
| 91 | |||
| 92 | sub clear ($self) { | ||
| 93 | return if $self->{first}; | ||
| 94 | $self->{fh}->print(("\e[1A\e[K") x $self->{lines}); | ||
| 95 | } | ||
| 96 | |||
| 97 | sub write ($self, $lines) { | ||
| 98 | $self->clear; | ||
| 99 | $self->{first} = 0; | ||
| 100 | $self->{fh}->print($lines->lines); | ||
| 101 | } | ||
| 102 | |||
| 103 | sub finalize ($self) { | ||
| 104 | $self->clear; | ||
| 105 | $self->{first} = 1; | ||
| 106 | } | ||
| 107 | |||
| 108 | 1; |