Skip to content

Commit

Permalink
Remove extra items because Reline::HISTORY is a sized queue
Browse files Browse the repository at this point in the history
  • Loading branch information
aycabta committed May 28, 2019
1 parent 3b7862c commit c67934b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/reline.rb
Expand Up @@ -45,9 +45,23 @@ def []=(index, val)
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

Expand Down
27 changes: 27 additions & 0 deletions test/reline/test_key_actor_emacs.rb
Expand Up @@ -1181,6 +1181,33 @@ def test_ed_search_prev_history
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')
Expand Down

0 comments on commit c67934b

Please sign in to comment.