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

filter: Statementstmt 0.0% 0 / 93 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 19 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 164 CC 11 SCAR 17.7
CC
11
Cov
0%
add_history
12.0
remove_history
12.0
add_line_to_history
6.0
CRAP 5.9
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 use warnings; use strict;
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 use File::HomeDir; use File::Spec;
41 my $HOME = File::HomeDir->my_home;
42  
43 sub add_line_to_history
44 {
45 my ($self, $line, $minlength) = @_;
46 my @rl_History = @{$self->{rl_History}};
47 my $rl_MaxHistorySize = $self->{rl_MaxHistorySize};
48 if (length($line) >= $minlength
49 && (!@rl_History || $rl_History[$#rl_History] ne $line)
50 ) {
51 ## if the history list is full, shift out an old one first....
52 while (@rl_History >= $self->{rl_MaxHistorySize}) {
53 shift(@rl_History);
54 $self->{rl_HistoryIndex}--;
55 }
56  
57 push(@rl_History, $line); ## tack new one on the end
58 $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 my $self = shift;
73 if ($self->{history_stifled} &&
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 return if $self->{rl_max_input_history} == 0;
79 shift @{$self->{rl_History}};
80 }
81 push @{$self->{rl_History}}, @_;
82 $self->{rl_HistoryIndex} += scalar @_;
83 $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 my ($self, $filename) = @_;
99 $filename = File::Spec->catfile($HOME, '.history') unless $filename;
100 my @history;
101 open(my $fh, '<:encoding(utf-8)', $filename ) or return $!;
102 while (my $hist = <$fh>) {
103 chomp($hist);
104 push @history, $hist;
105 };
106 # Use non OO form since this can be called in a non-OO way.
107 SetHistory($self, @history);
108 close $fh;
109 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 my ($self, $which) = @_;
123 return undef if
124 $which < 0 || $which >= $self->{rl_history_length};
125 my $removed = splice @{$self->{rl_History}}, $which, 1;
126 $self->{rl_history_length}--;
127 $self->{rl_HistoryIndex} =
128 $self->{rl_history_length} if
129 $self->{rl_history_length} <
130 $self->{rl_HistoryIndex};
131 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 my $self = shift;
144 @{$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 my $self = shift;
157 $self->{rl_History} = \@_;
158 $self->{rl_HistoryIndex} =
159 $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 my ($self) = shift;
173 $self->{history_stifled} ? 1 : 0;
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 my $self = shift;
189 if ($self->{history_stifled}) {
190 $self->{history_stifled }= 0;
191 return (scalar @{$self->{rl_History}});
192 } else {
193 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 my $self = shift;
208 my ($which, $data) = @_;
209 return undef if $which < 0 || $which >= $self->{rl_history_length};
210 my $replaced = splice @{$self->{rl_History}}, $which, 1, $data;
211 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 my $self = shift;
224 $self->{rl_History} = [];
225 $self->{rl_HistoryIndex} =
226 $self->{rl_history_length} = 0;
227 }
228  
229 sub history_list($)
230 {
231 my $self = shift;
232 my @rl_History = @{$self->{rl_History}};
233 @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 my ($self, $filename) = @_;
249 open(my $fh, '>:encoding(utf-8)', $filename ) or return $!;
250 for my $hist (@{$self->{rl_History}}) {
251 next unless $hist =~ /\S/;
252 print $fh $hist . "\n";
253 }
254 close $fh;
255 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 my ($self, $filename) = @_;
278 # Use non-OO form since this can be called in a non-OO way
279 ! 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 my ($self, $filename) = @_;
299 ! 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 1;