Skip to content

Commit

Permalink
Merge pull request #147 from aycabta/history-size-zero-drop
Browse files Browse the repository at this point in the history
Fix history_size behaviours
  • Loading branch information
aycabta authored Apr 25, 2020
2 parents b349c50 + f5149c3 commit c9401fa
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/reline/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def initialize
@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
Expand Down
42 changes: 31 additions & 11 deletions lib/reline/history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,47 @@ def concat(*val)
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)
# If history_size is zero, all histories are dropped.
return self if @config.history_size.zero?
# 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)
shift if size + 1 > @config.history_size
# If history_size is zero, all histories are dropped.
return self if @config.history_size.zero?
# 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
Expand Down
20 changes: 20 additions & 0 deletions test/reline/test_history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,26 @@ def test_delete_at__out_of_range
end
end

def test_history_size_zero
history = history_new(history_size: 0)
assert_equal 0, history.size
history << 'aa'
history << 'bb'
assert_equal 0, history.size
history.push(*%w{aa bb cc})
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)
Expand Down

0 comments on commit c9401fa

Please sign in to comment.