| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||
| 4 | # (C) Paul Evans, 2021 -- leonerd@leonerd.org.uk | ||||||||||||||||||||||
| 5 | |||||||||||||||||||||||
| 6 | package IO::Term::Status 0.01; | ||||||||||||||||||||||
| 7 | |||||||||||||||||||||||
| 8 | 4 | ▶ | use v5.14; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 9 | 4 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 10 | 4 | ▶ | use base qw( IO::Handle ); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 11 | |||||||||||||||||||||||
| 12 | 4 | my $EL = "\e[K"; | |||||||||||||||||||||
| 13 | 4 | my $CLEARLINE = "\r$EL"; | |||||||||||||||||||||
| 14 | 4 | my $PREVLINE = "\eM"; | |||||||||||||||||||||
| 15 | 4 | my $PREVLINEHOME = "\r$PREVLINE"; | |||||||||||||||||||||
| 16 | |||||||||||||||||||||||
| 17 | 4 | ▶ | use constant HAVE_STRING_TAGGED_TERMINAL => defined eval { | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 18 | 4 | require String::Tagged::Terminal; | |||||||||||||||||||||
| 19 | 4 | }; | |||||||||||||||||||||
| 20 | |||||||||||||||||||||||
| 21 | =head1 NAME | ||||||||||||||||||||||
| 22 | |||||||||||||||||||||||
| 23 | C<IO::Term::Status> - print log lines to a terminal with a running status bar | ||||||||||||||||||||||
| 24 | |||||||||||||||||||||||
| 25 | =head1 SYNOPSIS | ||||||||||||||||||||||
| 26 | |||||||||||||||||||||||
| 27 | use IO::Term::Status; | ||||||||||||||||||||||
| 28 | |||||||||||||||||||||||
| 29 | my $io = IO::Term::Status->new_for_stdout; | ||||||||||||||||||||||
| 30 | |||||||||||||||||||||||
| 31 | $io->set_status( "Running" ); | ||||||||||||||||||||||
| 32 | |||||||||||||||||||||||
| 33 | my @items = ...; | ||||||||||||||||||||||
| 34 | |||||||||||||||||||||||
| 35 | foreach my $idx ( 0 .. $#items ) { | ||||||||||||||||||||||
| 36 | $io->set_status( sprintf "Running | %d of %d", $idx+1, scalar @items ); | ||||||||||||||||||||||
| 37 | |||||||||||||||||||||||
| 38 | my $item = $items[$idx]; | ||||||||||||||||||||||
| 39 | $io->print_line( "Processing item $item..." ); | ||||||||||||||||||||||
| 40 | ... | ||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||
| 42 | |||||||||||||||||||||||
| 43 | $io->set_status( "" ); # Clear the status line before exiting | ||||||||||||||||||||||
| 44 | |||||||||||||||||||||||
| 45 | =head1 DESCRIPTION | ||||||||||||||||||||||
| 46 | |||||||||||||||||||||||
| 47 | This module provides a subclass of L<IO::Handle> for maintaining a running | ||||||||||||||||||||||
| 48 | status display on the terminal. It presumes the terminal can handle basic ANSI | ||||||||||||||||||||||
| 49 | control characters (thus is not suitable for printing to log files, etc). | ||||||||||||||||||||||
| 50 | |||||||||||||||||||||||
| 51 | The "status bar" consists of a single additional line of text, printed below | ||||||||||||||||||||||
| 52 | the current log of output. More lines of regular log can be printed using the | ||||||||||||||||||||||
| 53 | L</print_line> method, which maintains the running status bar below the | ||||||||||||||||||||||
| 54 | output. | ||||||||||||||||||||||
| 55 | |||||||||||||||||||||||
| 56 | =head2 With C<String::Tagged> | ||||||||||||||||||||||
| 57 | |||||||||||||||||||||||
| 58 | If the L<String::Tagged::Terminal> module is available, then the status string | ||||||||||||||||||||||
| 59 | can set to an instance of L<String::Tagged>, obeying the | ||||||||||||||||||||||
| 60 | L<String::Tagged::Formatting> tag conventions. This will be converted to | ||||||||||||||||||||||
| 61 | terminal output. | ||||||||||||||||||||||
| 62 | |||||||||||||||||||||||
| 63 | As an extra convenience, whatever the prevailing background colour is at the | ||||||||||||||||||||||
| 64 | end of the string will be preserved for line-erase purposes, meaning that | ||||||||||||||||||||||
| 65 | colour will extend the entire width of the status bar line. | ||||||||||||||||||||||
| 66 | |||||||||||||||||||||||
| 67 | =cut | ||||||||||||||||||||||
| 68 | |||||||||||||||||||||||
| 69 | *is_string_tagged = HAVE_STRING_TAGGED_TERMINAL ? | ||||||||||||||||||||||
| 70 | # It would be nice if we could #ifdef HAVE_PERL_VERSION(...) | ||||||||||||||||||||||
| 71 | ( $^V ge v5.32 ) ? | ||||||||||||||||||||||
| 72 | do { eval 'use experimental "isa"; sub { $_[0] isa String::Tagged }' } : | ||||||||||||||||||||||
| 73 | 0 | ▶ | do { require Scalar::Util; sub { Scalar::Util::blessed($_[0]) and $_[0]->isa( "String::Tagged" ) } } | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||
| 74 | 8 | ▶ | : sub { 0 }; | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 75 | |||||||||||||||||||||||
| 76 | =head1 CONSTRUCTORS | ||||||||||||||||||||||
| 77 | |||||||||||||||||||||||
| 78 | =head2 new | ||||||||||||||||||||||
| 79 | |||||||||||||||||||||||
| 80 | $io = IO::Term::Status->new | ||||||||||||||||||||||
| 81 | |||||||||||||||||||||||
| 82 | Constructs a new L<IO::Handle> subclassed instance of this type. | ||||||||||||||||||||||
| 83 | |||||||||||||||||||||||
| 84 | =head2 new_for_stdout | ||||||||||||||||||||||
| 85 | |||||||||||||||||||||||
| 86 | $io = IO::Term::Status->new_for_stdout | ||||||||||||||||||||||
| 87 | |||||||||||||||||||||||
| 88 | Constructs a new instance wrapping the C<STDOUT> filehandle, with autoflush | ||||||||||||||||||||||
| 89 | turned on. This is usually what you want for printing regular output to the | ||||||||||||||||||||||
| 90 | controlling terminal. | ||||||||||||||||||||||
| 91 | |||||||||||||||||||||||
| 92 | =cut | ||||||||||||||||||||||
| 93 | |||||||||||||||||||||||
| 94 | sub new_for_stdout | ||||||||||||||||||||||
| 95 | 0 | ▶ | { | ||||||||||||||||||||
|
Subroutine
new_for_stdout: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 96 | 0 | my $self = shift->new( @_ ); | |||||||||||||||||||||
| 97 | |||||||||||||||||||||||
| 98 | 0 | $self->fdopen( STDOUT->fileno, "w" ); | |||||||||||||||||||||
| 99 | 0 | $self->autoflush(1); | |||||||||||||||||||||
| 100 | |||||||||||||||||||||||
| 101 | 0 | return $self; | |||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||
| 103 | |||||||||||||||||||||||
| 104 | =head1 METHODS | ||||||||||||||||||||||
| 105 | |||||||||||||||||||||||
| 106 | =cut | ||||||||||||||||||||||
| 107 | |||||||||||||||||||||||
| 108 | sub _build_status | ||||||||||||||||||||||
| 109 | 8 | ▶ | { | ||||||||||||||||||||
|
Subroutine
_build_status: called CC=4 SCAR=17.2
| |||||||||||||||||||||||
| 110 | 8 | my ( $status ) = @_; | |||||||||||||||||||||
| 111 | |||||||||||||||||||||||
| 112 | 8 | ▶ | if( is_string_tagged( $status ) ) { | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 113 | 0 | my $termstr = String::Tagged::Terminal->new_from_formatting( $status ) | |||||||||||||||||||||
| 114 | ->build_terminal; | ||||||||||||||||||||||
| 115 | # Hack the EL in before any SGR reset at the end | ||||||||||||||||||||||
| 116 | 0 | ▶ | $termstr =~ s/\e\[m$/$EL\e[m/ | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 117 | or $termstr .= $EL; | ||||||||||||||||||||||
| 118 | 0 | return $termstr; | |||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||
| 120 | elsif( length $status ) { | ||||||||||||||||||||||
| 121 | 7 | return $status . $EL; | |||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||
| 123 | else { | ||||||||||||||||||||||
| 124 | 1 | return ""; | |||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||
| 127 | |||||||||||||||||||||||
| 128 | =head2 print_line | ||||||||||||||||||||||
| 129 | |||||||||||||||||||||||
| 130 | $io->print_line( @args ) | ||||||||||||||||||||||
| 131 | |||||||||||||||||||||||
| 132 | Prints a new line from the given arguments, joined as a string. C<@args> | ||||||||||||||||||||||
| 133 | should not contain the terminating linefeed. | ||||||||||||||||||||||
| 134 | |||||||||||||||||||||||
| 135 | This line is printed above any pending partial line. | ||||||||||||||||||||||
| 136 | |||||||||||||||||||||||
| 137 | =cut | ||||||||||||||||||||||
| 138 | |||||||||||||||||||||||
| 139 | sub print_line | ||||||||||||||||||||||
| 140 | 5 | ▶ | { | ||||||||||||||||||||
|
Subroutine
print_line: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||
| 141 | 5 | my $self = shift; | |||||||||||||||||||||
| 142 | 5 | my $partial = ${*$self}{its_partial}; | |||||||||||||||||||||
| 143 | 5 | my $status = ${*$self}{its_status}; | |||||||||||||||||||||
| 144 | |||||||||||||||||||||||
| 145 | 5 | ▶ | $self->print( join "", | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 146 | ( length $status ? ( | ||||||||||||||||||||||
| 147 | # Clear the current status first in case the line is wider than the | ||||||||||||||||||||||
| 148 | # terminal width | ||||||||||||||||||||||
| 149 | ( length $partial ? $CLEARLINE : () ), | ||||||||||||||||||||||
| 150 | "\n", $CLEARLINE, $PREVLINE, | ||||||||||||||||||||||
| 151 | ) : () ), | ||||||||||||||||||||||
| 152 | # Print the new content | ||||||||||||||||||||||
| 153 | @_, "\n", | ||||||||||||||||||||||
| 154 | ( length $status ? ( | ||||||||||||||||||||||
| 155 | # Leave an empty space for the partial | ||||||||||||||||||||||
| 156 | $CLEARLINE, "\n", | ||||||||||||||||||||||
| 157 | # Print the status | ||||||||||||||||||||||
| 158 | _build_status( $status ), | ||||||||||||||||||||||
| 159 | # Go back and print the partial | ||||||||||||||||||||||
| 160 | $PREVLINEHOME | ||||||||||||||||||||||
| 161 | ) : () ), | ||||||||||||||||||||||
| 162 | ( length $partial ? $partial : () ), | ||||||||||||||||||||||
| 163 | ); | ||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||
| 165 | |||||||||||||||||||||||
| 166 | =head2 more_partial | ||||||||||||||||||||||
| 167 | |||||||||||||||||||||||
| 168 | $io->more_partial( $more ) | ||||||||||||||||||||||
| 169 | |||||||||||||||||||||||
| 170 | Adds more text to the pending partial line displayed at the bottom, after any | ||||||||||||||||||||||
| 171 | complete lines. | ||||||||||||||||||||||
| 172 | |||||||||||||||||||||||
| 173 | =cut | ||||||||||||||||||||||
| 174 | |||||||||||||||||||||||
| 175 | sub more_partial | ||||||||||||||||||||||
| 176 | 6 | ▶ | { | ||||||||||||||||||||
|
Subroutine
more_partial: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 177 | 6 | my $self = shift; | |||||||||||||||||||||
| 178 | 6 | my ( $more ) = @_; | |||||||||||||||||||||
| 179 | |||||||||||||||||||||||
| 180 | 6 | ${*$self}{its_partial} .= $more; | |||||||||||||||||||||
| 181 | |||||||||||||||||||||||
| 182 | 6 | $self->print( $more ); | |||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||
| 184 | |||||||||||||||||||||||
| 185 | =head2 replace_partial | ||||||||||||||||||||||
| 186 | |||||||||||||||||||||||
| 187 | $io->replace_partial( $more ) | ||||||||||||||||||||||
| 188 | |||||||||||||||||||||||
| 189 | Replace the content of the pending partial line displayed at the bottom. | ||||||||||||||||||||||
| 190 | |||||||||||||||||||||||
| 191 | =cut | ||||||||||||||||||||||
| 192 | |||||||||||||||||||||||
| 193 | sub replace_partial | ||||||||||||||||||||||
| 194 | 1 | ▶ | { | ||||||||||||||||||||
|
Subroutine
replace_partial: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 195 | 1 | my $self = shift; | |||||||||||||||||||||
| 196 | 1 | my ( $partial ) = @_; | |||||||||||||||||||||
| 197 | |||||||||||||||||||||||
| 198 | 1 | ${*$self}{its_partial} = $partial; | |||||||||||||||||||||
| 199 | |||||||||||||||||||||||
| 200 | 1 | $self->print( $CLEARLINE . $partial ); | |||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||
| 202 | |||||||||||||||||||||||
| 203 | =head2 finish_partial | ||||||||||||||||||||||
| 204 | |||||||||||||||||||||||
| 205 | $io->finish_partial( $more ) | ||||||||||||||||||||||
| 206 | |||||||||||||||||||||||
| 207 | Adds more text to the pending partial line then turns it into a complete line | ||||||||||||||||||||||
| 208 | that gets printed. | ||||||||||||||||||||||
| 209 | |||||||||||||||||||||||
| 210 | =cut | ||||||||||||||||||||||
| 211 | |||||||||||||||||||||||
| 212 | sub finish_partial | ||||||||||||||||||||||
| 213 | 5 | ▶ | { | ||||||||||||||||||||
|
Subroutine
finish_partial: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||
| 214 | 5 | my $self = shift; | |||||||||||||||||||||
| 215 | 5 | my ( $more ) = @_; | |||||||||||||||||||||
| 216 | |||||||||||||||||||||||
| 217 | 5 | my $status = ${*$self}{its_status}; | |||||||||||||||||||||
| 218 | |||||||||||||||||||||||
| 219 | 5 | undef ${*$self}{its_partial}; | |||||||||||||||||||||
| 220 | |||||||||||||||||||||||
| 221 | 5 | ▶ | $self->print( join "", | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 222 | ( length $more ? $more : () ), | ||||||||||||||||||||||
| 223 | "\n", $CLEARLINE, | ||||||||||||||||||||||
| 224 | ( length $status ? ( | ||||||||||||||||||||||
| 225 | # Leave an empty space for the partial | ||||||||||||||||||||||
| 226 | "\n", | ||||||||||||||||||||||
| 227 | # Print the status | ||||||||||||||||||||||
| 228 | _build_status( $status ), | ||||||||||||||||||||||
| 229 | # Go back and print the partial | ||||||||||||||||||||||
| 230 | $PREVLINEHOME | ||||||||||||||||||||||
| 231 | ) : () ) | ||||||||||||||||||||||
| 232 | ); | ||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||
| 234 | |||||||||||||||||||||||
| 235 | =head2 set_status | ||||||||||||||||||||||
| 236 | |||||||||||||||||||||||
| 237 | $io->set_status( $status ) | ||||||||||||||||||||||
| 238 | |||||||||||||||||||||||
| 239 | Sets the status message string. | ||||||||||||||||||||||
| 240 | |||||||||||||||||||||||
| 241 | =cut | ||||||||||||||||||||||
| 242 | |||||||||||||||||||||||
| 243 | sub set_status | ||||||||||||||||||||||
| 244 | 3 | ▶ | { | ||||||||||||||||||||
|
Subroutine
set_status: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||
| 245 | 3 | my $self = shift; | |||||||||||||||||||||
| 246 | 3 | my ( $status ) = @_; | |||||||||||||||||||||
| 247 | |||||||||||||||||||||||
| 248 | 3 | my $partial = ${*$self}{its_partial}; | |||||||||||||||||||||
| 249 | |||||||||||||||||||||||
| 250 | 3 | ${*$self}{its_status} = $status; | |||||||||||||||||||||
| 251 | |||||||||||||||||||||||
| 252 | 3 | ▶ | $self->print( join "", | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 253 | # Move to status line | ||||||||||||||||||||||
| 254 | "\n", | ||||||||||||||||||||||
| 255 | # Reprint the status | ||||||||||||||||||||||
| 256 | $CLEARLINE, | ||||||||||||||||||||||
| 257 | _build_status( $status ), | ||||||||||||||||||||||
| 258 | # Go back and print the partial | ||||||||||||||||||||||
| 259 | $PREVLINEHOME, | ||||||||||||||||||||||
| 260 | ( length $partial ? $partial : () ), | ||||||||||||||||||||||
| 261 | ); | ||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||
| 263 | |||||||||||||||||||||||
| 264 | =head1 AUTHOR | ||||||||||||||||||||||
| 265 | |||||||||||||||||||||||
| 266 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||||
| 267 | |||||||||||||||||||||||
| 268 | =cut | ||||||||||||||||||||||
| 269 | |||||||||||||||||||||||
| 270 | 4 | 0x55AA; | |||||||||||||||||||||