Skip to content

Commit f5149c3

Browse files
committed
Negative history_size means unlimited
And unlimited is default.
1 parent 9bdbed9 commit f5149c3

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

lib/reline/config.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def initialize
5252
@key_actors[:emacs] = Reline::KeyActor::Emacs.new
5353
@key_actors[:vi_insert] = Reline::KeyActor::ViInsert.new
5454
@key_actors[:vi_command] = Reline::KeyActor::ViCommand.new
55-
@history_size = 500
55+
@history_size = -1 # unlimited
5656
@keyseq_timeout = 500
5757
@test_mode = false
5858
end

lib/reline/history.rb

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,45 @@ def concat(*val)
3131
def push(*val)
3232
# If history_size is zero, all histories are dropped.
3333
return self if @config.history_size.zero?
34-
diff = size + val.size - @config.history_size
35-
if diff > 0
36-
if diff <= size
37-
shift(diff)
38-
else
39-
diff -= size
40-
clear
41-
val.shift(diff)
34+
# If history_size is negative, history size is unlimited.
35+
if @config.history_size.positive?
36+
diff = size + val.size - @config.history_size
37+
if diff > 0
38+
if diff <= size
39+
shift(diff)
40+
else
41+
diff -= size
42+
clear
43+
val.shift(diff)
44+
end
4245
end
4346
end
44-
super(*(val.map{ |v| String.new(v, encoding: Reline.encoding_system_needs) }))
47+
super(*(val.map{ |v|
48+
String.new(v, encoding: Reline.encoding_system_needs)
49+
}))
4550
end
4651

4752
def <<(val)
4853
# If history_size is zero, all histories are dropped.
4954
return self if @config.history_size.zero?
50-
shift if size + 1 > @config.history_size
55+
# If history_size is negative, history size is unlimited.
56+
if @config.history_size.positive?
57+
shift if size + 1 > @config.history_size
58+
end
5159
super(String.new(val, encoding: Reline.encoding_system_needs))
5260
end
5361

5462
private def check_index(index)
5563
index += size if index < 0
56-
raise RangeError.new("index=<#{index}>") if index < -@config.history_size or @config.history_size < index
64+
if index < -2147483648 or 2147483647 < index
65+
raise RangeError.new("integer #{index} too big to convert to `int'")
66+
end
67+
# If history_size is negative, history size is unlimited.
68+
if @config.history_size.positive?
69+
if index < -@config.history_size or @config.history_size < index
70+
raise RangeError.new("index=<#{index}>")
71+
end
72+
end
5773
raise IndexError.new("index=<#{index}>") if index < 0 or size <= index
5874
index
5975
end

test/reline/test_history.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,16 @@ def test_history_size_zero
252252
assert_equal 0, history.size
253253
end
254254

255+
def test_history_size_negative_unlimited
256+
history = history_new(history_size: -1)
257+
assert_equal 0, history.size
258+
history << 'aa'
259+
history << 'bb'
260+
assert_equal 2, history.size
261+
history.push(*%w{aa bb cc})
262+
assert_equal 5, history.size
263+
end
264+
255265
private
256266

257267
def history_new(history_size: 10)

0 commit comments

Comments
 (0)