summaryrefslogtreecommitdiff
path: root/lib
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
parent9806da50f49843c6983e3110a23ab7822c2e089d (diff)
Add `class Reline::History` and test.
Diffstat (limited to 'lib')
-rw-r--r--lib/reline.rb62
-rw-r--r--lib/reline/history.rb60
2 files changed, 62 insertions, 60 deletions
diff --git a/lib/reline.rb b/lib/reline.rb
index bf8967c561..5bb47d5d3f 100644
--- a/lib/reline.rb
+++ b/lib/reline.rb
@@ -5,6 +5,7 @@ require 'reline/config'
require 'reline/key_actor'
require 'reline/key_stroke'
require 'reline/line_editor'
+require 'reline/history'
module Reline
Key = Struct.new('Key', :char, :combined_char, :with_meta)
@@ -26,66 +27,7 @@ module Reline
@@line_editor = Reline::LineEditor.new(@@config)
@@ambiguous_width = nil
- HISTORY = Class.new(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
- }.new(@@config)
+ HISTORY = History.new(@@config)
@@completion_append_character = nil
def self.completion_append_character
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