blib/lib/Tickit/Widget/Entry/Plugin/History.pm

filter: Statementstmt 14.2% 6 / 42 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 33.3% 2 / 6 Podpod 100% 1 / 1 totaltotal 12.0% 9 / 75 CC 11 SCAR 28.6
CC
11
Cov
9%
__ANON__
30.0
__ANON__
20.0
__ANON__
12.0
CRAP 17.5
↑ Summary
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2020 -- leonerd@leonerd.org.uk
5  
6 package Tickit::Widget::Entry::Plugin::History 0.01;
7  
8 1 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 C<Tickit::Widget::Entry::Plugin::History> - add readline-like history to a L<Tickit::Widget::Entry>
14  
15 =head1 SYNOPSIS
16  
17 use Tickit::Widget::Entry;
18 use Tickit::Widget::Entry::Plugin::History;
19  
20 my $entry = Tickit::Widget::Entry->new( ... );
21 Tickit::Widget::Entry::Plugin::History->apply( $entry );
22  
23 ...
24  
25 =head1 DESCRIPTION
26  
27 This package applies code to a L<Tickit::Widget::Entry> instance to implement
28 a history mechanism, which stores previously-entered values allowing them to
29 be recalled and reused later.
30  
31 =cut
32  
33 =head1 METHODS
34  
35 =cut
36  
37 =head2 apply
38  
39 Tickit::Widget::Entry::Plugin::History->apply( $entry, %opts )
40  
41 Applies the plugin code to the given L<Tickit::Widget::Entry> instance.
42  
43 The following named options are recognised:
44  
45 =over 4
46  
47 =item storage => ARRAY
48  
49 An optional reference to an array to store the history in. If absent, a new
50 anonymous array will be created.
51  
52 =item ignore_duplicates => BOOL
53  
54 If true, an entry will not be pushed into history if it is equal to the most
55 recent item already there.
56  
57 =back
58  
59 =cut
60  
61 sub apply
62 0 ▶{
Subroutine apply: not called CC=2 SCAR=17.9
Pod documented
63 0 my $class = shift;
64 0 my ( $entry, %opts ) = @_;
65  
66 0 ▶ my $storage = $opts{storage} // [];
Condition0%0/2
$opts{"storage"} // []
l!l
00
MC/DC0%0/1
$opts{"storage"} // []
$opts{"storage"}
Truth table0%0/2
A
A= $opts{"storage"}
Aresult
11
00
67 0 my $ignore_duplicates = !!$opts{ignore_duplicates};
68  
69 0 my $pending;
70 my $history_index;
71  
72 $entry->bind_keys(
73 0 ▶ Up => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
74 0 my ( $entry ) = @_;
75  
76 0 ▶ if( !defined $history_index ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $history_index) { } 0 0 0
elsif ($history_index == 0) { } 0 0 0
77 0 $pending = $entry->text;
78 0 ▶ return 1 unless @$storage;
Branch: 1 branch
BranchTrueFalseTotal
unless @$storage 0 0 0
79  
80 0 $history_index = $#$storage;
81 }
82 elsif( $history_index == 0 ) {
83 # don't move
84 0 return 1;
85 }
86 else {
87 0 $history_index--;
88 }
89  
90 0 my $line = $storage->[$history_index];
91 0 $entry->set_text( $line );
92 0 $entry->set_position( length $line );
93  
94 0 return 1;
95 },
96  
97 0 ▶ Down => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
98 0 my ( $entry ) = @_;
99  
100 0 ▶ return 1 unless defined $history_index;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $history_index 0 0 0
101 0 ▶ if( $history_index < $#$storage ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($history_index < $storage->$#*) { } 0 0 0
102 0 $history_index++;
103 }
104 else {
105 0 $entry->set_text( $pending );
106 0 undef $history_index;
107 0 return 1;
108 }
109  
110 0 my $line = $storage->[$history_index];
111 0 $entry->set_text( $line );
112 0 $entry->set_position( length $line );
113  
114 0 return 1;
115 },
116 0 );
117  
118 0 my $orig_on_enter = $entry->on_enter;
119 0 ▶ $entry->set_on_enter( sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
120 0 my $entry = shift;
121 0 my ( $line ) = @_;
122  
123 0 ▶ $entry->$orig_on_enter( $line ) if $orig_on_enter;
Branch: 1 branch
BranchTrueFalseTotal
if $orig_on_enter 0 0 0
124  
125 0 $entry->set_text( "" );
126  
127 0 ▶ push @$storage, $line unless $ignore_duplicates and @$storage and $line eq $storage->[-1];
Branch: 1 branch
BranchTrueFalseTotal
unless $ignore_duplicates and @$storage and $line eq $storage->[-1] 0 0 0
Condition0%0/6
$ignore_duplicates and @$storage and $line eq $storage->[-1]
!ll&&!rl&&r
000
$ignore_duplicates and @$storage
!ll&&!rl&&r
000
MC/DC0%0/3
$ignore_duplicates and @$storage and $line eq $storage->[-1]
$ignore_duplicates @$storage $line eq $storage->[-1]
Truth table0%0/4
A and B and C
A= $ignore_duplicatesB= @$storageC= $line eq $storage->[-1]
ABCresult
0XX0
10X0
1100
1111
128 # TODO: manage history size
129  
130 0 undef $history_index;
131 0 });
132 }
133  
134 =head1 AUTHOR
135  
136 Paul Evans <leonerd@leonerd.org.uk>
137  
138 =cut
139  
140 1 0x55AA;
↑ Summary