lib/Term/ReadLine/Perl5/OO/History.pm

filter: Statementstmt 77.6% 73 / 94 Branchbran 53.5% 15 / 28 Conditioncond 26.6% 4 / 15 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 78.9% 15 / 19 Podpod 92.8% 13 / 14 totaltotal 67.6% 121 / 179 CC 21 SCAR 18.5
CC
21
Cov
67%
add_line_to_history
30.0
remove_history
4.4
write_history
4.0
CRAP 6.4
« lib/Term/ReadLine/Perl5/Keymap.pm ↑ Summary blib/lib/Term/ReadLine/Perl5/OO/History.pm »
1 # Copyright (C) 2014 Rocky Bernstein <rocky@cpan.org>
2 package Term::ReadLine::Perl5::OO::History;
3 =pod
4  
5 =head1 NAME
6  
7 Term::ReadLine::Perl5::OO:History
8  
9 =head1 DESCRIPTION
10  
11 Variables and functions supporting L<Term::ReadLine::Perl5>'s and
12 L<Term::ReadLine::Perl5::OO>'s command history.
13  
14 =cut
15  
16 11 ▶use warnings; use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 =head1 SUBROUTINES
19  
20 =head2 add_line_to_history
21  
22 #B<add_line_to_history>(I<$line>, I<$minlength>)
23  
24 Insert I<$line> into history list if I<$line> is:
25  
26 =over
27  
28 =item *
29  
30 bigger than the minimal length I<$minlength>
31  
32 =item *
33  
34 not same as last entry
35  
36 =back
37  
38 =cut
39  
40 11 ▶use File::HomeDir; use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
41 11 my $HOME = File::HomeDir->my_home;
42  
43 sub add_line_to_history
44 {
45 0 ▶ my ($self, $line, $minlength) = @_;
Subroutine add_line_to_history: not called CC=5 SCAR=34.0
Pod documented
46 0 my @rl_History = @{$self->{rl_History}};
47 0 my $rl_MaxHistorySize = $self->{rl_MaxHistorySize};
48 0 ▶ if (length($line) >= $minlength
Branch: 1 branch
BranchTrueFalseTotal
if (length $line >= $minlength and !@rl_History || $rl_History[$#rl_History] ne $line) 0 0 0
Condition0%0/6
length $line >= $minlength and !@rl_History || $rl_History[$#rl_History] ne $line
!ll&&!rl&&r
000
not @rl_History or $rl_History[$#rl_History] ne $line
l!l&&r!l&&!r
000
MC/DC0%0/3
length $line >= $minlength and !@rl_History || $rl_History[$#rl_History] ne $line
length $line >= $minlength not @rl_History $rl_History[$#rl_History] ne $line
Truth table0%0/4
A and not(B) or C
A= length $line >= $minlengthB= not @rl_HistoryC= $rl_History[$#rl_History] ne $line
ABCresult
0XX0
1000
11X1
1011
49 && (!@rl_History || $rl_History[$#rl_History] ne $line)
50 ) {
51 ## if the history list is full, shift out an old one first....
52 0 ▶ while (@rl_History >= $self->{rl_MaxHistorySize}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rl_History >= $self->{"rl_MaxHistorySize"}) 0 0 0
53 0 shift(@rl_History);
54 0 $self->{rl_HistoryIndex}--;
55 }
56  
57 0 push(@rl_History, $line); ## tack new one on the end
58 0 $self->{rl_History} = \@rl_History;
59 }
60 }
61  
62 =head2 add_history
63  
64 #B<add_history>(I<$line1>, ...)
65  
66 Place lines in array I<@_> at the end of the history list unless the
67 history is stifled, or there are already too many items.
68  
69 =cut
70  
71 sub add_history {
72 8 ▶ my $self = shift;
Subroutine add_history: called CC=4 SCAR=13.9
Pod documented
73 8 ▶ if ($self->{history_stifled} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"history_stifled"} and $self->{"rl_history_length"} == $self->{"rl_max_input_history"}) 1 7 8
Condition66%2/3
$self->{"history_stifled"} and $self->{"rl_history_length"} == $self->{"rl_max_input_history"}
!ll&&!rl&&r
701
MC/DC50%1/2
$self->{"history_stifled"} and $self->{"rl_history_length"} == $self->{"rl_max_input_history"}
$self->{"history_stifled"} $self->{"rl_history_length"} == $self->{"rl_max_input_history"}
Truth table66%2/3
A and B
A= $self->{"history_stifled"}B= $self->{"rl_history_length"} == $self->{"rl_max_input_history"}
ABresult
0X0
100
111
74 ($self->{rl_history_length} ==
75 $self->{rl_max_input_history})) {
76 # If the history is stifled, and history_length is zero,
77 # and it equals max_input_history, we don't save items.
78 1 ▶ return if $self->{rl_max_input_history} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"rl_max_input_history"} == 0 0 1 1
79 1 shift @{$self->{rl_History}};
80 }
81 8 push @{$self->{rl_History}}, @_;
82 8 $self->{rl_HistoryIndex} += scalar @_;
83 8 $self->{rl_history_length} = scalar @{$self->{rl_History}};
84 }
85  
86  
87 =head2 read_history
88  
89 #B<read_history>(I<$filename>)
90  
91 Add the contents of I<$filename> to the history list, a line at a time. If
92 filename is undef, then read from `~/.history'. Returns 0 if
93 successful, or I<$!> if not.
94  
95 =cut
96  
97 sub read_history($;$) {
98 2 ▶ my ($self, $filename) = @_;
Subroutine read_history: called CC=4 SCAR=13.9
Pod documented
99 2 ▶ $filename = File::Spec->catfile($HOME, '.history') unless $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless $filename 0 2 2
100 2 my @history;
101 2 ▶ open(my $fh, '<:encoding(utf-8)', $filename ) or return $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(utf-8)", $filename or return $! 0 2 2
102 2 ▶ while (my $hist = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $hist = readline $fh)) 5 2 7
103 5 chomp($hist);
104 5 push @history, $hist;
105 };
106 # Use non OO form since this can be called in a non-OO way.
107 2 SetHistory($self, @history);
108 2 close $fh;
109 2 return 0;
110 }
111  
112 =head2 remove_history
113  
114 #B<remove_history>(I<unused>, I<$which>)
115  
116 Remove history element C<$which> from the history. The removed
117 element is returned.
118  
119 =cut
120  
121 sub remove_history($$) {
122 1 ▶ my ($self, $which) = @_;
Subroutine remove_history: called CC=4 SCAR=14.8
Pod documented
123 return undef if
124 1 ▶ $which < 0 || $which >= $self->{rl_history_length};
Branch: 1 branch
BranchTrueFalseTotal
if $which < 0 or $which >= $self->{"rl_history_length"} 0 1 1
Condition33%1/3
$which < 0 or $which >= $self->{"rl_history_length"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$which < 0 or $which >= $self->{"rl_history_length"}
$which < 0 $which >= $self->{"rl_history_length"}
Truth table33%1/3
A or B
A= $which < 0B= $which >= $self->{"rl_history_length"}
ABresult
1X1
011
000
125 1 my $removed = splice @{$self->{rl_History}}, $which, 1;
126 1 $self->{rl_history_length}--;
127 $self->{rl_HistoryIndex} =
128 $self->{rl_history_length} if
129 $self->{rl_history_length} <
130 1 ▶ $self->{rl_HistoryIndex};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"rl_history_length"} < $self->{"rl_HistoryIndex"} 1 0 1
131 1 return $removed;
132 }
133  
134 =head2 GetHistory
135  
136 B<GetHistory>
137  
138 returns the history of input as a list.
139  
140 =cut
141  
142 sub GetHistory {
143 8 ▶ my $self = shift;
Subroutine GetHistory: called CC=1 SCAR=0.0
Pod documented
144 8 @{$self->{rl_History}};
145 }
146  
147 =head2 SetHistory
148  
149 #B<SetHistory>(I<$line1> [, I<$line2>, ...])
150  
151 Sets the history of input, from where it can be used.
152  
153 =cut
154  
155 sub SetHistory {
156 7 ▶ my $self = shift;
Subroutine SetHistory: called CC=1 SCAR=0.0
Pod documented
157 7 $self->{rl_History} = \@_;
158 $self->{rl_HistoryIndex} =
159 7 $self->{rl_history_length} = $self->{rl_max_input_history} = scalar(@_);
160 }
161  
162 =head2 history_is_stifled
163  
164 C<history_is_stifled>
165  
166 Returns I<true> if saved history has a limited (stifled) or I<false>
167 if there is no limit (unstifled).
168  
169 =cut
170  
171 sub history_is_stifled {
172 5 ▶ my ($self) = shift;
Subroutine history_is_stifled: called CC=2 SCAR=6.9
Pod documented
173 5 ▶ $self->{history_stifled} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'history_stifled'} ? : 2 3 5
174 }
175  
176 =head2 unstifle_history
177  
178 C<unstifle_history>
179  
180 Unstifle or remove limit the history list.
181  
182 Theprevious maximum number of history entries is returned. The value
183 is positive if the history was stifled and negative if it wasn't.
184  
185 =cut
186  
187 sub unstifle_history($) {
188 1 ▶ my $self = shift;
Subroutine unstifle_history: called CC=2 SCAR=7.6
Pod documented
189 1 ▶ if ($self->{history_stifled}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'history_stifled'}) { } 1 0 1
190 1 $self->{history_stifled }= 0;
191 1 return (scalar @{$self->{rl_History}});
192 } else {
193 0 return - scalar @{$self->{rl_History}};
194 }
195 }
196  
197 =head2 replace_history_entry
198  
199 #B<replace_history_entry(I<$which>, I<$data>)>
200  
201 Make the history entry at I<$which> have I<$data>. This returns the old
202 entry. In the case of an invalid I<$which>, I<undef> is returned.
203  
204 =cut
205  
206 sub replace_history_entry {
207 1 ▶ my $self = shift;
Subroutine replace_history_entry: called CC=3 SCAR=11.6
Pod documented
208 1 my ($which, $data) = @_;
209 1 ▶ return undef if $which < 0 || $which >= $self->{rl_history_length};
Branch: 1 branch
BranchTrueFalseTotal
if $which < 0 or $which >= $self->{"rl_history_length"} 0 1 1
Condition33%1/3
$which < 0 or $which >= $self->{"rl_history_length"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$which < 0 or $which >= $self->{"rl_history_length"}
$which < 0 $which >= $self->{"rl_history_length"}
Truth table33%1/3
A or B
A= $which < 0B= $which >= $self->{"rl_history_length"}
ABresult
1X1
011
000
210 1 my $replaced = splice @{$self->{rl_History}}, $which, 1, $data;
211 1 return $replaced;
212 }
213  
214 =head2 clear_history
215  
216 #B<clear_history>()
217  
218 Clear or reset readline history.
219  
220 =cut
221  
222 sub clear_history($) {
223 0 ▶ my $self = shift;
Subroutine clear_history: not called CC=1 SCAR=6.9
Pod documented
224 0 $self->{rl_History} = [];
225 $self->{rl_HistoryIndex} =
226 0 $self->{rl_history_length} = 0;
227 }
228  
229 sub history_list($)
230 {
231 0 ▶ my $self = shift;
Subroutine history_list: not called CC=1 SCAR=6.9
Pod undocumented
232 0 my @rl_History = @{$self->{rl_History}};
233 0 @rl_History[1..$#rl_History]
234 }
235  
236 =head2 write_history
237 #B<write_history>(I<$filename>)
238  
239 Write the current history to filename, overwriting filename if
240 necessary. If filename is NULL, then write the history list to
241 `~/.history'. Returns 0 on success, or errno on a read or write error.
242  
243 I<read_history()> and I<write_history()> follow GNU Readline's C
244 convention of returning 0 for success and 1 for failure.
245  
246 =cut
247 sub write_history($$) {
248 2 ▶ my ($self, $filename) = @_;
Subroutine write_history: called CC=4 SCAR=14.0
Pod documented
249 2 ▶ open(my $fh, '>:encoding(utf-8)', $filename ) or return $!;
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:encoding(utf-8)", $filename or return $! 0 2 2
250 2 for my $hist (@{$self->{rl_History}}) {
251 5 ▶ next unless $hist =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $hist =~ /\S/ 0 5 5
252 5 print $fh $hist . "\n";
253 }
254 2 close $fh;
255 2 return 0;
256 }
257  
258 =head2 ReadHistory
259  
260 #B<ReadHistory>([I<$filename> [,I<$from> [,I<$to>]]])
261  
262 $i = ReadHistory('~/.history')
263  
264 Adds the contents of I<$filename> to the history list, a line at a
265 time. If $<filename> is false, then read from F<~/.history>. Start
266 reading at line I<$from> and end at I<$to>. If I<$from> is omitted or
267 zero, start at the beginning. If I<$to> is omitted or less than
268 I<$from>, then read until the end of the file. Returns true if
269 successful, or false if not.
270  
271 I<Note:> the return code is the negation of
272 L<read_history>. Otherwise, it's the same.
273  
274 =cut
275  
276 sub ReadHistory {
277 0 ▶ my ($self, $filename) = @_;
Subroutine ReadHistory: not called CC=1 SCAR=6.9
Pod documented
278 # Use non-OO form since this can be called in a non-OO way
279 0 ! read_history($self, $filename);
280 }
281  
282 =head2 WriteHistory
283  
284 #B<WriteHistory>([I<$filename>])
285  
286 $i = WriteHistory('~/.history')
287  
288 Writes the current history to I<$filename>, overwriting I<$filename>
289 if necessary. If I<$filename> is false, then write the history list
290 to F<~/.history>. Returns true if successful, or false if
291 not. I<Note:> the return code is the negation of
292 L<write_history>. Otherwise, it's the same.
293  
294 =cut
295  
296 sub WriteHistory {
297 # Use non-OO form since this can be called in a non-OO way
298 1 ▶ my ($self, $filename) = @_;
Subroutine WriteHistory: called CC=1 SCAR=0.0
Pod documented
299 1 ! write_history($self, $filename);
300 }
301 =head1 AUTHOR
302  
303 Rocky Bernstein
304  
305 =head1 SEE ALSO
306  
307 L<Term::ReadLine::Perl5>
308  
309 =cut
310  
311 11 1;
« lib/Term/ReadLine/Perl5/Keymap.pm ↑ Summary blib/lib/Term/ReadLine/Perl5/OO/History.pm »