diff options
author | aycabta <aycabta@gmail.com> | 2020-04-25 02:20:52 +0900 |
---|---|---|
committer | aycabta <aycabta@gmail.com> | 2020-04-29 19:13:14 +0900 |
commit | 4859352df62c05bbc10e1e62f966d493eb771e66 (patch) | |
tree | 7eb38097676d388d84cd677de7a01b3417b80e70 | |
parent | d27fa87418bcec8fff909f75a547a7c5e6dc83e5 (diff) | |
download | ruby-4859352df62c05bbc10e1e62f966d493eb771e66.tar.gz |
[ruby/reline] Negative history_size means unlimited
And unlimited is default.
https://github.com/ruby/reline/commit/f5149c3ca6
-rw-r--r-- | lib/reline/config.rb | 2 | ||||
-rw-r--r-- | lib/reline/history.rb | 38 | ||||
-rw-r--r-- | test/reline/test_history.rb | 10 |
3 files changed, 38 insertions, 12 deletions
diff --git a/lib/reline/config.rb b/lib/reline/config.rb index fd761aeb21..c5e4450034 100644 --- a/lib/reline/config.rb +++ b/lib/reline/config.rb @@ -52,7 +52,7 @@ class Reline::Config @key_actors[:emacs] = Reline::KeyActor::Emacs.new @key_actors[:vi_insert] = Reline::KeyActor::ViInsert.new @key_actors[:vi_command] = Reline::KeyActor::ViCommand.new - @history_size = 500 + @history_size = -1 # unlimited @keyseq_timeout = 500 @test_mode = false end diff --git a/lib/reline/history.rb b/lib/reline/history.rb index 2e6a12ecf3..7a1ed6b90b 100644 --- a/lib/reline/history.rb +++ b/lib/reline/history.rb @@ -31,29 +31,45 @@ class Reline::History < Array def push(*val) # If history_size is zero, all histories are dropped. return self if @config.history_size.zero? - diff = size + val.size - @config.history_size - if diff > 0 - if diff <= size - shift(diff) - else - diff -= size - clear - val.shift(diff) + # If history_size is negative, history size is unlimited. + if @config.history_size.positive? + diff = size + val.size - @config.history_size + if diff > 0 + if diff <= size + shift(diff) + else + diff -= size + clear + val.shift(diff) + end end end - super(*(val.map{ |v| String.new(v, encoding: Reline.encoding_system_needs) })) + super(*(val.map{ |v| + String.new(v, encoding: Reline.encoding_system_needs) + })) end def <<(val) # If history_size is zero, all histories are dropped. return self if @config.history_size.zero? - shift if size + 1 > @config.history_size + # If history_size is negative, history size is unlimited. + if @config.history_size.positive? + shift if size + 1 > @config.history_size + end super(String.new(val, encoding: Reline.encoding_system_needs)) end private def check_index(index) index += size if index < 0 - raise RangeError.new("index=<#{index}>") if index < -@config.history_size or @config.history_size < index + if index < -2147483648 or 2147483647 < index + raise RangeError.new("integer #{index} too big to convert to `int'") + end + # If history_size is negative, history size is unlimited. + if @config.history_size.positive? + if index < -@config.history_size or @config.history_size < index + raise RangeError.new("index=<#{index}>") + end + end raise IndexError.new("index=<#{index}>") if index < 0 or size <= index index end diff --git a/test/reline/test_history.rb b/test/reline/test_history.rb index 0cb148d112..58c240fc96 100644 --- a/test/reline/test_history.rb +++ b/test/reline/test_history.rb @@ -252,6 +252,16 @@ class Reline::History::Test < Reline::TestCase assert_equal 0, history.size end + def test_history_size_negative_unlimited + history = history_new(history_size: -1) + assert_equal 0, history.size + history << 'aa' + history << 'bb' + assert_equal 2, history.size + history.push(*%w{aa bb cc}) + assert_equal 5, history.size + end + private def history_new(history_size: 10) |