| 1 | # Copyright (C) 2014 Rocky Bernstein <rocky@cpan.org> | ||
| 2 | package Term::ReadLine::Perl5::History; | ||
| 3 | eval "use rlib '.' "; # rlib is now optional | ||
| 4 | use Term::ReadLine::Perl5::OO::History; | ||
| 5 | =pod | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Term::ReadLine::Perl5::History | ||
| 10 | |||
| 11 | =head1 DESCRIPTION | ||
| 12 | |||
| 13 | Variables and functions supporting L<Term::ReadLine::Perl5>'s command | ||
| 14 | history. This pretends to be OO code even though it isn't. It makes | ||
| 15 | use of global package variables and wraps that up as an object to pass | ||
| 16 | to the corresponding OO routine. | ||
| 17 | |||
| 18 | The underlying OO routines are in | ||
| 19 | L<Term::ReadLine::Perl5::OO::History>. | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | use warnings; use strict; | ||
| 24 | |||
| 25 | use vars qw(@EXPORT @ISA @rl_History $rl_HistoryIndex $rl_history_length | ||
| 26 | $rl_MaxHistorySize $rl_max_input_history $history_base | ||
| 27 | $history_stifled); | ||
| 28 | |||
| 29 | @ISA = qw(Exporter); | ||
| 30 | @EXPORT = qw(@rl_History $rl_HistoryIndex $rl_history_length | ||
| 31 | &add_line_to_history | ||
| 32 | $rl_MaxHistorySize $rl_max_input_history $history_stifled); | ||
| 33 | |||
| 34 | # FIXME: eventually we will remove these variables when we are fully OO. | ||
| 35 | @rl_History = (); | ||
| 36 | $rl_MaxHistorySize = 100; | ||
| 37 | $rl_history_length = 0; | ||
| 38 | $rl_max_input_history = 0; | ||
| 39 | $rl_history_length = $rl_max_input_history = 0; | ||
| 40 | |||
| 41 | $history_stifled = 0; | ||
| 42 | $history_base = 0; | ||
| 43 | |||
| 44 | =head1 SUBROUTINES | ||
| 45 | |||
| 46 | =head2 add_line_to_history | ||
| 47 | |||
| 48 | B<add_line_to_history>(I<$line>, I<$minlength>) | ||
| 49 | |||
| 50 | Insert I<$line> into history list if I<$line> is: | ||
| 51 | |||
| 52 | =over | ||
| 53 | |||
| 54 | =item * | ||
| 55 | |||
| 56 | bigger than the minimal length I<$minlength> | ||
| 57 | |||
| 58 | =item * | ||
| 59 | |||
| 60 | not same as last entry | ||
| 61 | |||
| 62 | =back | ||
| 63 | |||
| 64 | =cut | ||
| 65 | |||
| 66 | # Fake up a needed object using global state | ||
| 67 | sub _fake_self() { | ||
| 68 | { | ||
| 69 | rl_History => \@rl_History, | ||
| 70 | rl_MaxHistorySize => $rl_MaxHistorySize, | ||
| 71 | rl_HistoryIndex => $rl_HistoryIndex, | ||
| 72 | rl_history_length => $rl_history_length, | ||
| 73 | rl_max_input_history => $rl_max_input_history, | ||
| 74 | history_stifled => $history_stifled, | ||
| 75 | }; | ||
| 76 | } | ||
| 77 | |||
| 78 | |||
| 79 | # FIXME: DRY this adding a common routine to fake $self. | ||
| 80 | sub add_line_to_history | ||
| 81 | { | ||
| 82 | my ($line, $minlength) = @_; | ||
| 83 | |||
| 84 | my $self = _fake_self(); | ||
| 85 | Term::ReadLine::Perl5::OO::History::add_line_to_history( | ||
| 86 | $self, $line, $minlength); | ||
| 87 | } | ||
| 88 | |||
| 89 | |||
| 90 | sub add_history | ||
| 91 | { | ||
| 92 | shift; | ||
| 93 | my $self = _fake_self(); | ||
| 94 | Term::ReadLine::Perl5::OO::History::add_history($self, @_); | ||
| 95 | $rl_MaxHistorySize = $self->{rl_MaxHistorySize}; | ||
| 96 | $rl_HistoryIndex = $self->{rl_HistoryIndex}; | ||
| 97 | $rl_history_length = $self->{rl_history_length}; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub clear_history() | ||
| 101 | { | ||
| 102 | my $self = _fake_self(); | ||
| 103 | Term::ReadLine::Perl5::OO::History::clear_history($self); | ||
| 104 | @rl_History = @{$self->{rl_History}}; | ||
| 105 | $rl_MaxHistorySize = $self->{rl_MaxHistorySize}; | ||
| 106 | $rl_HistoryIndex = $self->{rl_HistoryIndex}; | ||
| 107 | $rl_history_length = $self->{rl_history_length}; | ||
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | sub GetHistory() | ||
| 112 | { | ||
| 113 | Term::ReadLine::Perl5::OO::History::GetHistory(_fake_self()); | ||
| 114 | } | ||
| 115 | |||
| 116 | sub history_is_stifled($) { | ||
| 117 | shift; | ||
| 118 | my $self = _fake_self(); | ||
| 119 | return Term::ReadLine::Perl5::OO::History::history_is_stifled($self); | ||
| 120 | } | ||
| 121 | |||
| 122 | sub read_history($$) { | ||
| 123 | my ($unused, $filename) = @_; | ||
| 124 | my $self = _fake_self(); | ||
| 125 | my $ret = Term::ReadLine::Perl5::OO::History::read_history($self, | ||
| 126 | $filename); | ||
| 127 | @rl_History = @{$self->{rl_History}}; | ||
| 128 | $rl_MaxHistorySize = $self->{rl_MaxHistorySize}; | ||
| 129 | $rl_HistoryIndex = $self->{rl_HistoryIndex}; | ||
| 130 | $rl_history_length = $self->{rl_history_length}; | ||
| 131 | return $ret; | ||
| 132 | } | ||
| 133 | |||
| 134 | sub ReadHistory($$) { | ||
| 135 | my ($unused, $filename) = @_; | ||
| 136 | ! read_history($unused, $filename); | ||
| 137 | } | ||
| 138 | |||
| 139 | sub remove_history($) | ||
| 140 | { | ||
| 141 | shift; | ||
| 142 | my ($which) = @_; | ||
| 143 | my $self = _fake_self(); | ||
| 144 | Term::ReadLine::Perl5::OO::History::remove_history($self, $which); | ||
| 145 | @rl_History = @{$self->{rl_History}}; | ||
| 146 | $rl_MaxHistorySize = $self->{rl_MaxHistorySize}; | ||
| 147 | $rl_HistoryIndex = $self->{rl_HistoryIndex}; | ||
| 148 | $rl_history_length = $self->{rl_history_length}; | ||
| 149 | } | ||
| 150 | |||
| 151 | sub replace_history_entry { | ||
| 152 | shift; | ||
| 153 | my ($which, $data) = @_; | ||
| 154 | my $self = _fake_self(); | ||
| 155 | my $replaced = | ||
| 156 | Term::ReadLine::Perl5::OO::History::replace_history_entry($self, | ||
| 157 | $which, | ||
| 158 | $data); | ||
| 159 | @rl_History = @{$self->{rl_History}}; | ||
| 160 | $rl_MaxHistorySize = $self->{rl_MaxHistorySize}; | ||
| 161 | $rl_HistoryIndex = $self->{rl_HistoryIndex}; | ||
| 162 | $rl_history_length = $self->{rl_history_length}; | ||
| 163 | return $replaced; | ||
| 164 | } | ||
| 165 | |||
| 166 | sub SetHistory | ||
| 167 | { | ||
| 168 | shift; | ||
| 169 | # Fake up a needed object using global state | ||
| 170 | my $self = _fake_self(); | ||
| 171 | Term::ReadLine::Perl5::OO::History::SetHistory($self, @_); | ||
| 172 | @rl_History = @{$self->{rl_History}}; | ||
| 173 | $rl_MaxHistorySize = $self->{rl_MaxHistorySize}; | ||
| 174 | $rl_HistoryIndex = $self->{rl_HistoryIndex}; | ||
| 175 | $rl_history_length = $self->{rl_history_length}; | ||
| 176 | } | ||
| 177 | |||
| 178 | sub unstifle_history($) | ||
| 179 | { | ||
| 180 | shift; | ||
| 181 | my $self = _fake_self(); | ||
| 182 | Term::ReadLine::Perl5::OO::History::unstifle_history($self); | ||
| 183 | $history_stifled = $self->{history_stifled}; | ||
| 184 | } | ||
| 185 | |||
| 186 | sub write_history($$) { | ||
| 187 | my ($unused, $filename) = @_; | ||
| 188 | my $self = _fake_self(); | ||
| 189 | Term::ReadLine::Perl5::OO::History::write_history($self, | ||
| 190 | $filename); | ||
| 191 | } | ||
| 192 | |||
| 193 | sub WriteHistory($$) { | ||
| 194 | my ($unused, $filename) = @_; | ||
| 195 | my $self = _fake_self(); | ||
| 196 | Term::ReadLine::Perl5::OO::History::WriteHistory($self, | ||
| 197 | $filename); | ||
| 198 | } | ||
| 199 | |||
| 200 | |||
| 201 | 1; |