| 1 | package Term::Fabulous::Text::Marks; | ||
| 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 | our $VERSION = '0.05'; | ||
| 9 | |||
| 10 | 1 | ▶ | use Object::Pad 0.825; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | class Term::Fabulous::Text::Marks :strict(params) { | ||
| 13 | 1 | ▶ | use List::Util qw(first uniqnum); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 0 | ▶ | use Term::Fabulous::Check qw(describe non_negative_integer string); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 15 | use Term::Fabulous::Text::Style qw(compose_styles resolve_style style); | ||
| 16 | |||
| 17 | # The name the error messages start with: the widget class the marks | ||
| 18 | # belong to. | ||
| 19 | field $owner :param; | ||
| 20 | field $text :param = ''; | ||
| 21 | |||
| 22 | field @spans; # [ start, end, style hash ], in the order given | ||
| 23 | field @highlights; # the same, painted over the spans | ||
| 24 | field @links; # [ start, end, target ], by start, none overlapping | ||
| 25 | |||
| 26 | # Counts the changes of the text, the spans and the links; highlights | ||
| 27 | # are not content, so marking what a search found leaves it alone. | ||
| 28 | field $content_revision = 0; | ||
| 29 | |||
| 30 | ADJUST :params ( :$spans = [], :$links = [] ) { | ||
| 31 | $owner = string( 'Term::Fabulous::Text::Marks', owner => $owner ); | ||
| 32 | $text = string( $owner, text => $text ); | ||
| 33 | @spans = $self->check_spans( span => $spans ); | ||
| 34 | die "$owner: links must be an array reference of [start, end, target], got " . describe($links) unless ref $links eq 'ARRAY'; | ||
| 35 | foreach my $link (@$links) { | ||
| 36 | die "$owner: a link is [start, end, target], got " . describe($link) unless ref $link eq 'ARRAY' && @$link == 3; | ||
| 37 | $self->_insert_link( @{$link}[ 2, 0, 1 ] ); | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | method text () { | ||
| 42 | return $text; | ||
| 43 | } | ||
| 44 | |||
| 45 | # A new text drops every mark: they pointed into the old one. | ||
| 46 | method set_text ($new) { | ||
| 47 | $text = string( $owner, text => $new ); | ||
| 48 | ( @spans, @highlights, @links ) = (); | ||
| 49 | $content_revision++; | ||
| 50 | return $self; | ||
| 51 | } | ||
| 52 | |||
| 53 | method content_revision () { | ||
| 54 | return $content_revision; | ||
| 55 | } | ||
| 56 | |||
| 57 | method has_marks () { | ||
| 58 | return @spans || @highlights || @links ? 1 : 0; | ||
| 59 | } | ||
| 60 | |||
| 61 | # --------------------------------------------------------------------- | ||
| 62 | # Spans and highlights | ||
| 63 | # --------------------------------------------------------------------- | ||
| 64 | |||
| 65 | method spans () { | ||
| 66 | return _copies(@spans); | ||
| 67 | } | ||
| 68 | |||
| 69 | method set_spans ($given) { | ||
| 70 | @spans = $self->check_spans( span => $given ); | ||
| 71 | $content_revision++; | ||
| 72 | return $self; | ||
| 73 | } | ||
| 74 | |||
| 75 | method add_span ( $style, $start = 0, $end = undef ) { | ||
| 76 | push @spans, $self->_span( span => $style, $start, $end // length $text ); | ||
| 77 | $content_revision++; | ||
| 78 | return $self; | ||
| 79 | } | ||
| 80 | |||
| 81 | method highlights () { | ||
| 82 | return _copies(@highlights); | ||
| 83 | } | ||
| 84 | |||
| 85 | method set_highlights ($given) { | ||
| 86 | @highlights = $self->check_spans( highlight => $given ); | ||
| 87 | return $self; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub _copies (@ranges) { | ||
| 91 | return [ map { [ $_->[0], $_->[1], { %{ $_->[2] } } ] } @ranges ]; | ||
| 92 | } | ||
| 93 | |||
| 94 | # Checked copies of ranges [ start, end, style ] of the text, each | ||
| 95 | # style normalized to a style hash of Term::Fabulous::Text::Style. | ||
| 96 | method check_spans ( $what, $given ) { | ||
| 97 | die "$owner: ${what}s must be an array reference of [start, end, style], got " . describe($given) unless ref $given eq 'ARRAY'; | ||
| 98 | return map { | ||
| 99 | die "$owner: a $what is [start, end, style], got " . describe($_) unless ref $_ eq 'ARRAY' && @$_ == 3; | ||
| 100 | $self->_span( $what, $_->[2], $_->[0], $_->[1] ); | ||
| 101 | } @$given; | ||
| 102 | } | ||
| 103 | |||
| 104 | method _span ( $what, $style, $start, $end ) { | ||
| 105 | return [ $self->_checked_range( $what, $start, $end ), style($style) ]; | ||
| 106 | } | ||
| 107 | |||
| 108 | method _checked_range ( $what, $start, $end ) { | ||
| 109 | $start = non_negative_integer( $owner, "$what start", $start ); | ||
| 110 | $end = non_negative_integer( $owner, "$what end", $end ); | ||
| 111 | my $length = length $text; | ||
| 112 | die "$owner: $what end $end lies before its start $start" if $end < $start; | ||
| 113 | die "$owner: $what end $end lies past the text ($length characters)" if $end > $length; | ||
| 114 | return ( $start, $end ); | ||
| 115 | } | ||
| 116 | |||
| 117 | # --------------------------------------------------------------------- | ||
| 118 | # Links | ||
| 119 | # --------------------------------------------------------------------- | ||
| 120 | |||
| 121 | method links () { | ||
| 122 | return [ map { [@$_] } @links ]; | ||
| 123 | } | ||
| 124 | |||
| 125 | method link_count () { | ||
| 126 | return scalar @links; | ||
| 127 | } | ||
| 128 | |||
| 129 | # The index of the link the character at the offset lies in, or undef. | ||
| 130 | method link_at ($offset) { | ||
| 131 | $offset = non_negative_integer( $owner, offset => $offset ); | ||
| 132 | return first { $links[$_][0] <= $offset && $offset < $links[$_][1] } 0 .. $#links; | ||
| 133 | } | ||
| 134 | |||
| 135 | # Puts a link in its place by start and returns its index there. | ||
| 136 | method add_link ( $target, $start = 0, $end = undef ) { | ||
| 137 | my $position = $self->_insert_link( $target, $start, $end // length $text ); | ||
| 138 | $content_revision++; | ||
| 139 | return $position; | ||
| 140 | } | ||
| 141 | |||
| 142 | method _insert_link ( $target, $start, $end ) { | ||
| 143 | die "$owner: a link needs a target" unless defined $target; | ||
| 144 | ( $start, $end ) = $self->_checked_range( link => $start, $end ); | ||
| 145 | die "$owner: a link must not be empty, got $start to $end" if $start == $end; | ||
| 146 | my $overlapped = first { $_->[0] < $end && $start < $_->[1] } @links; | ||
| 147 | die "$owner: the link $start to $end overlaps the link $overlapped->[0] to $overlapped->[1]" if defined $overlapped; | ||
| 148 | my $position = ( first { $links[$_][0] > $start } 0 .. $#links ) // scalar @links; | ||
| 149 | splice @links, $position, 0, [ $start, $end, $target ]; | ||
| 150 | return $position; | ||
| 151 | } | ||
| 152 | |||
| 153 | method clear_links () { | ||
| 154 | @links = (); | ||
| 155 | $content_revision++; | ||
| 156 | return $self; | ||
| 157 | } | ||
| 158 | |||
| 159 | # --------------------------------------------------------------------- | ||
| 160 | # Runs | ||
| 161 | # --------------------------------------------------------------------- | ||
| 162 | |||
| 163 | # The text cut at every boundary of its ranges, each piece with the | ||
| 164 | # styles that cover it stacked in order (under, then the spans, then | ||
| 165 | # the highlights, then over) and its palette tokens resolved against | ||
| 166 | # the theme: [ from, to, style hash ], covering the whole text. One | ||
| 167 | # sweep over the boundaries keeps the ranges that cover the piece at | ||
| 168 | # hand. | ||
| 169 | method runs (%options) { | ||
| 170 | my @unknown = grep { $_ ne 'theme' && $_ ne 'under' && $_ ne 'over' } sort keys %options; | ||
| 171 | die "Term::Fabulous::Text::Marks: runs does not take @unknown (known: theme under over)" if @unknown; | ||
| 172 | my $theme = $options{theme} // die "Term::Fabulous::Text::Marks: runs needs a theme"; | ||
| 173 | my @ranges = ( @{ $options{under} // [] }, @spans, @highlights, @{ $options{over} // [] } ); | ||
| 174 | my $length = length $text; | ||
| 175 | return [] unless $length; | ||
| 176 | |||
| 177 | my ( %starting_at, %ending_at ); | ||
| 178 | foreach my $index ( grep { $ranges[$_][0] < $ranges[$_][1] } 0 .. $#ranges ) { | ||
| 179 | push @{ $starting_at{ $ranges[$index][0] } }, $index; | ||
| 180 | push @{ $ending_at{ $ranges[$index][1] } }, $index; | ||
| 181 | } | ||
| 182 | my @boundaries = sort { $a <=> $b } uniqnum 0, $length, keys %starting_at, keys %ending_at; | ||
| 183 | |||
| 184 | my ( %covering, @runs ); | ||
| 185 | foreach my $index ( 1 .. $#boundaries ) { | ||
| 186 | my ( $from, $to ) = @boundaries[ $index - 1, $index ]; | ||
| 187 | delete @covering{ @{ $ending_at{$from} // [] } }; | ||
| 188 | $covering{$_} = 1 foreach @{ $starting_at{$from} // [] }; | ||
| 189 | my $style = compose_styles( map { $ranges[$_][2] } sort { $a <=> $b } keys %covering ); | ||
| 190 | push @runs, [ $from, $to, resolve_style( $style, $theme ) ]; | ||
| 191 | } | ||
| 192 | return \@runs; | ||
| 193 | } | ||
| 194 | } | ||
| 195 | |||
| 196 | 1; | ||
| 197 | |||
| 198 | __END__ | ||