| 1 | package Term::Fabulous::Role::MarkedText; | ||
| 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 | use Term::Fabulous::Role::Searchable; | ||
| 13 | |||
| 14 | role Term::Fabulous::Role::MarkedText | ||
| 15 | :does(Term::Fabulous::Role::Searchable) | ||
| 16 | { | ||
| 17 | |||
| 18 | # The Term::Fabulous::Text::Marks of the text the widget shows. | ||
| 19 | method text_marks; | ||
| 20 | |||
| 21 | # Called after the marks changed: $is_content is true for the spans, | ||
| 22 | # false for the highlights, which are not content. | ||
| 23 | method marks_changed; | ||
| 24 | |||
| 25 | method spans (@new) { | ||
| 26 | return $self->text_marks->spans unless @new; | ||
| 27 | $self->text_marks->set_spans( $new[0] ); | ||
| 28 | $self->marks_changed(1); | ||
| 29 | return $self->text_marks->spans; | ||
| 30 | } | ||
| 31 | |||
| 32 | method stylize ( $style, $start = 0, $end = undef ) { | ||
| 33 | $self->text_marks->add_span( $style, $start, $end ); | ||
| 34 | $self->marks_changed(1); | ||
| 35 | return $self; | ||
| 36 | } | ||
| 37 | |||
| 38 | method clear_spans () { | ||
| 39 | $self->text_marks->set_spans( [] ); | ||
| 40 | $self->marks_changed(1); | ||
| 41 | return $self; | ||
| 42 | } | ||
| 43 | |||
| 44 | method highlights (@new) { | ||
| 45 | return $self->text_marks->highlights unless @new; | ||
| 46 | $self->text_marks->set_highlights( $new[0] ); | ||
| 47 | $self->marks_changed(0); | ||
| 48 | return $self->text_marks->highlights; | ||
| 49 | } | ||
| 50 | |||
| 51 | method clear_highlights () { | ||
| 52 | $self->text_marks->set_highlights( [] ); | ||
| 53 | $self->marks_changed(0); | ||
| 54 | return $self; | ||
| 55 | } | ||
| 56 | |||
| 57 | method content_revision () { | ||
| 58 | return $self->text_marks->content_revision; | ||
| 59 | } | ||
| 60 | |||
| 61 | # Searching (Term::Fabulous::Role::Searchable): the one text, whose | ||
| 62 | # key is undef. | ||
| 63 | |||
| 64 | method search_texts () { | ||
| 65 | return ( [ undef, $self->text_marks->text ] ); | ||
| 66 | } | ||
| 67 | |||
| 68 | method search_revision ($key) { | ||
| 69 | return $self->content_revision; | ||
| 70 | } | ||
| 71 | |||
| 72 | method highlight_text ( $key, $ranges ) { | ||
| 73 | $self->highlights($ranges); | ||
| 74 | return; | ||
| 75 | } | ||
| 76 | |||
| 77 | method clear_text_highlights () { | ||
| 78 | $self->clear_highlights; | ||
| 79 | return; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | 1; | ||
| 84 | |||
| 85 | __END__ |