| 1 | package Term::Output::List::ANSI; | ||||||||||||
| 2 | 2 | ▶ | use 5.020; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 2 | ▶ | use Moo 2; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 2 | ▶ | use Term::Cap; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 2 | ▶ | use Scalar::Util 'weaken'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 2 | ▶ | use experimental 'signatures'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 2 | our $VERSION = '0.06'; | |||||||||||
| 9 | |||||||||||||
| 10 | =head1 NAME | ||||||||||||
| 11 | |||||||||||||
| 12 | Term::Output::List::ANSI - output an updateable list of ongoing jobs to an ANSI terminal | ||||||||||||
| 13 | |||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||
| 15 | |||||||||||||
| 16 | my $printer = Term::Output::List->new( | ||||||||||||
| 17 | hook_warnings => 1, | ||||||||||||
| 18 | ); | ||||||||||||
| 19 | my @ongoing_tasks = ('file1: frobnicating', 'file2: bamboozling', 'file3: frobnicating'); | ||||||||||||
| 20 | $printer->output_list(@ongoing_tasks); | ||||||||||||
| 21 | |||||||||||||
| 22 | $printer->output_permanent("Frobnicated gizmos"); # appears above the list | ||||||||||||
| 23 | |||||||||||||
| 24 | =cut | ||||||||||||
| 25 | |||||||||||||
| 26 | =head1 MEMBERS | ||||||||||||
| 27 | |||||||||||||
| 28 | =head2 C<< fh >> | ||||||||||||
| 29 | |||||||||||||
| 30 | Filehandle used for output. Default is C<< STDOUT >>. | ||||||||||||
| 31 | |||||||||||||
| 32 | =cut | ||||||||||||
| 33 | |||||||||||||
| 34 | has 'terminfo' => ( | ||||||||||||
| 35 | is => 'lazy', | ||||||||||||
| 36 | 0 | ▶ | default => sub { Term::Cap->Tgetent({ OSPEED => 112000 })}, | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 37 | 2 | ); | |||||||||||
| 38 | |||||||||||||
| 39 | has 'term_scroll_up' => ( | ||||||||||||
| 40 | is => 'lazy', | ||||||||||||
| 41 | 0 | ▶ | default => sub { $_[0]->terminfo->Tputs('UP') }, | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 42 | 2 | ); | |||||||||||
| 43 | |||||||||||||
| 44 | has 'term_clear_eol' => ( | ||||||||||||
| 45 | is => 'lazy', | ||||||||||||
| 46 | 0 | ▶ | default => sub { $_[0]->terminfo->Tputs('ce') }, | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 47 | 2 | ); | |||||||||||
| 48 | |||||||||||||
| 49 | =head2 C<< interactive >> | ||||||||||||
| 50 | |||||||||||||
| 51 | Whether the script is run interactively and should output intermittent | ||||||||||||
| 52 | updateable information | ||||||||||||
| 53 | |||||||||||||
| 54 | =head2 C<< hook_warnings >> | ||||||||||||
| 55 | |||||||||||||
| 56 | Install a hook for sending warnings to C<< ->output_permanent >>. This | ||||||||||||
| 57 | prevents ugly tearing/overwriting when your code outputs warnings. | ||||||||||||
| 58 | |||||||||||||
| 59 | =cut | ||||||||||||
| 60 | |||||||||||||
| 61 | =head1 METHODS | ||||||||||||
| 62 | |||||||||||||
| 63 | =head2 C<< Term::Output::List::ANSI->new() >> | ||||||||||||
| 64 | |||||||||||||
| 65 | my $output = Term::Output::List::ANSI->new( | ||||||||||||
| 66 | hook_warnings => 1, | ||||||||||||
| 67 | ) | ||||||||||||
| 68 | |||||||||||||
| 69 | =over 4 | ||||||||||||
| 70 | |||||||||||||
| 71 | =item C<< hook_warnings >> | ||||||||||||
| 72 | |||||||||||||
| 73 | Install a hook for sending warnings to C<< ->output_permanent >>. This | ||||||||||||
| 74 | prevents ugly tearing/overwriting when your code outputs warnings. | ||||||||||||
| 75 | |||||||||||||
| 76 | =back | ||||||||||||
| 77 | |||||||||||||
| 78 | =cut | ||||||||||||
| 79 | |||||||||||||
| 80 | =head2 C<< width >> | ||||||||||||
| 81 | |||||||||||||
| 82 | Width of the terminal. This is initialized at first use. You may (or may not) | ||||||||||||
| 83 | want to set up a C<< $SIG{WINCH} >> handler to set the terminal width when | ||||||||||||
| 84 | the terminal size changes. | ||||||||||||
| 85 | |||||||||||||
| 86 | =cut | ||||||||||||
| 87 | |||||||||||||
| 88 | has 'width' => ( | ||||||||||||
| 89 | is => 'lazy', | ||||||||||||
| 90 | 0 | ▶ | default => sub { `tput cols` }, | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 91 | 2 | ); | |||||||||||
| 92 | |||||||||||||
| 93 | =head1 METHODS | ||||||||||||
| 94 | |||||||||||||
| 95 | =head2 C<< Term::Output::List->new() >> | ||||||||||||
| 96 | |||||||||||||
| 97 | =cut | ||||||||||||
| 98 | |||||||||||||
| 99 | =head2 C<< ->scroll_up >> | ||||||||||||
| 100 | |||||||||||||
| 101 | Helper method to place the cursor at the top of the updateable list. | ||||||||||||
| 102 | |||||||||||||
| 103 | =cut | ||||||||||||
| 104 | |||||||||||||
| 105 | has 'ellipsis' => ( | ||||||||||||
| 106 | is => 'lazy', | ||||||||||||
| 107 | 0 | ▶ | default => sub { "\N{HORIZONTAL ELLIPSIS}" }, | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
Subroutine
__ANON__: not called CC=1 SCAR=0.2
| |||||||||||||
| 108 | 2 | ); | |||||||||||
| 109 | |||||||||||||
| 110 | 2 | with 'Term::Output::List::Role'; | |||||||||||
| 111 | |||||||||||||
| 112 | 0 | ▶ | sub scroll_up( $self, $count=$self->_last_lines ) { | ||||||||||
|
Subroutine
scroll_up: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 113 | 0 | ▶ | if( !$count) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 114 | } else { | ||||||||||||
| 115 | # Overwrite the number of lines we printed last time | ||||||||||||
| 116 | 0 | print { $self->fh } "\r" . sprintf $self->term_scroll_up(), ${count}; | |||||||||||
| 117 | #sleep 1; | ||||||||||||
| 118 | }; | ||||||||||||
| 119 | } | ||||||||||||
| 120 | |||||||||||||
| 121 | =head2 C<< ->output_permanent >> | ||||||||||||
| 122 | |||||||||||||
| 123 | $o->output_permanent("Frobnicated 3 items for job 2"); | ||||||||||||
| 124 | $o->output_list("Frobnicating 9 items for job 1", | ||||||||||||
| 125 | "Frobnicating 2 items for job 3", | ||||||||||||
| 126 | ); | ||||||||||||
| 127 | |||||||||||||
| 128 | Outputs items that should go on the permanent record. It is expected to | ||||||||||||
| 129 | output the (remaining) list of ongoing jobs after that. | ||||||||||||
| 130 | |||||||||||||
| 131 | =cut | ||||||||||||
| 132 | |||||||||||||
| 133 | 10 | ▶ | sub output_permanent( $self, @items ) { | ||||||||||
|
Subroutine
output_permanent: called CC=5 SCAR=28.0
Pod
documented
| |||||||||||||
| 134 | 10 | ▶ | my $total = $self->_last_lines // 0; | ||||||||||
|
Condition50%1/2
$self->_last_lines // 0
MC/DC0%0/1
$self->_last_lines // 0
$self->_last_lines
Truth table50%1/2
A
A= $self->_last_lines
| |||||||||||||
| 135 | 10 | ▶ | if( !$self->interactive ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 136 | 10 | print { $self->fh } join("\n", @items) . "\n"; | |||||||||||
| 137 | |||||||||||||
| 138 | } else { | ||||||||||||
| 139 | 0 | $self->scroll_up($total); | |||||||||||
| 140 | 0 | my $w = $self->width; | |||||||||||
| 141 | 0 | my $clear_eol = $self->term_clear_eol; | |||||||||||
| 142 | |||||||||||||
| 143 | 0 | ▶ | if( @items ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 144 | |||||||||||||
| 145 | 0 | print { $self->fh } | |||||||||||
| 146 | join("$clear_eol\n", | ||||||||||||
| 147 | 0 | map { $self->_trim( $_, $w ) } | |||||||||||
| 148 | 0 | map { s/\s*\z//r } | |||||||||||
| 149 | @items | ||||||||||||
| 150 | )."$clear_eol\n"; | ||||||||||||
| 151 | }; | ||||||||||||
| 152 | |||||||||||||
| 153 | # If we have fewer items than before, clear the lines of the vanished items | ||||||||||||
| 154 | 0 | my $blank = $total - @items; | |||||||||||
| 155 | 0 | ▶ | if( $blank > 0 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 156 | 0 | print { $self->fh } "$clear_eol\n"x ($blank); | |||||||||||
| 157 | 0 | $self->scroll_up( $blank ); | |||||||||||
| 158 | } | ||||||||||||
| 159 | 0 | $self->fresh_output(); | |||||||||||
| 160 | } | ||||||||||||
| 161 | } | ||||||||||||
| 162 | |||||||||||||
| 163 | =head2 C<< ->output_list @items >> | ||||||||||||
| 164 | |||||||||||||
| 165 | $o->output_list("Frobnicating 9 items for job 1", | ||||||||||||
| 166 | "Frobnicating 2 items for job 3", | ||||||||||||
| 167 | ); | ||||||||||||
| 168 | |||||||||||||
| 169 | Outputs items that can be updated later, as long as no intervening output | ||||||||||||
| 170 | (like from C<print>, C<say> or C<warn>) has happened. If you want to output | ||||||||||||
| 171 | lines that should not be overwritten later, see C<</->output_permanent>> | ||||||||||||
| 172 | |||||||||||||
| 173 | =head2 C<< ->fresh_output >> | ||||||||||||
| 174 | |||||||||||||
| 175 | $o->fresh_output(); | ||||||||||||
| 176 | |||||||||||||
| 177 | Helper subroutine to make all items from the last output list remain as is. | ||||||||||||
| 178 | |||||||||||||
| 179 | For compatibility between output to a terminal and output without a terminal, | ||||||||||||
| 180 | you should use C<< ->output_permanent >> for things that should be permanent | ||||||||||||
| 181 | instead. | ||||||||||||
| 182 | |||||||||||||
| 183 | =cut | ||||||||||||
| 184 | |||||||||||||
| 185 | 2 | 1; | |||||||||||