diff options
author | aycabta <aycabta@gmail.com> | 2019-05-29 10:04:39 +0900 |
---|---|---|
committer | aycabta <aycabta@gmail.com> | 2019-05-29 10:04:58 +0900 |
commit | 3f132979236f4aa42e3347a920123ce271a1e695 (patch) | |
tree | c2b1362a7e06a79439818dbf8e1dc5dad3e06d13 /lib | |
parent | c86d1fbed5e887ea7b59582510fb26ec912b7898 (diff) | |
download | ruby-3f132979236f4aa42e3347a920123ce271a1e695.tar.gz |
Remove extra items because Reline::HISTORY is a sized queue
Diffstat (limited to 'lib')
-rw-r--r-- | lib/reline.rb | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/lib/reline.rb b/lib/reline.rb index 656fab2fea..0c65d8738a 100644 --- a/lib/reline.rb +++ b/lib/reline.rb @@ -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 |