ruby-changes:60936
From: aycabta <ko1@a...>
Date: Wed, 29 Apr 2020 19:14:26 +0900 (JST)
Subject: [ruby-changes:60936] 4859352df6 (master): [ruby/reline] Negative history_size means unlimited
https://git.ruby-lang.org/ruby.git/commit/?id=4859352df6 From 4859352df62c05bbc10e1e62f966d493eb771e66 Mon Sep 17 00:00:00 2001 From: aycabta <aycabta@g...> Date: Sat, 25 Apr 2020 02:20:52 +0900 Subject: [ruby/reline] Negative history_size means unlimited And unlimited is default. https://github.com/ruby/reline/commit/f5149c3ca6 diff --git a/lib/reline/config.rb b/lib/reline/config.rb index fd761ae..c5e4450 100644 --- a/lib/reline/config.rb +++ b/lib/reline/config.rb @@ -52,7 +52,7 @@ class Reline::Config https://github.com/ruby/ruby/blob/trunk/lib/reline/config.rb#L52 @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 2e6a12e..7a1ed6b 100644 --- a/lib/reline/history.rb +++ b/lib/reline/history.rb @@ -31,29 +31,45 @@ class Reline::History < Array https://github.com/ruby/ruby/blob/trunk/lib/reline/history.rb#L31 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 < -@c..._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 < -@c..._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 0cb148d..58c240f 100644 --- a/test/reline/test_history.rb +++ b/test/reline/test_history.rb @@ -252,6 +252,16 @@ class Reline::History::Test < Reline::TestCase https://github.com/ruby/ruby/blob/trunk/test/reline/test_history.rb#L252 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) -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/