blib/lib/Term/Scroller.pm

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