blib/lib/Term/Scroller.pm

filter: Statementstmt 97.8% 135 / 138 Branchbran 84.7% 39 / 46 Conditioncond 65.0% 26 / 40 MC/DCmcdc 33.3% 7 / 21 Subroutinesub 100% 18 / 18 Podpod 100% 3 / 3 totaltotal 85.7% 228 / 266 CC 44 SCAR 32.1
CC
44
Cov
89%
new
39.7
_crop_to_width
8.1
_parse_window_spec
2.0
CRAP 24.8
↑ Summary Build »
1 package Term::Scroller;
2  
3 5 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use feature 'unicode_strings';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 our $VERSION = '1.2';
10  
11 =head1 NAME
12  
13 Term::Scroller - Display text in a scrolling viewport in your terminal
14  
15 =head1 SYNOPSIS
16  
17 use Term::Scroller;
18
19 # Default options
20 my $scroll = Term::Scroller->new();
21 print $scroll "blah blah blah\n" for (1..100);
22 # You should always call the end() method when you're done
23 $scroll->end();
24  
25 # Some more options (limited window size, with a border)
26 $scroll = scroller(width => 40, height => 5, window => '-#-#-#-#');
27 print $scroll "beee daah booo\n" for (1..100);
28 $scroll->end();
29  
30 # See the rest of the documentation for more options!
31  
32 =head1 DESCRIPTION
33  
34 B<Term::Scroller> provides you with a way to view a large stream of text
35 inside a small, scrolling viewport on your terminal. The size and borders of
36 the viewport/window can be customized along with a number of other control
37 options.
38  
39 For a command-line program that uses this to display the output of commands
40 see L<scroller>.
41  
42 =cut
43  
44 5 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 5 ▶use Symbol qw(qualify_to_ref);
Subroutine BEGIN: called CC=1 SCAR=0.0
48 5 ▶use Scalar::Util qw(openhandle);
Subroutine BEGIN: called CC=1 SCAR=0.0
49 5 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 5 ▶use IO::Pty;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 5 ▶use Term::ReadKey qw(GetTerminalSize);
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 5 ▶use Term::Scroller::Linefeed qw(linefeed);
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 5 our @ISA = qw(IO::Pty);
57  
58 =head1 Methods
59  
60 =head2 new
61  
62 Returns a new scroller instance. The scroller is a filehandle
63 (actually an instance of L<IO::Pty>) where anything written to
64 it is displayed in the scrolling window. The scroller will sanitize any
65 cursor-related ANSI escape sequences so text that expects them to work might
66 look garbled, but at least it should be very difficult to escape or break the
67 window. color-related ANSI sequences are left untouched though so colored
68 text will still work inside the window.
69  
70 I<Don't forget to call the 'end' method when you're done with it!>
71  
72 The arguments to B<scroller> are interpreted as a hash and can contain the
73 following options:
74  
75 =head3 Size & Styling Options
76  
77 =over 4
78  
79 =item * B<height>
80  
81 Integer representing the maximum height (in lines) of the viewport. The window
82 will grow as more lines of input come in and start scrolling once it reaches
83 this height. (B<Default>: 10)
84  
85 =item * B<width>
86  
87 Integer representing the maximum width (in columns) of the viewport. Input lines
88 will truncated at this width, not including ANSI escape sequences.
89 (B<Default>: the width of the connected terminal, or 80 if the width
90 couldn't be determined for some reason).
91  
92 =item * B<tabwidth>
93  
94 Integer representing the width of tabs (in characters) when viewed in the
95 viewport. For consistent printing, tabs are replaced with this number of
96 spaces. (B<Default>: 4)
97  
98 =item * B<style>
99  
100 A string representing an ANSI color escape sequence used to style the text
101 displayed inside the window. If this is set, it will override any color
102 escape sequences in the input text. This can be a raw escape sequence (e.g
103 "\033[2m" for faded text) or a constant from the L<Term::ANSIColor> module.
104 (B<Default>: undef).
105  
106 =item * B<window>
107  
108 A string specifying the characters to use when drawing a border around the
109 window. See the B<WINDOW DRAWING> section for how to create a border
110 specification. The documentation for L<scroller> also has some examples you
111 can copy from. (B<Default>: undef, so no borders, if only we could live
112 in such a world)
113  
114 =back
115  
116 =head3 Control Options
117  
118 =over 4
119  
120 =item * B<out>
121  
122 Filehandle to write/draw the window to. Naturally, this should be connected to
123 a terminal. (B<Default>: the currently selected filehandle, so C<STDOUT> unless
124 you selected something else first).
125  
126 =item * B<hide>
127  
128 If true, the window will be erased once its done (i.e. once you close the
129 input filehandle). Otherwise, the window remains with the last lines of text
130 still visible. (B<Default>: False).
131  
132 =item * B<passthrough>
133  
134 If this is an open filehandle, input text will also be passed through to this
135 filehandle completely unaltered. Useful if you want a record of all the text
136 that went through the window.
137  
138 =back
139  
140 =cut
141  
142 sub new {
143 9 ▶ my $class = shift;
Subroutine new: called CC=36 SCAR=36.8
Pod documented
144  
145 9 my %params = @_;
146 9 ▶ my $outfh = $params{out} // qualify_to_ref(select);
Condition33%1/3
$params{"out"} // &qualify_to_ref(select)
l!l&&r!l&&!r
900
MC/DC0%0/2
$params{"out"} // &qualify_to_ref(select)
$params{"out"} &qualify_to_ref(select)
Truth table33%1/3
A // B
A= $params{"out"}B= &qualify_to_ref(select)
ABresult
1X1
011
000
147 9 ▶ my $buf_height = $params{height} // 10;
Condition100%2/2
$params{"height"} // 10
l!l
36
MC/DC100%1/1
$params{"height"} // 10
$params{"height"}
Truth table100%2/2
A
A= $params{"height"}
Aresult
11
00
148 9 ▶ my $buf_width = $params{width} // (GetTerminalSize $outfh)[0] // 80;
Condition80%4/5
($params{"width"} // (GetTerminalSize($outfh))[0]) // 80
l!l
36
$params{"width"} // (GetTerminalSize($outfh))[0]
l!l&&r!l&&!r
306
MC/DC50%1/2
($params{"width"} // (GetTerminalSize($outfh))[0]) // 80
$params{"width"} (GetTerminalSize($outfh))[0]
Truth table66%2/3
A // B
A= $params{"width"}B= (GetTerminalSize($outfh))[0]
ABresult
1X1
011
000
149 9 ▶ my $tab_width = $params{tabwidth} // 4;
Condition100%2/2
$params{"tabwidth"} // 4
l!l
45
MC/DC100%1/1
$params{"tabwidth"} // 4
$params{"tabwidth"}
Truth table100%2/2
A
A= $params{"tabwidth"}
Aresult
11
00
150 9 my $style = $params{style};
151 9 my $windowspec = $params{window};
152 9 ▶ my $hide = $params{hide} // 0;
Condition100%2/2
$params{"hide"} // 0
l!l
27
MC/DC100%1/1
$params{"hide"} // 0
$params{"hide"}
Truth table100%2/2
A
A= $params{"hide"}
Aresult
11
00
153 9 my $passthru = $params{passthrough};
154  
155 9 my $pty = IO::Pty->new;
156  
157 9 ▶ defined(my $pid = fork) or croak "unable to fork: $!";
Branch: 1 branch
BranchTrueFalseTotal
defined(my $pid = fork) or croak("unable to fork: $!") 0 9 9
158  
159 # Parent: Return the new scroller
160 9 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 6 3 9
161 6 bless $pty => $class;
162 6 ${*$pty}{'term_scroller_pid'} = $pid;
163 6 return $pty;
164 }
165  
166 ################################################
167 # Forked child: reads pty and writes to output
168 ################################################
169  
170 3 close $pty;
171 3 select $outfh;
172  
173 3 my @buf;
174  
175 3 my $tab = " "x$tab_width;
176  
177 # Parse window
178 3 my $line_end = "";
179 3 my $line_start = "";
180 3 my $window_top;
181 my $window_bot;
182 3 my $window_extra_height = 0; # Height of window top + bottom
183  
184 3 ▶ if (defined $windowspec) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $windowspec) 1 2 3
185 1 my %window = _parse_window_spec($windowspec);
186 1 ▶ $line_start = $window{left} // "";
Condition50%1/2
$window{"left"} // ""
l!l
10
MC/DC0%0/1
$window{"left"} // ""
$window{"left"}
Truth table50%1/2
A
A= $window{"left"}
Aresult
11
00
187 1 ▶ $line_end = $window{right} // "";
Condition50%1/2
$window{"right"} // ""
l!l
10
MC/DC0%0/1
$window{"right"} // ""
$window{"right"}
Truth table50%1/2
A
A= $window{"right"}
Aresult
11
00
188  
189 1 ▶ if ($window{hastop}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($window{"hastop"}) 1 0 1
190 1 $window_extra_height++;
191 $window_top = ( $window{topleft} // " " ) .
192 ( $window{top} // " " ) x ($buf_width-2) .
193 1 ▶ ( $window{topright} // " " );
Condition50%3/6
$window{"topleft"} // " "
l!l
10
$window{"top"} // " "
l!l
10
$window{"topright"} // " "
l!l
10
MC/DC0%0/1
$window{"topleft"} // " "
$window{"topleft"}
MC/DC0%0/1
$window{"top"} // " "
$window{"top"}
MC/DC0%0/1
$window{"topright"} // " "
$window{"topright"}
Truth table50%1/2
A
A= $window{"topleft"}
Aresult
11
00
Truth table50%1/2
A
A= $window{"top"}
Aresult
11
00
Truth table50%1/2
A
A= $window{"topright"}
Aresult
11
00
194 }
195 1 ▶ if ($window{hasbot}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($window{"hasbot"}) 1 0 1
196 1 $window_extra_height++;
197 $window_bot = ( $window{botleft} // " " ) .
198 ( $window{bot} // " " ) x ($buf_width-2) .
199 1 ▶ ( $window{botright} // " " );
Condition50%3/6
$window{"botleft"} // " "
l!l
10
$window{"bot"} // " "
l!l
10
$window{"botright"} // " "
l!l
10
MC/DC0%0/1
$window{"botleft"} // " "
$window{"botleft"}
MC/DC0%0/1
$window{"bot"} // " "
$window{"bot"}
MC/DC0%0/1
$window{"botright"} // " "
$window{"botright"}
Truth table50%1/2
A
A= $window{"botleft"}
Aresult
11
00
Truth table50%1/2
A
A= $window{"bot"}
Aresult
11
00
Truth table50%1/2
A
A= $window{"botright"}
Aresult
11
00
200 }
201  
202 1 $buf_width -= ( length($line_start) + length($line_end) );
203 }
204  
205 3 my $firstline = 1;
206  
207 3 ▶ while (my $line = linefeed($pty)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = linefeed($pty)) 22 3 25
208  
209 22 ▶ if ($firstline) {
Branch: 1 branch
BranchTrueFalseTotal
if ($firstline) 3 19 22
210 3 ▶ print "$window_top\n" if defined $window_top;
Branch: 1 branch
BranchTrueFalseTotal
if defined $window_top 1 2 3
211 3 ▶ print "$window_bot\n" if defined $window_bot;
Branch: 1 branch
BranchTrueFalseTotal
if defined $window_bot 1 2 3
212 3 $firstline = 0;
213 }
214  
215 22 chomp $line;
216 22 $line =~ s/\t/$tab/g;
217 22 my $to_print = "";
218  
219 22 ▶ if (defined $style) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $style) { } 4 18 22
220 # Remove all escape sequences
221 4 $line =~ s/\033\[\d*(?>(;\d*)+)*[A-HJKSTfm]//g;
222 # Pad
223 4 ▶ if (length $line < $buf_width and $line_end) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $line < $buf_width and $line_end) 0 4 4
Condition33%1/3
length $line < $buf_width and $line_end
!ll&&!rl&&r
040
MC/DC0%0/2
length $line < $buf_width and $line_end
length $line < $buf_width $line_end
Truth table33%1/3
A and B
A= length $line < $buf_widthB= $line_end
ABresult
0X0
100
111
224 0 $line .= " " x ($buf_width - length $line);
225 }
226 # Crop to buffer, add style
227 4 $to_print = $style . (substr $line, 0, $buf_width) . "\033[0m";
228 }
229 else {
230 # Remove cursor-changing escape sequences
231 18 $line =~ s/\033\[\d*(?>(;\d*)+)*[A-HJKSTf]//g;
232 # Crop to buffer, keeping remaining escapes intact
233 18 ▶ $to_print = _crop_to_width($line, $buf_width, $line_end ? " " : "");
Branch: 1 branch
BranchTrueFalseTotal
$line_end ? : 14 4 18
234 }
235  
236 22 $to_print = $line_start . $to_print . $line_end;
237  
238 # Uncomment to just print line
239 #print "$to_print\n"; next;
240  
241 # Print next frame:
242 22 my $height = @buf + $window_extra_height; # Reset cursor back to top
243 22 ▶ print "\033[$height;F" if $height > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $height > 0 20 2 22
244 22 push @buf, $to_print; # Add line to buffer and rotate
245 22 ▶ shift @buf if @buf > $buf_height;
Branch: 1 branch
BranchTrueFalseTotal
if @buf > $buf_height 9 13 22
246 22 ▶ print "$window_top\n" if defined $window_top; # Print frame
Branch: 1 branch
BranchTrueFalseTotal
if defined $window_top 14 8 22
247 22 print "$_\033[K\n" for (@buf);
248 22 print "\033[0m";
249 22 ▶ print "$window_bot\n" if defined $window_bot;
Branch: 1 branch
BranchTrueFalseTotal
if defined $window_bot 14 8 22
250 }
251  
252 3 ▶ close $passthru if openhandle($passthru);
Branch: 1 branch
BranchTrueFalseTotal
if &openhandle($passthru) 0 3 3
253  
254 3 ▶ if ($hide) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hide) 1 2 3
255 # Erase buffer
256 1 print "\033[1;F\033[K" for (1..@buf+$window_extra_height)
257 }
258  
259 exit
260 3 }
261  
262 =head2 pid
263  
264 Every scroller instance uses a forked child process that reads the
265 psuedoterminal and draws the window to the terminal. This method returns the
266 pid of an instance's associated fork.
267 =cut
268 sub pid {
269 6 ▶ my $self = shift;
Subroutine pid: called CC=1 SCAR=0.0
Pod documented
270 6 return ${*$self}{'term_scroller_pid'};
271 }
272  
273 =head2 end
274  
275 This is the preferred way to stop using a scroller. Simply closing the
276 filehandle will immediately destroy the allocated pty which means any text that
277 the window hasn't drawn yet will be lost. This method will safely wait for the
278 child process to catch up before closing by sending an explicit C<EOF> to the
279 pty, waiting on the child process and then finally closing the filehandle.
280 Returns the exit status of the instance's child process.
281 =cut
282 sub end {
283 6 ▶ my $self = shift;
Subroutine end: called CC=1 SCAR=0.0
Pod documented
284 6 print $self "\04\n\04";
285 6 $self->flush;
286 6 waitpid($self->pid, 0);
287 6 return $?;
288 }
289  
290 =head1 WINDOW DRAWING
291  
292 A window specification is a string up to 8 characters long indicating which character
293 to use for a part of the window, in clockwise order. That is, the characters
294 specify the top side, top-right corner, right side, bottom-right corner,
295 bottom side, bottom-left corner, left side and top-left corner respectively.
296 If any character is a whitespace or is missing (due to the string not being
297 long enough), then that part of the window will not be drawn.
298  
299 =cut
300  
301 #_crop_to_width STRING, LENGTH, PADDING
302 #
303 #Cut the given string down to LENGTH characters while keeping any
304 #SGR ANSI esccape sequences intact. Return the new string.
305 #If PADDING is specified, the final string will be padded with the
306 #value of PADDING to reach the length of the buffer.
307 sub _crop_to_width {
308 18 ▶ my $in = shift;
Subroutine _crop_to_width: called CC=8 SCAR=20.9
309 18 my $len = shift;
310 18 my $pad = shift;
311  
312 # We need to crop the line to the width of the buffer, but keep
313 # any SGR (color/text-style) escape sequences intact. To do this,
314 # we split the input line into chunks consisting of a run of
315 # non-escape sequence characters optionally followed by one escape
316 # sequence. We use these to rebuild the line that we're gonna print,
317 # keeping all escape sequences, but stopping the regular text
318 # at the width of the buffer.
319  
320 18 my $out = ""; # line that will eventually get printed
321 18 my $text_length = 0; # length of text sequences so far
322 18 my $sgr_split = qr{ # regex to split into text+sgr sequences
323 (?<TEXT> .*? )
324 (?<SGR> \033\[\d+(?>(;\d*)+)*m )?
325 }x;
326  
327 # Iterate through matches
328 18 ▶ while ($in =~ m/$sgr_split/cg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in =~ /$sgr_split/cgu) 1217 18 1235
329 1217 ▶ my $text = $+{TEXT} // "";
Condition50%1/2
$+{"TEXT"} // ""
l!l
12170
MC/DC0%0/1
$+{"TEXT"} // ""
$+{"TEXT"}
Truth table50%1/2
A
A= $+{"TEXT"}
Aresult
11
00
330 1217 ▶ my $sgr = $+{SGR} // "";
Condition100%2/2
$+{"SGR"} // ""
l!l
21215
MC/DC100%1/1
$+{"SGR"} // ""
$+{"SGR"}
Truth table100%2/2
A
A= $+{"SGR"}
Aresult
11
00
331  
332 # Add text if we haven't yet passed the buffer width
333 1217 ▶ if ($text_length < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text_length < $len) { } 873 344 1217
334 873 $text_length += length($text);
335 # Crop to buffer width if we went over
336 873 ▶ if ($text_length > $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text_length > $len) 0 873 873
337 0 $text = substr $text, 0, -( $text_length - $len );
338 0 $text_length = $len;
339 }
340 # If we've already passed the buffer width, no more text
341 } else {
342 344 $text = "";
343 }
344  
345 1217 $out .= $text.$sgr;
346 }
347  
348 18 ▶ if ($pad and $text_length < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pad and $text_length < $len) 6 12 18
Condition100%3/3
$pad and $text_length < $len
!ll&&!rl&&r
486
MC/DC100%2/2
$pad and $text_length < $len
$pad $text_length < $len
Truth table100%3/3
A and B
A= $padB= $text_length < $len
ABresult
0X0
100
111
349 6 $out .= $pad x ( $len - $text_length );
350 }
351  
352 18 return $out;
353 }
354  
355 #_parse_window_spec EXPR
356 #
357 #Parse a window spec string and return a hash with the following fields for
358 #the pieces of the window (in clockwise order): top, topright, right, botright,
359 #bot, botleft, left, topleft. Any one of these fields may be undefined to
360 #indicate that it should be blank.
361 #
362 #The hash also has fields for each side of the window with a boolean value
363 #indicating whether that side is to be drawn (at least one of its side and
364 #two corners are specified): hastop, hasright, hasbot, hasleft.
365 sub _parse_window_spec {
366 1 ▶ my $spec = shift;
Subroutine _parse_window_spec: called CC=2 SCAR=7.0
367  
368 1 my %window;
369 @window{qw' top topright right botright bot botleft left topleft '} =
370 1 ▶ map { m/\s/ ? undef : $_ } split(//, $spec);
Branch: 1 branch
BranchTrueFalseTotal
/\s/u ? : 0 8 8
371  
372 @window{qw' hastop hasright hasbot hasleft '} =
373 1 ▶ map { any {defined $window{$_} } @$_ } (
Subroutine __ANON__: called CC=1 SCAR=0.0
374 [qw' topleft top topright '], # top side
375 [qw' topright right botright '], # right side
376 [qw' botright bot botleft '], # bottom side
377 [qw' botleft left topleft '] # left side
378 );
379  
380 1 return %window;
381 }
382  
383 5 1;
384  
385 =head1 SEE ALSO
386  
387 L<scroller>
388  
389 =head1 AUTHOR
390  
391 Cameron Tauxe C<camerontauxe@gmail.com>
392  
393 =head1 LICENSE AND COPYRIGHT
394  
395 This software is copyright (c) 2020 by Cameron Tauxe.
396  
397 This is free software; you can redistribute it and/or modify it under
398 the same terms as the Perl 5 programming language system itself.
399  
400 =cut
↑ Summary Build »