| 1 | package Term::Output::List::Role; | ||||||||||
| 2 | 2 | ▶ | use 5.020; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 2 | ▶ | use Moo::Role; | ||||||||
|
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::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 | 2 | 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 | 1 | ▶ | default => sub { \*STDOUT }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 41 | 2 | ); | |||||||||
| 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 | 2 | ▶ | default => sub { -t $_[0]->fh }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 53 | 2 | ); | |||||||||
| 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 | 2 | has 'hook_warnings' => ( | |||||||||
| 63 | is => 'ro', | ||||||||||
| 64 | default => undef, | ||||||||||
| 65 | ); | ||||||||||
| 66 | |||||||||||
| 67 | 2 | ▶ | sub BUILD( $self, $args ) { | ||||||||
|
Subroutine
BUILD: called CC=3 SCAR=21.6
Pod
undocumented
| |||||||||||
| 68 | 2 | ▶ | if( $args->{hook_warnings} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 0 | ▶ | if( ! $SIG{__WARN__}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 0 | weaken( my $s = $self ); | |||||||||
| 71 | 0 | ▶ | $SIG{__WARN__} = sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 72 | 0 | ▶ | if( $self ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | 0 | my $msg = "@_"; | |||||||||
| 74 | 0 | $self->output_permanent($msg ); | |||||||||
| 75 | } else { | ||||||||||
| 76 | 0 | print STDERR "@_"; | |||||||||
| 77 | } | ||||||||||
| 78 | 0 | }; | |||||||||
| 79 | } | ||||||||||
| 80 | } | ||||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | 2 | requires qw( width output_permanent ellipsis ); | |||||||||
| 84 | |||||||||||
| 85 | 0 | ▶ | sub _trim( $self, $item, $width=$self->width ) { | ||||||||
|
Subroutine
_trim: not called CC=3 SCAR=24.8
| |||||||||||
| 86 | 0 | state $ell = $self->ellipsis; | |||||||||
| 87 | 0 | ▶ | if( length($item) > $width - 1 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 0 | return substr($item,0,$width-length($ell)-1).$ell | |||||||||
| 89 | } else { | ||||||||||
| 90 | 0 | return $item | |||||||||
| 91 | } | ||||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | 8 | ▶ | sub output_list( $self, @items ) { | ||||||||
|
Subroutine
output_list: called CC=2 SCAR=13.0
Pod
undocumented
| |||||||||||
| 95 | 8 | ▶ | if( $self->interactive ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | 0 | @items = map { s/\r?\n$//r } | |||||||||
| 97 | 0 | map { split /\r?\n/ } | |||||||||
| 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 | ▶ | sub fresh_output( $self ) { | ||||||||
|
Subroutine
fresh_output: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 118 | 0 | $self->_last_lines( 0 ); | |||||||||
| 119 | } | ||||||||||
| 120 | |||||||||||
| 121 | 2 | 1; | |||||||||