diff options
author | manga_osyo <manga.osyo@gmail.com> | 2019-06-23 13:29:09 +0900 |
---|---|---|
committer | aycabta <aycabta@gmail.com> | 2019-07-15 00:17:59 +0900 |
commit | 073cc52dcc5f0945e56877c703688517f58c6a65 (patch) | |
tree | db0914f7a4e8620a2c1c710fda57b12939d0476e /lib/reline | |
parent | 9806da50f49843c6983e3110a23ab7822c2e089d (diff) | |
download | ruby-073cc52dcc5f0945e56877c703688517f58c6a65.tar.gz |
Add `class Reline::History` and test.
Diffstat (limited to 'lib/reline')
-rw-r--r-- | lib/reline/history.rb | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/lib/reline/history.rb b/lib/reline/history.rb new file mode 100644 index 0000000000..d988230941 --- /dev/null +++ b/lib/reline/history.rb @@ -0,0 +1,60 @@ +class Reline::History < Array + def initialize(config) + @config = config + end + + def to_s + 'HISTORY' + end + + def delete_at(index) + index = check_index(index) + super(index) + end + + def [](index) + index = check_index(index) + super(index) + end + + def []=(index, val) + index = check_index(index) + 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 IndexError.new("index=<#{index}>") if index < 0 or size <= index + index + end + + private def set_config(config) + @config = config + end +end |