diff --git a/lib/reline.rb b/lib/reline.rb index df2b620448..8e7d4ff016 100644 --- a/lib/reline.rb +++ b/lib/reline.rb @@ -45,9 +45,23 @@ 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 - shift(diff) if diff > 0 + 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 diff --git a/test/reline/test_key_actor_emacs.rb b/test/reline/test_key_actor_emacs.rb index f76ce66dfe..e7596ff570 100644 --- a/test/reline/test_key_actor_emacs.rb +++ b/test/reline/test_key_actor_emacs.rb @@ -1181,6 +1181,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')