tools/lib/Term/Fabulous/Screenshot/Screen.pm

filter: Statementstmt 41.2% 52 / 126 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 56.0% 14 / 25 Podpod 100% 6 / 6 totaltotal 32.7% 72 / 220 CC 29 SCAR 37.9
CC
29
Cov
29%
_apply_sgr
132.0
_parse_line
72.0
from_capture
30.0
CRAP 44.4
« blib/lib/Term/Fabulous/Render/Target/Grid.pm ↑ Summary tools/lib/Term/Fabulous/Screenshot/Scene.pm »
1 package Term::Fabulous::Screenshot::Screen;
2  
3 1 ▶use v5.32;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Object::Pad 0.825;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 class Term::Fabulous::Screenshot::Screen :strict(params) {
11 1 ▶ use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶ use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶ use MIME::Base64 qw(decode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶ use Term::Fabulous::Render::Attr qw(STYLE_FLAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶ use Term::Fabulous::Termbox qw(TB_HI_BLACK);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶ use Term::Fabulous::Unicode qw(grapheme_clusters cluster_columns);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶ use constant RGB_MASK => 0xFFFFFF;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # termbox2 attribute flags and SGR parameters, and the style each
21 # stands for; Term::Fabulous::Static writes the parameters from the
22 # same table.
23 1 ▶ use constant STYLE_BY_FLAG => map { $_->[0] => $_->[2] } STYLE_FLAGS;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶ use constant STYLE_BY_SGR => map { $_->[1] => $_->[2] } STYLE_FLAGS;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 0 ▶ field $columns :param :reader;
Subroutine columns: not called CC=1 SCAR=2.6
Pod documented
27 0 ▶ field $rows :param :reader;
Subroutine rows: not called CC=1 SCAR=2.6
Pod documented
28  
29 # One array per row of cells: { x, glyph, columns, fg, bg, styles }.
30 # fg and bg are 0xRRGGBB or undef for the terminal's default color;
31 # styles is a hash of the style names that apply.
32 1 field $cells :param;
33  
34 # The sixel pictures over the cells: { x, y, columns, rows, png }, the
35 # first cell, the cells covered and the PNG data.
36 field $pictures :param = [];
37  
38 1 ADJUST {
39 croak "Term::Fabulous::Screenshot::Screen: the screen needs at least one column and one row, got ${columns}x$rows"
40 unless $columns >= 1 && $rows >= 1;
41 croak "Term::Fabulous::Screenshot::Screen: $rows rows declared, " . scalar(@$cells) . " given" unless @$cells == $rows;
42 foreach my $picture (@$pictures) {
43 my ( $x, $y, $picture_columns, $picture_rows ) = @$picture{qw(x y columns rows)};
44 croak "Term::Fabulous::Screenshot::Screen: the picture of ${picture_columns}x$picture_rows cells at $x,$y is not inside the ${columns}x$rows screen"
45 unless $x >= 0 && $y >= 0 && $picture_columns >= 1 && $picture_rows >= 1 && $x + $picture_columns <= $columns && $y + $picture_rows <= $rows;
46 }
47 }
48  
49 0 ▶ method pictures () {
Subroutine pictures: not called CC=1 SCAR=4.1
Pod documented
50 0 return @$pictures;
51 1 }
52  
53 0 ▶ method row ($y) {
Subroutine row: not called CC=3 SCAR=24.8
Pod documented
54 0 ▶ croak "Term::Fabulous::Screenshot::Screen: there is no row $y" unless $y >= 0 && $y < $rows;
Branch: 1 branch
BranchTrueFalseTotal
unless $y >= 0 and $y < $rows 0 0 0
Condition0%0/3
$y >= 0 and $y < $rows
!ll&&!rl&&r
000
MC/DC0%0/2
$y >= 0 and $y < $rows
$y >= 0 $y < $rows
Truth table0%0/3
A and B
A= $y >= 0B= $y < $rows
ABresult
0X0
100
111
55 0 return @{ $cells->[$y] };
56 1 }
57  
58 # A screen read from termbox2's front buffer by the harness:
59 # { columns, rows, cells => [ [ [ glyph, columns, fg, bg ], ... ], ... ],
60 # pictures => [ { x, y, columns, rows, png => BASE64 }, ... ] }.
61 0 ▶ sub from_capture ( $class, $capture ) {
Subroutine from_capture: not called CC=5 SCAR=34.0
Pod documented
62 0 my ( $width, $height ) = @$capture{qw(columns rows)};
63 0 my @rows;
64 0 foreach my $captured_row ( @{ $capture->{cells} } ) {
65 0 my ( $x, @row ) = (0);
66 0 foreach my $captured (@$captured_row) {
67 0 my ( $glyph, $glyph_columns, $fg, $bg ) = @$captured;
68 0 push @row, _cell( $x, $glyph, $glyph_columns, _attr_rgb($fg), _attr_rgb($bg), _attr_styles( $fg | $bg ) );
69 0 $x += $glyph_columns;
70 }
71 0 ▶ croak "Term::Fabulous::Screenshot::Screen: captured row " . scalar(@rows) . " covers $x columns instead of $width" unless $x == $width;
Branch: 1 branch
BranchTrueFalseTotal
unless $x == $width 0 0 0
72 0 push @rows, \@row;
73 }
74 my @pictures = map {
75 0 { %$_, png => decode_base64( $_->{png} ) }
76 0 ▶ } @{ $capture->{pictures} // [] };
Condition0%0/2
$capture->{"pictures"} // []
l!l
00
MC/DC0%0/1
$capture->{"pictures"} // []
$capture->{"pictures"}
Truth table0%0/2
A
A= $capture->{"pictures"}
Aresult
11
00
77 0 return $class->new( columns => $width, rows => $height, cells => \@rows, pictures => \@pictures );
78 }
79  
80 # A screen from what a program printed into a terminal $width columns
81 # wide: UTF-8 text with the SGR sequences Term::Fabulous::Static writes,
82 # lines ending in CR LF (the terminal turns LF into CR LF). Each line
83 # is one row; trailing empty lines are dropped.
84 0 ▶ sub from_output ( $class, $bytes, $width ) {
Subroutine from_output: not called CC=4 SCAR=30.0
Pod documented
85 0 my $text = decode( 'UTF-8', $bytes, Encode::FB_CROAK | Encode::LEAVE_SRC );
86 0 my @lines = split /\r\n/, $text, -1;
87 0 ▶ pop @lines while @lines && $lines[-1] eq '';
Branch: 1 branch
BranchTrueFalseTotal
@lines and $lines[-1] eq "" and do { pop @lines } 0 0 0
Condition0%0/3
@lines and $lines[-1] eq ""
!ll&&!rl&&r
000
MC/DC0%0/2
@lines and $lines[-1] eq ""
@lines $lines[-1] eq ""
Truth table0%0/3
A and B
A= @linesB= $lines[-1] eq ""
ABresult
0X0
100
111
88 0 ▶ croak 'Term::Fabulous::Screenshot::Screen: the program printed nothing' unless @lines;
Branch: 1 branch
BranchTrueFalseTotal
unless @lines 0 0 0
89  
90 0 my @rows = map { _parse_line( $lines[$_], $_, $width ) } 0 .. $#lines;
91 0 return $class->new( columns => $width, rows => scalar(@rows), cells => \@rows );
92 }
93  
94 0 ▶ sub _cell ( $x, $glyph, $glyph_columns, $fg, $bg, $styles ) {
Subroutine _cell: not called CC=1 SCAR=6.9
95 0 return { x => $x, glyph => $glyph, columns => $glyph_columns, fg => $fg, bg => $bg, styles => $styles };
96 }
97  
98 0 ▶ sub _attr_rgb ($attr) {
Subroutine _attr_rgb: not called CC=3 SCAR=24.8
99 0 ▶ return 0 if $attr & TB_HI_BLACK;
Branch: 1 branch
BranchTrueFalseTotal
if $attr & 536870912 0 0 0
100 0 my $rgb = $attr & RGB_MASK;
101 0 ▶ return $rgb == 0 ? undef : $rgb;
Branch: 1 branch
BranchTrueFalseTotal
$rgb == 0 ? : 0 0 0
102 }
103  
104 0 ▶ sub _attr_styles ($attrs) {
Subroutine _attr_styles: not called CC=1 SCAR=6.9
105 0 my %style_by_flag = STYLE_BY_FLAG;
106 0 return { map { $style_by_flag{$_} => 1 } grep { $attrs & $_ } keys %style_by_flag };
107 }
108  
109 # Splits a line into SGR sequences and text, and lays the text out in
110 # cells with the current style. Any other control sequence means the
111 # output is not a printed report.
112 0 ▶ sub _parse_line ( $line, $number, $width ) {
Subroutine _parse_line: not called CC=8 SCAR=42.8
113 0 my %style = ( fg => undef, bg => undef, styles => {} );
114 0 my @row;
115 0 my $x = 0;
116 0 foreach my $token ( split /(\e\[[0-9;]*m)/, $line ) {
117 0 ▶ next unless length $token;
Branch: 1 branch
BranchTrueFalseTotal
unless length $token 0 0 0
118 0 ▶ if ( $token =~ /\A\e\[([0-9;]*)m\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ /\A\e\[([0-9;]*)m\z/u) 0 0 0
119 0 _apply_sgr( \%style, $1, $number );
120 0 next;
121 }
122 0 ▶ if ( $token =~ /([\x00-\x1f\x7f])/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ /([\x00-\x1f\x7f])/u) 0 0 0
123 0 croak sprintf "Term::Fabulous::Screenshot::Screen: line %d of the output contains the control character 0x%02x; "
124 . "only text and color sequences can be turned into a screenshot (did a full-screen program end before the screenshot was taken?)",
125 $number + 1, ord $1;
126 }
127 0 foreach my $cluster ( grapheme_clusters($token) ) {
128 0 my $cluster_columns = cluster_columns($cluster);
129 0 push @row, _cell( $x, $cluster, $cluster_columns, $style{fg}, $style{bg}, { %{ $style{styles} } } );
130 0 $x += $cluster_columns;
131 }
132 }
133 0 ▶ croak "Term::Fabulous::Screenshot::Screen: line " . ( $number + 1 ) . " of the output is $x columns wide, the terminal only $width" if $x > $width;
Branch: 1 branch
BranchTrueFalseTotal
if $x > $width 0 0 0
134 0 push @row, _cell( $_, ' ', 1, undef, undef, {} ) foreach $x .. $width - 1;
135 0 return \@row;
136 }
137  
138 0 ▶ sub _apply_sgr ( $style, $parameters, $line_number ) {
Subroutine _apply_sgr: not called CC=11 SCAR=48.8
139 0 ▶ my @codes = length $parameters ? split( /;/, $parameters, -1 ) : (0);
Branch: 1 branch
BranchTrueFalseTotal
length $parameters ? : 0 0 0
140 0 my %style_by_sgr = STYLE_BY_SGR;
141 0 ▶ while (@codes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@codes) 0 0 0
142 0 my $code = shift @codes;
143 0 ▶ if ( $code eq '' || $code == 0 ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($code eq '' or $code == 0) { } 0 0 0
elsif (exists $style_by_sgr{$code}) { } 0 0 0
elsif ($code == 38 || $code == 48 and @codes >= 4 and $codes[0] == 2) { } 0 0 0
Condition0%0/3
$code eq "" or $code == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$code eq "" or $code == 0
$code eq "" $code == 0
Truth table0%0/3
A or B
A= $code eq ""B= $code == 0
ABresult
1X1
011
000
144 0 %$style = ( fg => undef, bg => undef, styles => {} );
145 }
146 elsif ( exists $style_by_sgr{$code} ) {
147 0 $style->{styles}{ $style_by_sgr{$code} } = 1;
148 }
149 ▶ elsif ( ( $code == 38 || $code == 48 ) && @codes >= 4 && $codes[0] == 2 ) {
Condition0%0/9
$code == 38 || $code == 48 and @codes >= 4 and $codes[0] == 2
!ll&&!rl&&r
000
$code == 38 || $code == 48 and @codes >= 4
!ll&&!rl&&r
000
$code == 38 or $code == 48
l!l&&r!l&&!r
000
MC/DC0%0/4
$code == 38 || $code == 48 and @codes >= 4 and $codes[0] == 2
$code == 38 $code == 48 @codes >= 4 $codes[0] == 2
Truth table0%0/7
A or B and C and D
A= $code == 38B= $code == 48C= @codes >= 4D= $codes[0] == 2
ABCDresult
00XX0
1X0X0
010X0
1X100
01100
1X111
01111
150 0 my ( undef, $red, $green, $blue ) = splice @codes, 0, 4;
151 0 ▶ $style->{ $code == 38 ? 'fg' : 'bg' } = ( $red << 16 ) | ( $green << 8 ) | $blue;
Branch: 1 branch
BranchTrueFalseTotal
$code == 38 ? : 0 0 0
152 }
153 else {
154 0 croak "Term::Fabulous::Screenshot::Screen: line " . ( $line_number + 1 ) . " of the output uses the unsupported SGR parameters '$parameters'";
155 }
156 }
157 0 return;
158 }
159 }
160  
161 1 1;
162  
163 __END__
« blib/lib/Term/Fabulous/Render/Target/Grid.pm ↑ Summary tools/lib/Term/Fabulous/Screenshot/Scene.pm »