blib/lib/Term/Fabulous/Role/Searchable.pm

filter: Statementstmt 90.0% 18 / 20 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.5% 25 / 27 CC 1 SCAR 0.4
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Term/Fabulous/OptionList.pm ↑ Summary blib/lib/Term/Fabulous/Text/Marks.pm »
1 package Term::Fabulous::Role::Searchable;
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 Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 our @EXPORT_OK = qw(searchables_in nested_part nested_search_texts nested_search_revision nested_highlight_text nested_clear_text_highlights nested_reveal_text);
12  
13 1 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 0 ▶use Term::Fabulous::Check qw(describe);
Subroutine BEGIN: called CC=1 SCAR=2.6
15  
16 # The widgets of a subtree that answer a search: a widget that does the
17 # role answers for everything inside it; the children of any other
18 # widget are looked at in layout order.
19 sub searchables_in ($widget) {
20 return () unless blessed $widget;
21 return $widget if $widget->DOES(__PACKAGE__);
22 return () unless $widget->can('layout_children');
23 return map { searchables_in($_) } @{ $widget->layout_children };
24 }
25  
26 # A widget that holds other searchable widgets (the pages of a Tabs, the
27 # body of an accordion item) answers for them with keys of two parts:
28 # the index of the inner widget and that widget's own key.
29 sub nested_search_texts ($widgets) {
30 return map {
31 my $index = $_;
32 map { [ [ $index, $_->[0] ], $_->[1] ] } $widgets->[$index]->search_texts
33 } 0 .. $#$widgets;
34 }
35  
36 # The inner widget a nested key names, and its own key; no widget when
37 # the index is past the widgets that exist now.
38 sub _nested ( $what, $widgets, $key ) {
39 die "Term::Fabulous::Role::Searchable: $what needs a key of [ index, key ], got " . describe($key)
40 unless ref $key eq 'ARRAY' && @$key == 2 && defined $key->[0] && !ref $key->[0] && $key->[0] =~ /\A[0-9]+\z/;
41 return ( $widgets->[ $key->[0] ], $key->[1] );
42 }
43  
44 sub nested_part ( $widgets, $key ) {
45 my ($widget) = _nested( nested_part => $widgets, $key );
46 return $widget;
47 }
48  
49 # The revision names the widget at the index too: after the widgets
50 # changed, an old key may lead to another widget, whose own revision
51 # starts like any other.
52 sub nested_search_revision ( $widgets, $key ) {
53 my ( $widget, $inner ) = _nested( nested_search_revision => $widgets, $key );
54 return 'gone' unless defined $widget;
55 return $widget->search_serial . ':' . $widget->search_revision($inner);
56 }
57  
58 sub nested_highlight_text ( $widgets, $key, $ranges ) {
59 my ( $widget, $inner ) = _nested( nested_highlight_text => $widgets, $key );
60 $widget->highlight_text( $inner, $ranges ) if defined $widget;
61 return;
62 }
63  
64 sub nested_clear_text_highlights ($widgets) {
65 $_->clear_text_highlights foreach @$widgets;
66 return;
67 }
68  
69 sub nested_reveal_text ( $widgets, $key, $offset ) {
70 my ( $widget, $inner ) = _nested( nested_reveal_text => $widgets, $key );
71 return [ undef, undef ] unless defined $widget;
72 return $widget->reveal_text( $inner, $offset );
73 }
74  
75 use Object::Pad 0.825;
76  
77 role Term::Fabulous::Role::Searchable {
78 method search_texts;
79 method search_revision;
80 method highlight_text;
81 method clear_text_highlights;
82 method reveal_text;
83  
84 # Numbers every searchable widget for the life of the process; an
85 # address could be taken again by a widget made after one was freed.
86 my $last_serial = 0;
87  
88 field $search_serial :reader;
89  
90 ADJUST {
91 $search_serial = ++$last_serial;
92 }
93 }
94  
95 1;
96  
97 __END__
« blib/lib/Term/Fabulous/OptionList.pm ↑ Summary blib/lib/Term/Fabulous/Text/Marks.pm »