File Coverage

blib/lib/Term/Output/List.pm
Criterion Covered Total %
statement 22 27 81.4
branch 2 6 33.3
condition n/a
subroutine 5 5 100.0
pod 1 2 50.0
total 30 40 75.0


line stmt bran cond sub pod time code
1             package Term::Output::List;
2 3     3   16435 use 5.020;
  3         16  
3 3     3   2112 use experimental 'signatures';
  3         14602  
  3         21  
4              
5 3     3   2919 use Module::Load 'load';
  3         5987  
  3         22  
6              
7             our $VERSION = '0.06';
8              
9             =head1 NAME
10              
11             Term::Output::List - output an updateable list of ongoing jobs
12              
13             =head1 SYNOPSIS
14              
15             my $printer = Term::Output::List->new(
16             hook_warnings => 1,
17             ellipsis => "\N{HORIZONTAL ELLIPSIS}",
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     2 0 4 sub detect_terminal_type($os = $^O) {
  2         9  
  2         3  
27 2 50       8 if( $os eq 'MSWin32' ) {
28 0         0 require Win32::Console;
29 0 0       0 if( Win32::Console->Mode & 0x0004 ) { #ENABLE_VIRTUAL_TERMINAL_PROCESSING
30 0         0 return 'ansi';
31             } else {
32 0         0 return 'win32'
33             }
34             } else {
35 2         7 return 'ansi';
36             }
37             }
38              
39 2     2 1 847276 sub new($class,%args) {
  2         6  
  2         6  
  2         3  
40 2         9 my $ttype = detect_terminal_type();
41              
42 2         9 my $impl = 'Term::Output::List::ANSI';
43 2 50       9 if( $ttype eq 'win32' ) {
44 0         0 $impl = 'Term::Output::List::Win32';
45             }
46 2         11 load $impl;
47 2         47 return $impl->new( %args )
48             }
49              
50             =head1 METHODS
51              
52             =head2 C<< Term::Output::List->new() >>
53              
54             =over 4
55              
56             =item C<< fh >>
57              
58             Filehandle used for output. Default is C<< STDOUT >>.
59              
60             =item C<< interactive >>
61              
62             Whether the script is run interactively and should output intermittent
63             updateable information
64              
65             =item C<< hook_warnings >>
66              
67             Install a hook for sending warnings to C<< ->output_permanent >>. This
68             prevents ugly tearing/overwriting when your code outputs warnings.
69              
70             =back
71              
72             =head2 C<< ->scroll_up >>
73              
74             Helper method to place the cursor at the top of the updateable list.
75              
76             =head2 C<< ->output_permanent >>
77              
78             $o->output_permanent("Frobnicated 3 items for job 2");
79             $o->output_list("Frobnicating 9 items for job 1",
80             "Frobnicating 2 items for job 3",
81             );
82             $o->output_permanent("Frobnicated 3 items for job 2");
83              
84             Outputs items that should go on the permanent record. It is expected to
85             output the (remaining) list of ongoing jobs after that.
86              
87             =head2 C<< ->output_list @items >>
88              
89             $o->output_list("Frobnicating 9 items for job 1",
90             "Frobnicating 2 items for job 3",
91             );
92              
93             Outputs items that can be updated later, as long as no intervening output
94             (like from C, C or C) has happened. If you want to output
95             lines that should not be overwritten later, see C<output_permanent>>
96              
97             =head2 C<< ->fresh_output >>
98              
99             $o->fresh_output();
100              
101             Helper subroutine to make all items from the last output list remain as is.
102              
103             For compatibility between output to a terminal and output without a terminal,
104             you should use C<< ->output_permanent >> for things that should be permanent
105             instead.
106              
107             =cut
108              
109             1;