blib/lib/Term/Output/List/ANSI.pm

filter: Statementstmt 58.3% 35 / 60 Branchbran 21.4% 3 / 14 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 62.5% 10 / 16 Podpod 100% 4 / 4 totaltotal 54.6% 53 / 97 SCAR 30.7
CC
10
Cov
51%
output_permanent
27.0
scroll_up
12.0
output_list
2.9
CRAP 21.5
« blib/lib/Term/Output/List/Win32.pm ↑ Summary blib/lib/Term/Output/List.pm »
1 package Term::Output::List::ANSI;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Moo 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Term::Cap;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 our $VERSION = '0.03';
10  
11 =head1 NAME
12  
13 Term::Output::List::ANSI - output an updateable list of ongoing jobs to an ANSI terminal
14  
15 =head1 SYNOPSIS
16  
17 my $printer = Term::Output::List->new();
18 my @ongoing_tasks = ('file1: frobnicating', 'file2: bamboozling', 'file3: frobnicating');
19 $printer->output_list(@ongoing_tasks);
20  
21 $printer->output_permanent("Frobnicated gizmos"); # appears above the list
22  
23 =cut
24  
25 2 has '_last_lines' => (
26 is => 'rw',
27 );
28  
29 =head1 MEMBERS
30  
31 =head2 C<< fh >>
32  
33 Filehandle used for output. Default is C<< STDOUT >>.
34  
35 =cut
36  
37 has 'fh' => (
38 is => 'lazy',
39 1 ▶ default => sub { \*STDOUT },
Subroutine __ANON__: called CC=1 SCAR=0.0
40 2 );
41  
42 has 'terminfo' => (
43 is => 'lazy',
44 0 ▶ default => sub { Term::Cap->Tgetent({ OSPEED => 112000 })},
Subroutine __ANON__: not called CC=1 SCAR=6.9
45 2 );
46  
47 has 'term_scroll_up' => (
48 is => 'lazy',
49 0 ▶ default => sub { $_[0]->terminfo->Tputs('UP') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 2 );
51  
52 has 'term_clear_eol' => (
53 is => 'lazy',
54 0 ▶ default => sub { $_[0]->terminfo->Tputs('ce') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
55 2 );
56  
57 has 'interactive' => (
58 is => 'lazy',
59 2 ▶ default => sub { -t $_[0]->fh },
Subroutine __ANON__: called CC=1 SCAR=0.0
60 2 );
61  
62 =head2 C<< width >>
63  
64 Width of the terminal. This is initialized at first use. You may (or may not)
65 want to set up a C<< $SIG{WINCH} >> handler to set the terminal width when
66 the terminal size changes.
67  
68 =cut
69  
70 has 'width' => (
71 is => 'lazy',
72 0 ▶ default => sub { `tput cols` },
Subroutine __ANON__: not called CC=1 SCAR=6.9
73 2 );
74  
75 =head1 METHODS
76  
77 =head2 C<< Term::Output::List->new() >>
78  
79 =cut
80  
81 =head2 C<< ->scroll_up >>
82  
83 Helper method to place the cursor at the top of the updateable list.
84  
85 =cut
86  
87 0 ▶sub scroll_up( $self, $count=$self->_last_lines ) {
Subroutine scroll_up: not called CC=3 SCAR=24.8
Pod documented
88 0 ▶ if( !$count) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $count) { } 0 0 0
89 } else {
90 # Overwrite the number of lines we printed last time
91 0 print { $self->fh } "\r" . sprintf $self->term_scroll_up(), ${count};
92 #sleep 1;
93 };
94 }
95  
96 =head2 C<< ->output_permanent >>
97  
98 $o->output_permanent("Frobnicated 3 items for job 2");
99 $o->output_list("Frobnicating 9 items for job 1",
100 "Frobnicating 2 items for job 3",
101 );
102  
103 Outputs items that should go on the permanent record. It is expected to
104 output the (remaining) list of ongoing jobs after that.
105  
106 =cut
107  
108 ▶sub output_permanent( $self, @items ) {
Subroutine output_permanent: called CC=7 SCAR=33.0
Pod documented
109 10 ▶ my $total = $self->_last_lines // 0;
Condition50%1/2
$self->_last_lines // 0
l!l
010
MC/DC0%0/1
$self->_last_lines // 0
$self->_last_lines
Truth table50%1/2
A
A= $self->_last_lines
Aresult
11
00
110 10 ▶ if( $self->interactive ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->interactive) { } 0 10 10
111 0 $self->scroll_up();
112 0 my $w = $self->width;
113 0 my $clear_eol = $self->term_clear_eol;
114 0 ▶ if( @items ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@items) 0 0 0
115 0 print { $self->fh }
116 join("$clear_eol\n",
117 0 ▶ map { length($_) > $w - 1 ? (substr($_,0,$w-3).'..'): $_
Branch: 1 branch
BranchTrueFalseTotal
length $_ > $w - 1 ? : 0 0 0
118 } @items)."$clear_eol\n";
119 };
120 } else {
121 10 print { $self->fh } join("\n", @items) . "\n";
122 }
123 #sleep 1;
124  
125 10 ▶ if( $self->interactive ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->interactive) 0 10 10
126 0 my $blank = $total - @items;
127 0 my $clear_eol = $self->term_clear_eol;
128 0 ▶ if( $blank > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blank > 0) 0 0 0
129 0 print { $self->fh } "$clear_eol\n"x ($blank);
130 0 $self->scroll_up( $blank );
131 }
132 0 $self->fresh_output();
133 }
134 }
135  
136 =head2 C<< ->output_list @items >>
137  
138 $o->output_list("Frobnicating 9 items for job 1",
139 "Frobnicating 2 items for job 3",
140 );
141  
142 Outputs items that can be updated later, as long as no intervening output
143 (like from C<print>, C<say> or C<warn>) has happened. If you want to output
144 lines that should not be overwritten later, see C<</->output_permanent>>
145  
146 =cut
147  
148 ▶sub output_list( $self, @items ) {
Subroutine output_list: called CC=2 SCAR=10.5
Pod documented
149 8 ▶ if( $self->interactive ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->interactive) 0 8 8
150 0 $self->output_permanent(@items);
151 #sleep 1;
152 0 $self->_last_lines( 0+@items);
153 }
154 }
155  
156 =head2 C<< ->fresh_output >>
157  
158 $o->fresh_output();
159  
160 Helper subroutine to make all items from the last output list remain as is.
161  
162 For compatibility between output to a terminal and output without a terminal,
163 you should use C<< ->output_permanent >> for things that should be permanent
164 instead.
165  
166 =cut
167  
168 ▶sub fresh_output( $self ) {
Subroutine fresh_output: not called CC=1 SCAR=6.9
Pod documented
169 0 $self->_last_lines( 0 );
170 }
171  
172 2 1;
« blib/lib/Term/Output/List/Win32.pm ↑ Summary blib/lib/Term/Output/List.pm »