aboutsummaryrefslogtreecommitdiffstats
path: root/lib/reline
diff options
context:
space:
mode:
authormanga_osyo <manga.osyo@gmail.com>2019-06-23 13:29:09 +0900
committeraycabta <aycabta@gmail.com>2019-07-15 00:17:59 +0900
commit073cc52dcc5f0945e56877c703688517f58c6a65 (patch)
treedb0914f7a4e8620a2c1c710fda57b12939d0476e /lib/reline
parent9806da50f49843c6983e3110a23ab7822c2e089d (diff)
downloadruby-073cc52dcc5f0945e56877c703688517f58c6a65.tar.gz
Add `class Reline::History` and test.
Diffstat (limited to 'lib/reline')
-rw-r--r--lib/reline/history.rb60
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