blib/lib/Term/Fabulous/Render/Target/Grid.pm

filter: Statementstmt 29.9% 56 / 187 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 32 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 41.3% 12 / 29 Podpod 100% 14 / 14 totaltotal 26.3% 82 / 311 CC 35 SCAR 43.7
CC
35
Cov
22%
row_text
272.0
clear_cells
42.0
_color_codes
30.0
CRAP 78.7
« blib/lib/Term/Fabulous/Render/Border.pm ↑ Summary tools/lib/Term/Fabulous/Screenshot/Screen.pm »
1 package Term::Fabulous::Render::Target::Grid;
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 our $VERSION = '0.05';
9  
10 1 ▶use Object::Pad 0.825;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Term::Fabulous::Render::Target::Mask;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Term::Fabulous::Render::Target::Sixel;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 class Term::Fabulous::Render::Target::Grid
16 :does(Term::Fabulous::Render::Target::Mask)
17 :does(Term::Fabulous::Render::Target::Sixel)
18 :strict(params)
19 {
20 1 ▶ use Term::Fabulous::Render::Attr qw(STYLE_FLAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶ use Term::Fabulous::Termbox qw(TB_DEFAULT TB_HI_BLACK TB_REVERSE);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶ use Term::Fabulous::Unicode qw(string_columns);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 ▶ use constant RESET => "\e[0m";
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # Term::Fabulous::Render::Attr packs the color into the low 24 bits and
27 # the flags above them.
28 1 ▶ use constant COLOR_MASK => 0xFFFFFF;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 # Painted cells, indexed [y][x]; a cell is [ $glyph, $fg, $bg ] or undef
31 # when nothing has been painted there. A wide cluster is stored only in
32 # the cell it starts in; its continuation cells keep whatever was
33 # painted there before (undef on a cleared grid).
34 1 field @rows;
35  
36 # The pixels of a cell, for a grid that stands for a terminal showing
37 # sixel; the pictures of the last frame.
38 1 field @_cell_size;
39 1 field @_sixels;
40  
41 ADJUST :params ( :$sixel_cell_size = undef ) {
42 die "Term::Fabulous::Render::Target::Grid: sixel_cell_size must be [width, height] in pixels, both at least 1"
43 if defined $sixel_cell_size && !( ref $sixel_cell_size eq 'ARRAY' && @$sixel_cell_size == 2 && !grep { !defined || !/\A[1-9][0-9]*\z/ } @$sixel_cell_size );
44 @_cell_size = @{ $sixel_cell_size // [] };
45 }
46  
47 0 ▶ method sixel_cell_size () {
Subroutine sixel_cell_size: not called CC=1 SCAR=4.1
Pod documented
48 0 return @_cell_size;
49 1 }
50  
51 0 ▶ method sixel_area ( $width, $height ) {
Subroutine sixel_area: not called CC=1 SCAR=6.9
Pod documented
52 0 return [ 0, 0, $width, $height ];
53 1 }
54  
55 0 ▶ method show_sixels (@placements) {
Subroutine show_sixels: not called CC=1 SCAR=6.9
Pod documented
56 @_sixels = map {
57 0 { %$_ }
58 } @placements;
59 0 return;
60 1 }
61  
62 0 ▶ method sixels () {
Subroutine sixels: not called CC=1 SCAR=6.9
Pod documented
63 return map {
64 0 { %$_ }
65 } @_sixels;
66 1 }
67  
68 0 ▶ method clear_cells (@kept_rects) {
Subroutine clear_cells: not called CC=6 SCAR=37.4
Pod documented
69 0 my @kept_rows;
70 0 foreach my $rect (@kept_rects) {
71 0 my ( $x0, $y0, $x1, $y1 ) = @$rect;
72 0 foreach my $y ( $y0 .. $y1 - 1 ) {
73 0 ▶ my $row = $rows[$y] // next;
Condition0%0/2
$rows[$y] // (next)
l!l
00
MC/DC0%0/1
$rows[$y] // (next)
$rows[$y]
Truth table0%0/2
A
A= $rows[$y]
Aresult
11
00
74 0 ▶ ( $kept_rows[$y] //= [] )->[$_] = $row->[$_] foreach $x0 .. $x1 - 1;
Condition0%0/2
$kept_rows[$y] //= []
l!l
00
MC/DC0%0/1
$kept_rows[$y] //= []
$kept_rows[$y]
Truth table0%0/2
A
A= $kept_rows[$y]
Aresult
11
00
75 }
76 }
77 0 @rows = @kept_rows;
78 0 return;
79 1 }
80  
81 0 ▶ method present_cells () {
Subroutine present_cells: not called CC=1 SCAR=6.9
Pod documented
82 0 return;
83 1 }
84  
85 0 ▶ method put_cell ( $x, $y, $glyph, $fg, $bg ) {
Subroutine put_cell: not called CC=2 SCAR=17.9
Pod documented
86 0 ▶ ( $rows[$y] //= [] )->[$x] = [ $glyph, $fg, $bg ];
Condition0%0/2
$rows[$y] //= []
l!l
00
MC/DC0%0/1
$rows[$y] //= []
$rows[$y]
Truth table0%0/2
A
A= $rows[$y]
Aresult
11
00
87 0 return;
88 1 }
89  
90 0 ▶ method put_extension ( $x, $y, $character ) {
Subroutine put_extension: not called CC=2 SCAR=17.9
Pod documented
91 0 ▶ my $cell = $rows[$y][$x] // die "Term::Fabulous::Render::Target::Grid: extend_cell($x, $y) on a cell that was never set";
Condition0%0/2
$rows[$y][$x] // die("Term::Fabulous::Render::Target::Grid: extend_cell($x, $y) on a cell that was never set")
l!l
00
MC/DC0%0/1
$rows[$y][$x] // die("Term::Fabulous::Render::Target::Grid: extend_cell($x, $y) on a cell that was never set")
$rows[$y][$x]
Truth table0%0/2
A
A= $rows[$y][$x]
Aresult
11
00
92 0 $cell->[0] .= $character;
93 0 return;
94 1 }
95  
96 0 ▶ method put_row ( $x, $y, $columns, $bg ) {
Subroutine put_row: not called CC=3 SCAR=24.8
Pod documented
97 0 ▶ my $row = $rows[$y] //= [];
Condition0%0/2
$rows[$y] //= []
l!l
00
MC/DC0%0/1
$rows[$y] //= []
$rows[$y]
Truth table0%0/2
A
A= $rows[$y]
Aresult
11
00
98 0 $row->[$_] = [ ' ', 0, $bg ] foreach $x .. $x + $columns - 1;
99 0 return;
100 1 }
101  
102 0 ▶ method cell ( $x, $y ) {
Subroutine cell: not called CC=1 SCAR=6.9
Pod documented
103 0 return $rows[$y][$x];
104 1 }
105  
106 0 ▶ method painted_cell ( $x, $y ) {
Subroutine painted_cell: not called CC=2 SCAR=17.9
Pod documented
107 0 ▶ my $cell = $rows[$y][$x] // return ();
Condition0%0/2
$rows[$y][$x] // (return ())
l!l
00
MC/DC0%0/1
$rows[$y][$x] // (return ())
$rows[$y][$x]
Truth table0%0/2
A
A= $rows[$y][$x]
Aresult
11
00
108 0 return @$cell;
109 1 }
110  
111 0 ▶ method grid_height () {
Subroutine grid_height: not called CC=1 SCAR=6.9
Pod documented
112 0 return scalar @rows;
113 1 }
114  
115 0 ▶ method grid_row ($y) {
Subroutine grid_row: not called CC=2 SCAR=17.9
Pod documented
116 0 ▶ return $rows[$y] // [];
Condition0%0/2
$rows[$y] // []
l!l
00
MC/DC0%0/1
$rows[$y] // []
$rows[$y]
Truth table0%0/2
A
A= $rows[$y]
Aresult
11
00
117 1 }
118  
119 0 ▶ method row_text ( $y, %options ) {
Subroutine row_text: not called CC=16 SCAR=56.1
Pod documented
120 0 my @unknown = grep { !/\A(?:columns|colors|trim_trailing_whitespace)\z/ } sort keys %options;
121 0 ▶ die "Term::Fabulous::Render::Target::Grid: row_text does not accept @unknown (known options: columns, colors, trim_trailing_whitespace)" if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 0 0 0
122 0 ▶ my $columns = $options{columns} // die "Term::Fabulous::Render::Target::Grid: row_text needs the number of columns";
Condition0%0/2
$options{"columns"} // die("Term::Fabulous::Render::Target::Grid: row_text needs the number of columns")
l!l
00
MC/DC0%0/1
$options{"columns"} // die("Term::Fabulous::Render::Target::Grid: row_text needs the number of columns")
$options{"columns"}
Truth table0%0/2
A
A= $options{"columns"}
Aresult
11
00
123  
124 0 my $cells = $self->grid_row($y);
125 0 my $last = $columns - 1;
126 0 ▶ if ( $options{trim_trailing_whitespace} // 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"trim_trailing_whitespace"} // 1) 0 0 0
Condition0%0/2
$options{"trim_trailing_whitespace"} // 1
l!l
00
MC/DC0%0/1
$options{"trim_trailing_whitespace"} // 1
$options{"trim_trailing_whitespace"}
Truth table0%0/2
A
A= $options{"trim_trailing_whitespace"}
Aresult
11
00
127 0 ▶ $last = $#$cells if $#$cells < $last;
Branch: 1 branch
BranchTrueFalseTotal
if $cells->$#* < $last 0 0 0
128 0 ▶ $last-- while $last >= 0 && _is_blank( $cells->[$last] );
Branch: 1 branch
BranchTrueFalseTotal
$last >= 0 and _is_blank($cells->[$last]) and do { --$last } 0 0 0
Condition0%0/3
$last >= 0 and _is_blank($cells->[$last])
!ll&&!rl&&r
000
MC/DC0%0/2
$last >= 0 and _is_blank($cells->[$last])
$last >= 0 _is_blank($cells->[$last])
Truth table0%0/3
A and B
A= $last >= 0B= _is_blank($cells->[$last])
ABresult
0X0
100
111
129 }
130  
131 0 ▶ my $colors = $options{colors} // 0;
Condition0%0/2
$options{"colors"} // 0
l!l
00
MC/DC0%0/1
$options{"colors"} // 0
$options{"colors"}
Truth table0%0/2
A
A= $options{"colors"}
Aresult
11
00
132 0 my ( $line, $style ) = ( '', '' );
133 0 my $x = 0;
134 0 ▶ while ( $x <= $last ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x <= $last) 0 0 0
135 0 my $cell = $cells->[$x];
136 0 ▶ my ( $glyph, $fg, $bg ) = defined $cell ? @$cell : ( ' ', TB_DEFAULT, TB_DEFAULT );
Branch: 1 branch
BranchTrueFalseTotal
defined $cell ? : 0 0 0
137 0 ▶ if ($colors) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colors) 0 0 0
138 0 my $wanted = _sgr( $fg, $bg );
139 0 ▶ if ( $wanted ne $style ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wanted ne $style) 0 0 0
140 0 ▶ $line .= RESET if length $style;
Branch: 1 branch
BranchTrueFalseTotal
if length $style 0 0 0
141 0 $line .= $wanted;
142 0 $style = $wanted;
143 }
144 }
145 0 $line .= $glyph;
146 0 ▶ $x += defined $cell ? string_columns($glyph) : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $cell ? : 0 0 0
147 }
148 0 ▶ $line .= RESET if length $style;
Branch: 1 branch
BranchTrueFalseTotal
if length $style 0 0 0
149 0 return $line;
150 1 }
151  
152 # A cell that would print as a plain space: never painted, or a space in
153 # the terminal's default background.
154 0 ▶ sub _is_blank ($cell) {
Subroutine _is_blank: not called CC=4 SCAR=30.0
155 0 ▶ return 1 unless defined $cell;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cell 0 0 0
156 0 my ( $glyph, $fg, $bg ) = @$cell;
157 0 ▶ return $glyph eq ' ' && $bg == TB_DEFAULT && !( $fg & TB_REVERSE );
Condition0%0/6
$glyph eq ' ' && $bg == 0 && !($fg & 67108864)
!ll&&!rl&&r
000
$glyph eq " " and $bg == 0
!ll&&!rl&&r
000
MC/DC0%0/3
$glyph eq ' ' && $bg == 0 && !($fg & 67108864)
$glyph eq " " $bg == 0 !($fg & 67108864)
Truth table0%0/4
A and B && not(C)
A= $glyph eq " "B= $bg == 0C= !($fg & 67108864)
ABCresult
0XX0
10X0
1100
1111
158 }
159  
160 # The style flags count on the foreground, reverse video on either
161 # color, as termbox2 has it.
162 0 ▶ sub _sgr ( $fg, $bg ) {
Subroutine _sgr: not called CC=2 SCAR=17.9
163 0 my $flags = $fg | ( $bg & TB_REVERSE );
164 0 my @codes = ( ( map { $_->[1] } grep { $flags & $_->[0] } STYLE_FLAGS ), _color_codes( 38, $fg ), _color_codes( 48, $bg ) );
165 0 ▶ return @codes ? "\e[" . join( ';', @codes ) . 'm' : '';
Branch: 1 branch
BranchTrueFalseTotal
@codes ? : 0 0 0
166 }
167  
168 # The terminal default needs no code; opaque black carries its own flag
169 # because termbox2 reads 0x000000 as the default color.
170 0 ▶ sub _color_codes ( $base, $attr ) {
Subroutine _color_codes: not called CC=5 SCAR=34.0
171 0 ▶ return () if $attr == TB_DEFAULT;
Branch: 1 branch
BranchTrueFalseTotal
if $attr == 0 0 0 0
172 0 ▶ my $rgb = $attr & TB_HI_BLACK ? 0 : $attr & COLOR_MASK;
Branch: 1 branch
BranchTrueFalseTotal
$attr & 536870912 ? : 0 0 0
173 0 ▶ return () if !( $attr & TB_HI_BLACK ) && $rgb == 0;
Branch: 1 branch
BranchTrueFalseTotal
if not $attr & 536870912 and $rgb == 0 0 0 0
Condition0%0/3
not $attr & 536870912 and $rgb == 0
!ll&&!rl&&r
000
MC/DC0%0/2
not $attr & 536870912 and $rgb == 0
not $attr & 536870912 $rgb == 0
Truth table0%0/3
not(A) and B
A= not $attr & 536870912B= $rgb == 0
ABresult
0X0
100
111
174 0 return ( $base, 2, ( $rgb >> 16 ) & 0xFF, ( $rgb >> 8 ) & 0xFF, $rgb & 0xFF );
175 }
176 }
177  
178 1 1;
179  
180 __END__
« blib/lib/Term/Fabulous/Render/Border.pm ↑ Summary tools/lib/Term/Fabulous/Screenshot/Screen.pm »