mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Remove extra items because Reline::HISTORY is a sized queue
This commit is contained in:
parent
c86d1fbed5
commit
3f13297923
2 changed files with 55 additions and 2 deletions
|
@ -26,6 +26,10 @@ module Reline
|
|||
@@ambiguous_width = nil
|
||||
|
||||
HISTORY = Class.new(Array) {
|
||||
def initialize(config)
|
||||
@config = config
|
||||
end
|
||||
|
||||
def to_s
|
||||
'HISTORY'
|
||||
end
|
||||
|
@ -45,21 +49,42 @@ module Reline
|
|||
super(index, String.new(val, encoding: Encoding::default_external))
|
||||
end
|
||||
|
||||
def concat(*val)
|
||||
val.each do |v|
|
||||
push(*v)
|
||||
end
|
||||
end
|
||||
|
||||
def push(*val)
|
||||
diff = size + val.size - @config.history_size
|
||||
if diff > 0
|
||||
if diff <= size
|
||||
shift(diff)
|
||||
else
|
||||
diff -= size
|
||||
clear
|
||||
val.shift(diff)
|
||||
end
|
||||
end
|
||||
super(*(val.map{ |v| String.new(v, encoding: Encoding::default_external) }))
|
||||
end
|
||||
|
||||
def <<(val)
|
||||
shift if size + 1 > @config.history_size
|
||||
super(String.new(val, encoding: Encoding::default_external))
|
||||
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
|
||||
raise RangeError.new("index=<#{index}>") if index < -@config.history_size or @config.history_size < index
|
||||
raise IndexError.new("index=<#{index}>") if index < 0 or size <= index
|
||||
index
|
||||
end
|
||||
}.new
|
||||
|
||||
private def set_config(config)
|
||||
@config = config
|
||||
end
|
||||
}.new(@@config)
|
||||
|
||||
@@completion_append_character = nil
|
||||
def self.completion_append_character
|
||||
|
|
|
@ -5,6 +5,7 @@ class Reline::KeyActor::Emacs::Test < Reline::TestCase
|
|||
Reline.send(:test_mode)
|
||||
@prompt = '> '
|
||||
@config = Reline::Config.new # Emacs mode is default
|
||||
Reline::HISTORY.send(:set_config, @config)
|
||||
@encoding = (RELINE_TEST_ENCODING rescue Encoding.default_external)
|
||||
@line_editor = Reline::LineEditor.new(@config)
|
||||
@line_editor.reset(@prompt, @encoding)
|
||||
|
@ -1181,6 +1182,33 @@ class Reline::KeyActor::Emacs::Test < Reline::TestCase
|
|||
assert_cursor_max(3)
|
||||
end
|
||||
|
||||
def test_larger_histories_than_history_size
|
||||
history_size = @config.history_size
|
||||
@config.history_size = 2
|
||||
Reline::HISTORY.concat(%w{abc 123 AAA})
|
||||
assert_line('')
|
||||
assert_byte_pointer_size('')
|
||||
assert_cursor(0)
|
||||
assert_cursor_max(0)
|
||||
input_keys("\C-p")
|
||||
assert_line('AAA')
|
||||
assert_byte_pointer_size('AAA')
|
||||
assert_cursor(3)
|
||||
assert_cursor_max(3)
|
||||
input_keys("\C-p")
|
||||
assert_line('123')
|
||||
assert_byte_pointer_size('123')
|
||||
assert_cursor(3)
|
||||
assert_cursor_max(3)
|
||||
input_keys("\C-p")
|
||||
assert_line('123')
|
||||
assert_byte_pointer_size('123')
|
||||
assert_cursor(3)
|
||||
assert_cursor_max(3)
|
||||
ensure
|
||||
@config.history_size = history_size
|
||||
end
|
||||
|
||||
=begin # TODO: move KeyStroke instance from Reline to LineEditor
|
||||
def test_key_delete
|
||||
input_keys('ab')
|
||||
|
|
Loading…
Add table
Reference in a new issue