« blib/lib/Term/Fabulous/Widget/Checkbox.pm
↑ Summary
blib/lib/Term/Fabulous/Widget/ColorPicker.pm »
| 1 | package Term::Fabulous::Widget::CodeBlock; | ||
| 2 | |||
| 3 | use v5.32; | ||
| 4 | use warnings; | ||
| 5 | use feature 'signatures'; | ||
| 6 | no warnings 'experimental::signatures'; | ||
| 7 | |||
| 8 | use Object::Pad 0.825; | ||
| 9 | |||
| 10 | use Term::Fabulous::Role::MarkedText; | ||
| 11 | use Term::Fabulous::Widget::Display; | ||
| 12 | |||
| 13 | our $VERSION = '0.05'; | ||
| 14 | |||
| 15 | class Term::Fabulous::Widget::CodeBlock | ||
| 16 | :isa(Term::Fabulous::Widget::Display) | ||
| 17 | :does(Term::Fabulous::Role::MarkedText) | ||
| 18 | :strict(params) | ||
| 19 | { | ||
| 20 | use Clay::XS qw(sizing_grow); | ||
| 21 | use List::Util qw(max min); | ||
| 22 | use Scalar::Util qw(refaddr); | ||
| 23 | use Term::Fabulous::Check qw(describe non_negative_integer string); | ||
| 24 | use Term::Fabulous::Document::Look; | ||
| 25 | use Term::Fabulous::Termbox qw(TB_DEFAULT); | ||
| 26 | use Term::Fabulous::Text::Marks; | ||
| 27 | use Term::Fabulous::Text::Style qw(apply_style); | ||
| 28 | use Term::Fabulous::Unicode qw(cluster_columns grapheme_clusters); | ||
| 29 | |||
| 30 | # One column on either side of the code, in the code's background, | ||
| 31 | # where the marks of clipped lines go. | ||
| 32 | use constant MARGIN => 1; | ||
| 33 | use constant CLIPPED_LEFT => "\x{2039}"; | ||
| 34 | use constant CLIPPED_RIGHT => "\x{203A}"; | ||
| 35 | |||
| 36 | field $code :param = ''; | ||
| 37 | field $language :param = undef; | ||
| 38 | field $highlighter :param = undef; | ||
| 39 | field $offset :param = 0; | ||
| 40 | field $look_class :param = 'Term::Fabulous::Document::Look'; | ||
| 41 | |||
| 42 | # The program's spans, what a search marks over them, and a count of | ||
| 43 | # the changes of the code (Term::Fabulous::Text::Marks). | ||
| 44 | field $marks; | ||
| 45 | |||
| 46 | # Per line, its clusters as [ cluster, columns, offset in the code ], | ||
| 47 | # and its width in columns. The highlighter's spans until the code, | ||
| 48 | # the language or the highlighter change; the runs to paint with (see | ||
| 49 | # _runs) until anything they are made of changes, the look among it. | ||
| 50 | field @lines; | ||
| 51 | field $highlighted; | ||
| 52 | field $runs; | ||
| 53 | field $runs_look; | ||
| 54 | |||
| 55 | ADJUST :params ( :$spans //= [] ) { | ||
| 56 | $code = string( $self, code => $code ); | ||
| 57 | $language = _checked_language( $self, $language ); | ||
| 58 | $highlighter = _checked_highlighter( $self, $highlighter ); | ||
| 59 | $marks = Term::Fabulous::Text::Marks->new( owner => ref($self), text => $code, spans => $spans ); | ||
| 60 | $offset = non_negative_integer( $self, offset => $offset ); | ||
| 61 | $look_class = _checked_look_class( $self, $look_class ); | ||
| 62 | $self->_split_lines; | ||
| 63 | } | ||
| 64 | |||
| 65 | # The name of a loaded look class: the base look, or a subclass with | ||
| 66 | # styles of its own, as a document view hands to its code blocks. | ||
| 67 | sub _checked_look_class ( $owner, $value ) { | ||
| 68 | die ref($owner) . ": look_class must be the name of a Term::Fabulous::Document::Look class, got " . describe($value) | ||
| 69 | unless defined $value && !ref $value && length $value && $value->isa('Term::Fabulous::Document::Look'); | ||
| 70 | return $value; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub _checked_language ( $owner, $value ) { | ||
| 74 | return undef unless defined $value; | ||
| 75 | $value = string( $owner, language => $value ); | ||
| 76 | die ref($owner) . ": language must not be empty" unless length $value; | ||
| 77 | return $value; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub _checked_highlighter ( $owner, $value ) { | ||
| 81 | die ref($owner) . ": highlighter must be a code reference or undef, got " . describe($value) if defined $value && ref $value ne 'CODE'; | ||
| 82 | return $value; | ||
| 83 | } | ||
| 84 | |||
| 85 | method _split_lines () { | ||
| 86 | my $position = 0; | ||
| 87 | @lines = map { | ||
| 88 | my $line = $_; | ||
| 89 | my @clusters; | ||
| 90 | my $columns = 0; | ||
| 91 | foreach my $cluster ( grapheme_clusters($line) ) { | ||
| 92 | my $width = cluster_columns($cluster); | ||
| 93 | push @clusters, [ $cluster, $width, $position ]; | ||
| 94 | $position += length $cluster; | ||
| 95 | $columns += $width; | ||
| 96 | } | ||
| 97 | $position++; # the line break | ||
| 98 | +{ clusters => \@clusters, columns => $columns }; | ||
| 99 | } split /\n/, $code, -1; | ||
| 100 | @lines = ( { clusters => [], columns => 0 } ) unless @lines; | ||
| 101 | $self->_forget_highlighting; | ||
| 102 | return; | ||
| 103 | } | ||
| 104 | |||
| 105 | method _forget_highlighting () { | ||
| 106 | $highlighted = undef; | ||
| 107 | $runs = undef; | ||
| 108 | return; | ||
| 109 | } | ||
| 110 | |||
| 111 | # --------------------------------------------------------------------- | ||
| 112 | # Accessors | ||
| 113 | # --------------------------------------------------------------------- | ||
| 114 | |||
| 115 | # New code drops the spans and the highlights: they pointed into the | ||
| 116 | # old one. | ||
| 117 | method code (@new) { | ||
| 118 | return $code unless @new; | ||
| 119 | $code = string( $self, code => $new[0] ); | ||
| 120 | $marks->set_text($code); | ||
| 121 | $self->_split_lines; | ||
| 122 | $self->mark_changed; | ||
| 123 | return $code; | ||
| 124 | } | ||
| 125 | |||
| 126 | method language (@new) { | ||
| 127 | return $language unless @new; | ||
| 128 | $language = _checked_language( $self, $new[0] ); | ||
| 129 | $self->_forget_highlighting; | ||
| 130 | $self->mark_changed; | ||
| 131 | return $language; | ||
| 132 | } | ||
| 133 | |||
| 134 | method highlighter (@new) { | ||
| 135 | return $highlighter unless @new; | ||
| 136 | $highlighter = _checked_highlighter( $self, $new[0] ); | ||
| 137 | $self->_forget_highlighting; | ||
| 138 | $self->mark_changed; | ||
| 139 | return $highlighter; | ||
| 140 | } | ||
| 141 | |||
| 142 | # Term::Fabulous::Role::MarkedText: the marks, and what follows from a | ||
| 143 | # change of them. | ||
| 144 | method text_marks () { | ||
| 145 | return $marks; | ||
| 146 | } | ||
| 147 | |||
| 148 | method marks_changed ($is_content) { | ||
| 149 | $runs = undef; | ||
| 150 | $self->mark_changed; | ||
| 151 | return; | ||
| 152 | } | ||
| 153 | |||
| 154 | # --------------------------------------------------------------------- | ||
| 155 | # Searching (Term::Fabulous::Role::Searchable, through | ||
| 156 | # Term::Fabulous::Role::MarkedText): the code | ||
| 157 | # --------------------------------------------------------------------- | ||
| 158 | |||
| 159 | # The code never wraps, so the row of a character is its line. | ||
| 160 | method reveal_text ( $key, $offset ) { | ||
| 161 | $offset = non_negative_integer( $self, offset => $offset ); | ||
| 162 | my $line = () = substr( $code, 0, $offset ) =~ /\n/g; | ||
| 163 | return [ $self, $line ]; | ||
| 164 | } | ||
| 165 | |||
| 166 | method look_class (@new) { | ||
| 167 | return $look_class unless @new; | ||
| 168 | $look_class = _checked_look_class( $self, $new[0] ); | ||
| 169 | $self->mark_changed; | ||
| 170 | return $look_class; | ||
| 171 | } | ||
| 172 | |||
| 173 | # A new offset paints again only when it changes what shows. | ||
| 174 | method offset (@new) { | ||
| 175 | return $offset unless @new; | ||
| 176 | my $shown_before = $self->shown_offset; | ||
| 177 | $offset = non_negative_integer( $self, offset => $new[0] ); | ||
| 178 | $self->mark_changed if $self->shown_offset != $shown_before; | ||
| 179 | return $offset; | ||
| 180 | } | ||
| 181 | |||
| 182 | method line_count () { | ||
| 183 | return scalar @lines; | ||
| 184 | } | ||
| 185 | |||
| 186 | method widest_line_columns () { | ||
| 187 | return max map { $_->{columns} } @lines; | ||
| 188 | } | ||
| 189 | |||
| 190 | # --------------------------------------------------------------------- | ||
| 191 | # Scrolling sideways | ||
| 192 | # --------------------------------------------------------------------- | ||
| 193 | |||
| 194 | method _visible_columns () { | ||
| 195 | return max( 0, $self->columns - 2 * MARGIN ); | ||
| 196 | } | ||
| 197 | |||
| 198 | method max_offset () { | ||
| 199 | return max( 0, $self->widest_line_columns - $self->_visible_columns ); | ||
| 200 | } | ||
| 201 | |||
| 202 | method shown_offset () { | ||
| 203 | return min( $offset, $self->max_offset ); | ||
| 204 | } | ||
| 205 | |||
| 206 | # --------------------------------------------------------------------- | ||
| 207 | # Painting | ||
| 208 | # --------------------------------------------------------------------- | ||
| 209 | |||
| 210 | method natural_size () { | ||
| 211 | return ( sizing_grow(), scalar @lines ); | ||
| 212 | } | ||
| 213 | |||
| 214 | method _highlighted_spans () { | ||
| 215 | return $highlighted //= $highlighter ? [ $marks->check_spans( 'highlighter span' => $highlighter->( $code, $language ) ) ] : []; | ||
| 216 | } | ||
| 217 | |||
| 218 | # The runs of the code in the current look, made again only when the | ||
| 219 | # look (a theme switch) or the marks changed: the highlighter's spans, | ||
| 220 | # then the program's, then the highlights, stacked over the base look | ||
| 221 | # with their palette tokens in the theme's colors | ||
| 222 | # (Term::Fabulous::Text::Marks), as { to, look, fg, bg }, and a last | ||
| 223 | # run past the end. | ||
| 224 | method _runs_in ($look) { | ||
| 225 | return $runs if defined $runs && refaddr($runs_look) == refaddr($look); | ||
| 226 | my $style = $look->style('code_block'); | ||
| 227 | my $base = { attrs => 0, color => $style->{color}, background => $style->{background} }; | ||
| 228 | my @runs = map { +{ to => $_->[1], look => apply_style( $base, $_->[2] ) } } @{ $marks->runs( theme => $self->drawing_theme, under => $self->_highlighted_spans ) }; | ||
| 229 | $runs = [ map { +{ %$_, fg => $self->_fg_attr( $_->{look} ), bg => $self->color_attr( $_->{look}{background} ) } } @runs, { to => length($code) + 1, look => $base } ]; | ||
| 230 | $runs_look = $look; | ||
| 231 | return $runs; | ||
| 232 | } | ||
| 233 | |||
| 234 | method paint () { | ||
| 235 | my $look = $look_class->of($self); | ||
| 236 | my $background = $self->color_attr( $look->style('code_block')->{background} ); | ||
| 237 | $self->fill_attrs( 0, $_, $self->columns, ' ', undef, $background ) foreach 0 .. $self->rows - 1; | ||
| 238 | |||
| 239 | my $visible = $self->_visible_columns; | ||
| 240 | return unless $visible; | ||
| 241 | my $shift = $self->shown_offset; | ||
| 242 | my $runs = $self->_runs_in($look); | ||
| 243 | my $marker = $look->style('clipped'); | ||
| 244 | my ( $marker_fg, $marker_bg ) = ( $self->color_attr( $marker->{color} ), $self->color_attr( $marker->{background} ) ); | ||
| 245 | |||
| 246 | my $run_index = 0; | ||
| 247 | foreach my $y ( 0 .. min( $self->rows, scalar @lines ) - 1 ) { | ||
| 248 | my $line = $lines[$y]; | ||
| 249 | my $column = 0; | ||
| 250 | foreach my $cluster_info ( @{ $line->{clusters} } ) { | ||
| 251 | my ( $cluster, $width, $position ) = @$cluster_info; | ||
| 252 | my $from = $column; | ||
| 253 | $column += $width; | ||
| 254 | next if $from < $shift; | ||
| 255 | last if $column > $shift + $visible; | ||
| 256 | $run_index++ while $runs->[$run_index]{to} <= $position; | ||
| 257 | $self->put_attrs( MARGIN + $from - $shift, $y, $cluster, @{ $runs->[$run_index] }{qw(fg bg)} ); | ||
| 258 | } | ||
| 259 | $self->put_attrs( 0, $y, CLIPPED_LEFT, $marker_fg, $marker_bg ) if $shift > 0 && $line->{columns} > 0; | ||
| 260 | $self->put_attrs( $self->columns - 1, $y, CLIPPED_RIGHT, $marker_fg, $marker_bg ) if $line->{columns} > $shift + $visible; | ||
| 261 | } | ||
| 262 | return; | ||
| 263 | } | ||
| 264 | |||
| 265 | method _fg_attr ($look) { | ||
| 266 | return ( $self->color_attr( $look->{color} ) // TB_DEFAULT ) | $look->{attrs}; | ||
| 267 | } | ||
| 268 | |||
| 269 | # --------------------------------------------------------------------- | ||
| 270 | # KDL | ||
| 271 | # --------------------------------------------------------------------- | ||
| 272 | |||
| 273 | method layout_properties :common () { | ||
| 274 | return ( $class->SUPER::layout_properties, code => 'scalar', language => 'scalar', offset => 'scalar', look_class => 'scalar' ); | ||
| 275 | } | ||
| 276 | } | ||
| 277 | |||
| 278 | 1; | ||
| 279 | |||
| 280 | __END__ |