blib/lib/Term/Fabulous/TextView.pm untested

filter: Statementstmt 0.0% 0 / 234 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 85 MC/DCmcdc 0.0% 0 / 50 Subroutinesub 0.0% 0 / 9 Podpod n/a totaltotal 0.0% 0 / 452 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::TextView;
2  
3 use v5.32;
4 use warnings;
5 use feature 'signatures';
6 no warnings 'experimental::signatures';
7  
8 our $VERSION = '0.05';
9  
10 use Object::Pad 0.825;
11  
12 class Term::Fabulous::TextView :strict(params) {
13 use List::Util qw(max min sum0);
14 use Scalar::Util qw(blessed);
15 use Term::Fabulous::Unicode qw(sanitize_text cluster_columns);
16 use Term::Fabulous::Viewport qw(clamp_offset max_offset reveal_range);
17  
18 field $editor :param :reader;
19 field $display :param = \&sanitize_text;
20 field $wrap :param :reader = 0;
21 field $scrollbar :param :reader = 0;
22  
23 field $columns :reader = 0;
24 field $rows :reader = 0;
25  
26 # The first visual row shown, and (without wrapping) the columns
27 # scrolled out on the left: always where a cluster of the cursor's
28 # line starts.
29 field $top :reader(top_row) = 0;
30 field $left :reader(left_column) = 0;
31  
32 # Grows when the display function changes, which changes the widths.
33 field $display_generation = 0;
34  
35 # The visual rows for the current text and size, see _layout, and
36 # "width:wrap:generation" => { line text => [ its parts ] } for the
37 # lines the last layout showed: an edit wraps only the lines it changed.
38 field $layout_cache;
39 field %parts_by_key;
40  
41 # What the view last followed the cursor for.
42 field $followed = '';
43  
44 # [ $column, $row, $offset ]: the column vertical moves aim for, kept
45 # while the cursor stays where the last vertical move put it.
46 field @goal;
47  
48 ADJUST {
49 die "Term::Fabulous::TextView: editor must be a Term::Fabulous::Editor, got " . ( ref $editor || ( defined $editor ? "'$editor'" : 'undef' ) )
50 unless blessed $editor && $editor->isa('Term::Fabulous::Editor');
51 _check_display($display);
52 ( $wrap, $scrollbar ) = ( _flag( wrap => $wrap ), _flag( scrollbar => $scrollbar ) );
53 }
54  
55 sub _check_display ($code) {
56 die "Term::Fabulous::TextView: display must be a code reference, got " . ( ref $code || ( defined $code ? "'$code'" : 'undef' ) )
57 unless ref $code eq 'CODE';
58 return;
59 }
60  
61 sub _flag ( $name, $value ) {
62 die "Term::Fabulous::TextView: $name must be a plain boolean value, got a " . ref($value) . " reference" if ref $value;
63 return $value ? 1 : 0;
64 }
65  
66 # ---------------------------------------------------------------------
67 # Settings
68 # ---------------------------------------------------------------------
69  
70 method set_size ( $new_columns, $new_rows ) {
71 die "Term::Fabulous::TextView: set_size needs a non-negative integer size, got " . join( ' x ', map { defined ? "'$_'" : 'undef' } $new_columns, $new_rows )
72 unless 2 == grep { defined && !ref && /\A[0-9]+\z/ } $new_columns, $new_rows;
73 ( $columns, $rows ) = ( $new_columns + 0, $new_rows + 0 );
74 return $self;
75 }
76  
77 method set_wrap ($value) {
78 $wrap = _flag( wrap => $value );
79 $left = 0;
80 return $self;
81 }
82  
83 method set_scrollbar ($value) {
84 $scrollbar = _flag( scrollbar => $value );
85 return $self;
86 }
87  
88 method set_display ($code) {
89 _check_display($code);
90 $display = $code;
91 return $self->display_changed;
92 }
93  
94 # The display function shows clusters differently now.
95 method display_changed () {
96 $display_generation++;
97 return $self;
98 }
99  
100 # Back to the top-left, as for a new text.
101 method home () {
102 ( $top, $left ) = ( 0, 0 );
103 return $self;
104 }
105  
106 # ---------------------------------------------------------------------
107 # Clusters
108 # ---------------------------------------------------------------------
109  
110 method clusters ( $line, $from, $to ) {
111 my $text = $editor->line($line);
112 my @boundaries = grep { $_ >= $from && $_ <= $to } $editor->boundaries($line);
113 return map {
114 my $shown = $display->( substr( $text, $boundaries[$_], $boundaries[ $_ + 1 ] - $boundaries[$_] ) );
115 [ $boundaries[$_], $shown, cluster_columns($shown) ]
116 } 0 .. $#boundaries - 1;
117 }
118  
119 method _columns_to ( $line, $from, $offset ) {
120 return sum0 map { $_->[2] } $self->clusters( $line, $from, $offset );
121 }
122  
123 # The boundary of the part [from, to) of a line shown at a column: the
124 # start of the cluster covering it, or $to past the end.
125 method _offset_at_column ( $line, $from, $to, $column ) {
126 my $x = 0;
127 foreach my $cluster ( $self->clusters( $line, $from, $to ) ) {
128 return $cluster->[0] if $column < $x + $cluster->[2];
129 $x += $cluster->[2];
130 }
131 return $to;
132 }
133  
134 # ---------------------------------------------------------------------
135 # Visual rows: every line is cut into the parts shown on one row each.
136 # A row is [ $line, $from, $to, $is_last_part, $shown_from ]: the part
137 # [from, to) of the line, shown from $shown_from on.
138 # ---------------------------------------------------------------------
139  
140 # The parts [from, to, shown_from] of a line for a text width.
141 # Wrapping breaks after the last blank that fits, or else before the
142 # cluster that does not fit. A blank that does not fit any more hangs
143 # at the break: it starts the next part, unseen (shown_from is after
144 # it), so no row starts with the blank that ended the row above. A
145 # line whose last part fills the width gets an empty part after it,
146 # where the cursor can stand at its end.
147 method _wrap_line ( $line, $width ) {
148 my $end = length $editor->line($line);
149 return [ 0, $end, 0 ] unless $wrap;
150  
151 my @clusters = $self->clusters( $line, 0, $end );
152 my $offset_of = sub ($index) { $index < @clusters ? $clusters[$index][0] : $end };
153 my $part = sub ( $start, $shown, $stop ) {
154 [ map { $offset_of->($_) } $start, $stop, $shown ]
155 };
156  
157 my ( @parts, $break );
158 my ( $start, $shown, $used ) = ( 0, 0, 0 );
159 foreach my $index ( 0 .. $#clusters ) {
160 my ( undef, $display_cluster, $cluster_columns ) = @{ $clusters[$index] };
161 my $is_blank = $display_cluster =~ /\A\s\z/;
162 if ( $used + $cluster_columns > $width && $index > $shown ) {
163 if ($is_blank) {
164 push @parts, $part->( $start, $shown, $index );
165 ( $start, $shown, $used ) = ( $index, $index + 1, 0 );
166 undef $break;
167 next;
168 }
169 my $cut = defined $break && $break > $shown ? $break : $index;
170 push @parts, $part->( $start, $shown, $cut );
171 ( $start, $shown ) = ( $cut, $cut );
172 $used = sum0 map { $_->[2] } @clusters[ $cut .. $index - 1 ];
173 undef $break;
174  
175 # The word carried over to the new row may leave no room for
176 # this cluster either.
177 if ( $used + $cluster_columns > $width && $index > $shown ) {
178 push @parts, $part->( $start, $shown, $index );
179 ( $start, $shown, $used ) = ( $index, $index, 0 );
180 }
181 }
182 $used += $cluster_columns;
183 $break = $index + 1 if $is_blank;
184 }
185 push @parts, $part->( $start, $shown, scalar @clusters );
186 push @parts, [ $end, $end, $end ] if @clusters && $used >= $width;
187 return @parts;
188 }
189  
190 # { key, width, scrollbar, rows => [ visual rows ], first => [ the first
191 # visual row of each line ] } for the current text and size. The row
192 # count at the full width decides whether a scrollbar is needed.
193 method _layout () {
194 my $key = join ':', $editor->text_revision, $columns, $rows, $wrap, $scrollbar, $display_generation;
195 return $layout_cache if defined $layout_cache && $layout_cache->{key} eq $key;
196  
197 my @lines = $editor->lines;
198 my %parts_now;
199 my ( $width, $has_scrollbar ) = ( max( $columns, 1 ), 0 );
200 my $parts = $self->_parts_of_lines( \@lines, $width, \%parts_now );
201 if ( $scrollbar && $columns > 1 && sum0( map { scalar @$_ } @$parts ) > $rows ) {
202 ( $width, $has_scrollbar ) = ( $columns - 1, 1 );
203 $parts = $self->_parts_of_lines( \@lines, $width, \%parts_now );
204 }
205 %parts_by_key = %parts_now;
206  
207 my ( @visual_rows, @first );
208 foreach my $line ( 0 .. $#$parts ) {
209 push @first, scalar @visual_rows;
210 push @visual_rows, map { [ $line, @$_ ] } @{ $parts->[$line] };
211 }
212 return $layout_cache = { key => $key, width => $width, scrollbar => $has_scrollbar, rows => \@visual_rows, first => \@first };
213 }
214  
215 # The parts of every line as [ $from, $to, $is_last, $shown_from ].
216 # Wrapping depends on the text of a line, the width, wrap and the
217 # display only, so the parts of a line come from the last layout when
218 # it had the line.
219 method _parts_of_lines ( $lines, $width, $parts_now ) {
220 my $key = "$width:$wrap:$display_generation";
221 my $before = $parts_by_key{$key} // {};
222 my $now = $parts_now->{$key} //= {};
223 return [
224 map {
225 my $text = $lines->[$_];
226 $now->{$text} //= $before->{$text} // do {
227 my @parts = $self->_wrap_line( $_, $width );
228 [ map { [ @{ $parts[$_] }[ 0, 1 ], $_ == $#parts ? 1 : 0, $parts[$_][2] ] } 0 .. $#parts ];
229 };
230 } 0 .. $#$lines
231 ];
232 }
233  
234 method visual_rows () {
235 return map { [@$_] } @{ $self->_layout->{rows} };
236 }
237  
238 method visual_row_count () {
239 return scalar @{ $self->_layout->{rows} };
240 }
241  
242 method visible_rows () {
243 my $all = $self->_layout->{rows};
244 my $first = min( $top, $self->max_top );
245 return map { [ @{ $all->[$_] } ] } $first .. min( $first + $rows, scalar @$all ) - 1;
246 }
247  
248 method text_columns () {
249 return $self->_layout->{width};
250 }
251  
252 method has_scrollbar () {
253 return $self->_layout->{scrollbar};
254 }
255  
256 method max_top () {
257 return max_offset( $self->visual_row_count, $rows );
258 }
259  
260 # The visual row showing an editor position: the end of a wrapped
261 # part shows at the start of the next one.
262 method visual_row_of ( $line, $offset ) {
263 my $layout = $self->_layout;
264 my $index = $layout->{first}[$line];
265 $index++ while !$layout->{rows}[$index][3] && $offset >= $layout->{rows}[$index][2];
266 return $index;
267 }
268  
269 # ---------------------------------------------------------------------
270 # Scrolling and positions
271 # ---------------------------------------------------------------------
272  
273 # Scrolls to the cursor when the editor or the view changed since the
274 # last call; a view scrolled away from the cursor (the mouse wheel)
275 # stays where it is otherwise.
276 method follow_cursor () {
277 my $key = join ':', $editor->revision, $columns, $rows, $wrap, $scrollbar, $display_generation;
278 return $self if $key eq $followed;
279 $followed = $key;
280 return $self->scroll_to_cursor;
281 }
282  
283 method scroll_to_cursor () {
284 return $self if $columns < 1 || $rows < 1;
285 my ( $line, $offset ) = $editor->cursor;
286 my $visual = $self->visual_row_of( $line, $offset );
287 $top = reveal_range( $self->visual_row_count, $rows, $top, $visual, $visual + 1 );
288 $self->_scroll_sideways_to( $line, $offset ) unless $wrap;
289 return $self;
290 }
291  
292 # Shows the cursor and the cell after its line when the cursor is at
293 # the end, scrolls no further than needed to fill the view, and starts
294 # the view where a cluster of the cursor's line starts.
295 method _scroll_sideways_to ( $line, $offset ) {
296 my $width = $self->text_columns;
297 my @clusters = $self->clusters( $line, 0, length $editor->line($line) );
298 my ( $cursor_x, $cursor_columns, $end_x ) = ( undef, 1, 0 );
299 foreach my $cluster (@clusters) {
300 ( $cursor_x, $cursor_columns ) = ( $end_x, $cluster->[2] ) if $cluster->[0] == $offset;
301 $end_x += $cluster->[2];
302 }
303 $cursor_x //= $end_x;
304  
305 $left = min( $left, max( 0, $end_x + 1 - $width ) );
306 $left = $cursor_x if $cursor_x < $left;
307 $left = $cursor_x + $cursor_columns - $width if $cursor_x + $cursor_columns > $left + $width;
308 $left = 0 if $left < 0;
309  
310 my $start = 0;
311 foreach my $cluster (@clusters) {
312 last if $start >= $left;
313 $start += $cluster->[2];
314 }
315 $left = $start;
316 return;
317 }
318  
319 # Scrolls by visual rows within the text; returns how many it moved.
320 method scroll_rows ($count) {
321 my $before = $top;
322 $top = clamp_offset( $self->visual_row_count, $rows, $top + $count );
323 return $top - $before;
324 }
325  
326 # The view cell of the cursor, or the empty list when it is out of view.
327 method cursor_cell () {
328 my ( $line, $offset ) = $editor->cursor;
329 my $visual = $self->visual_row_of( $line, $offset );
330 my $y = $visual - $top;
331 return () if $y < 0 || $y >= $rows;
332 my ( undef, undef, undef, undef, $shown ) = @{ $self->_layout->{rows}[$visual] };
333 my $x = $self->_columns_to( $line, $shown, max( $offset, $shown ) ) - ( $wrap ? 0 : $left );
334 return () if $x < 0 || $x >= $self->text_columns;
335 return ( $x, $y );
336 }
337  
338 # The editor position shown at a view cell. A click past the end of a
339 # wrapped part stays on its row, before its last cluster, since the
340 # end itself is shown at the start of the next row.
341 method position_at ( $column, $row ) {
342 my $all = $self->_layout->{rows};
343 my $index = min( $top + $row, $#$all );
344 my ( $line, undef, $to, $is_last, $shown ) = @{ $all->[$index] };
345 my $offset = $self->_offset_at_column( $line, $shown, $to, $column + ( $wrap ? 0 : $left ) );
346 $offset = $self->_last_cluster_of( $line, $shown, $to ) if !$is_last && $offset == $to && $to > $shown;
347 return ( $line, $offset );
348 }
349  
350 method _last_cluster_of ( $line, $from, $to ) {
351 my @clusters = $self->clusters( $line, $from, $to );
352 return $clusters[-1][0];
353 }
354  
355 # Moves the cursor by visual rows, aiming for the column it had when
356 # vertical movement started. Beyond the first (last) row it goes to the
357 # start (end) of the text, which starts a new aim.
358 method move_vertically ( $count, $extend = 0 ) {
359 my $all = $self->_layout->{rows};
360 my ( $line, $offset ) = $editor->cursor;
361 my $visual = $self->visual_row_of( $line, $offset );
362 my $goal_column = @goal && $goal[1] == $line && $goal[2] == $offset ? $goal[0] : $self->_columns_to( $line, $all->[$visual][4], max( $offset, $all->[$visual][4] ) );
363  
364 my $target = $visual + $count;
365 if ( $target < 0 || $target > $#$all ) {
366 $target < 0 ? $editor->move_document_start($extend) : $editor->move_document_end($extend);
367 @goal = ();
368 return $self;
369 }
370  
371 my ( $target_line, undef, $to, $is_last, $shown ) = @{ $all->[$target] };
372 my $target_offset = $self->_offset_at_column( $target_line, $shown, $to, $goal_column );
373 $target_offset = $self->_last_cluster_of( $target_line, $shown, $to ) if !$is_last && $target_offset == $to && $to > $shown;
374 $editor->move_to( $target_line, $target_offset, $extend );
375 @goal = ( $goal_column, $editor->cursor );
376 return $self;
377 }
378 }
379  
380 1;
381  
382 __END__