File Coverage

blib/lib/Term/Output/List/Role.pm
Criterion Covered Total %
statement 24 49 48.9
branch 2 10 20.0
condition n/a
subroutine 7 10 70.0
pod 1 3 33.3
total 34 72 47.2


line stmt bran cond sub pod time code
1             package Term::Output::List::Role;
2 2     2   22900 use 5.020;
  2         9  
3 2     2   9 use Moo::Role;
  2         3  
  2         13  
4 2     2   1125 use Term::Cap;
  2         5  
  2         62  
5 2     2   10 use Scalar::Util 'weaken';
  2         3  
  2         175  
6 2     2   11 use experimental 'signatures';
  2         3  
  2         16  
7              
8             our $VERSION = '0.06';
9              
10             =head1 NAME
11              
12             Term::Output::List::Role - common methods to Term::Output::List implementations
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             has '_last_lines' => (
27             is => 'rw',
28             );
29              
30             =head1 MEMBERS
31              
32             =head2 C<< fh >>
33              
34             Filehandle used for output. Default is C<< STDOUT >>.
35              
36             =cut
37              
38             has 'fh' => (
39             is => 'lazy',
40             default => sub { \*STDOUT },
41             );
42              
43             =head2 C<< interactive >>
44              
45             Whether the script is run interactively and should output intermittent
46             updateable information
47              
48             =cut
49              
50             has 'interactive' => (
51             is => 'lazy',
52             default => sub { -t $_[0]->fh },
53             );
54              
55             =head2 C<< hook_warnings >>
56              
57             Install a hook for sending warnings to C<< ->output_permanent >>. This
58             prevents ugly tearing/overwriting when your code outputs warnings.
59              
60             =cut
61              
62             has 'hook_warnings' => (
63             is => 'ro',
64             default => undef,
65             );
66              
67 2     2 0 6343 sub BUILD( $self, $args ) {
  2         4  
  2         4  
  2         3  
68 2 50       19 if( $args->{hook_warnings} ) {
69 0 0       0 if( ! $SIG{__WARN__}) {
70 0         0 weaken( my $s = $self );
71             $SIG{__WARN__} = sub {
72 0 0   0   0 if( $self ) {
73 0         0 my $msg = "@_";
74 0         0 $self->output_permanent($msg );
75             } else {
76 0         0 print STDERR "@_";
77             }
78 0         0 };
79             }
80             }
81             }
82              
83             requires qw( width output_permanent ellipsis );
84              
85 0     0   0 sub _trim( $self, $item, $width=$self->width ) {
  0         0  
  0         0  
  0         0  
  0         0  
86 0         0 state $ell = $self->ellipsis;
87 0 0       0 if( length($item) > $width - 1 ) {
88 0         0 return substr($item,0,$width-length($ell)-1).$ell
89             } else {
90 0         0 return $item
91             }
92             }
93              
94 8     8 0 106 sub output_list( $self, @items ) {
  8         14  
  8         16  
  8         11  
95 8 50       146 if( $self->interactive ) {
96 0           @items = map { s/\r?\n$//r }
97 0           map { split /\r?\n/ }
  0            
98             @items
99             ;
100 0           $self->output_permanent(@items);
101 0           $self->_last_lines( 0+@items );
102             }
103             }
104              
105             =head2 C<< ->fresh_output >>
106              
107             $o->fresh_output();
108              
109             Helper subroutine to make all items from the last output list remain as is.
110              
111             For compatibility between output to a terminal and output without a terminal,
112             you should use C<< ->output_permanent >> for things that should be permanent
113             instead.
114              
115             =cut
116              
117 0     0 1   sub fresh_output( $self ) {
  0            
  0            
118 0           $self->_last_lines( 0 );
119             }
120              
121             1;