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

filter: Statementstmt 0.0% 0 / 119 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 46 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 0.0% 0 / 12 Podpod n/a totaltotal 0.0% 0 / 232 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Term::Fabulous::Render::Target::Grid;
2  
3 use v5.32;
4 use warnings;
5 use feature 'signatures';
6 no warnings 'experimental::signatures';
7  
8 our $VERSION = '0.03';
9  
10 use Object::Pad 0.825;
11  
12 use Term::Fabulous::Render::Target::Mask;
13 use Term::Fabulous::Render::Target::Sixel;
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 use Term::Fabulous::Render::Attr qw(STYLE_FLAGS);
21 use Term::Fabulous::Termbox qw(TB_DEFAULT TB_HI_BLACK TB_REVERSE);
22 use Term::Fabulous::Unicode qw(string_columns);
23  
24 use constant RESET => "\e[0m";
25  
26 # Term::Fabulous::Render::Attr packs the color into the low 24 bits and
27 # the flags above them.
28 use constant COLOR_MASK => 0xFFFFFF;
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 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 field @_cell_size;
39 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 method sixel_cell_size () {
48 return @_cell_size;
49 }
50  
51 method sixel_area ( $width, $height ) {
52 return [ 0, 0, $width, $height ];
53 }
54  
55 method show_sixels (@placements) {
56 @_sixels = map {
57 { %$_ }
58 } @placements;
59 return;
60 }
61  
62 method sixels () {
63 return map {
64 { %$_ }
65 } @_sixels;
66 }
67  
68 method clear_cells (@kept_rects) {
69 my @kept_rows;
70 foreach my $rect (@kept_rects) {
71 my ( $x0, $y0, $x1, $y1 ) = @$rect;
72 foreach my $y ( $y0 .. $y1 - 1 ) {
73 my $row = $rows[$y] // next;
74 ( $kept_rows[$y] //= [] )->[$_] = $row->[$_] foreach $x0 .. $x1 - 1;
75 }
76 }
77 @rows = @kept_rows;
78 return;
79 }
80  
81 method present_cells () {
82 return;
83 }
84  
85 method put_cell ( $x, $y, $glyph, $fg, $bg ) {
86 ( $rows[$y] //= [] )->[$x] = [ $glyph, $fg, $bg ];
87 return;
88 }
89  
90 method put_extension ( $x, $y, $character ) {
91 my $cell = $rows[$y][$x] // die "Term::Fabulous::Render::Target::Grid: extend_cell($x, $y) on a cell that was never set";
92 $cell->[0] .= $character;
93 return;
94 }
95  
96 method put_row ( $x, $y, $columns, $bg ) {
97 my $row = $rows[$y] //= [];
98 $row->[$_] = [ ' ', 0, $bg ] foreach $x .. $x + $columns - 1;
99 return;
100 }
101  
102 method cell ( $x, $y ) {
103 return $rows[$y][$x];
104 }
105  
106 method painted_cell ( $x, $y ) {
107 my $cell = $rows[$y][$x] // return ();
108 return @$cell;
109 }
110  
111 method grid_height () {
112 return scalar @rows;
113 }
114  
115 method grid_row ($y) {
116 return $rows[$y] // [];
117 }
118  
119 method row_text ( $y, %options ) {
120 my @unknown = grep { !/\A(?:columns|colors|trim_trailing_whitespace)\z/ } sort keys %options;
121 die "Term::Fabulous::Render::Target::Grid: row_text does not accept @unknown (known options: columns, colors, trim_trailing_whitespace)" if @unknown;
122 my $columns = $options{columns} // die "Term::Fabulous::Render::Target::Grid: row_text needs the number of columns";
123  
124 my $cells = $self->grid_row($y);
125 my $last = $columns - 1;
126 if ( $options{trim_trailing_whitespace} // 1 ) {
127 $last = $#$cells if $#$cells < $last;
128 $last-- while $last >= 0 && _is_blank( $cells->[$last] );
129 }
130  
131 my $colors = $options{colors} // 0;
132 my ( $line, $style ) = ( '', '' );
133 my $x = 0;
134 while ( $x <= $last ) {
135 my $cell = $cells->[$x];
136 my ( $glyph, $fg, $bg ) = defined $cell ? @$cell : ( ' ', TB_DEFAULT, TB_DEFAULT );
137 if ($colors) {
138 my $wanted = _sgr( $fg, $bg );
139 if ( $wanted ne $style ) {
140 $line .= RESET if length $style;
141 $line .= $wanted;
142 $style = $wanted;
143 }
144 }
145 $line .= $glyph;
146 $x += defined $cell ? string_columns($glyph) : 1;
147 }
148 $line .= RESET if length $style;
149 return $line;
150 }
151  
152 # A cell that would print as a plain space: never painted, or a space in
153 # the terminal's default background.
154 sub _is_blank ($cell) {
155 return 1 unless defined $cell;
156 my ( $glyph, $fg, $bg ) = @$cell;
157 return $glyph eq ' ' && $bg == TB_DEFAULT && !( $fg & TB_REVERSE );
158 }
159  
160 # The style flags count on the foreground, reverse video on either
161 # color, as termbox2 has it.
162 sub _sgr ( $fg, $bg ) {
163 my $flags = $fg | ( $bg & TB_REVERSE );
164 my @codes = ( ( map { $_->[1] } grep { $flags & $_->[0] } STYLE_FLAGS ), _color_codes( 38, $fg ), _color_codes( 48, $bg ) );
165 return @codes ? "\e[" . join( ';', @codes ) . 'm' : '';
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 sub _color_codes ( $base, $attr ) {
171 return () if $attr == TB_DEFAULT;
172 my $rgb = $attr & TB_HI_BLACK ? 0 : $attr & COLOR_MASK;
173 return () if !( $attr & TB_HI_BLACK ) && $rgb == 0;
174 return ( $base, 2, ( $rgb >> 16 ) & 0xFF, ( $rgb >> 8 ) & 0xFF, $rgb & 0xFF );
175 }
176 }
177  
178 1;
179  
180 __END__