mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
[ruby/reline] Negative history_size means unlimited
And unlimited is default. https://github.com/ruby/reline/commit/f5149c3ca6
This commit is contained in:
parent
d27fa87418
commit
4859352df6
3 changed files with 38 additions and 12 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Reference in a new issue