From caef2ddaaf4a121272ad5c11d046ff4511c0f560 Mon Sep 17 00:00:00 2001 From: aycabta Date: Mon, 13 May 2019 03:26:10 +0900 Subject: [PATCH] Implement Reline::HISTORY as an expanded Array --- lib/reline.rb | 37 ++++++++++++++++++++++++++++++++++++- lib/reline/config.rb | 2 ++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/reline.rb b/lib/reline.rb index 9773f5cb3b..2639021863 100644 --- a/lib/reline.rb +++ b/lib/reline.rb @@ -9,7 +9,6 @@ module Reline extend self FILENAME_COMPLETION_PROC = nil USERNAME_COMPLETION_PROC = nil - HISTORY = Array.new if RUBY_PLATFORM =~ /mswin|mingw/ IS_WINDOWS = true @@ -33,6 +32,42 @@ module Reline @@line_editor = Reline::LineEditor.new(@@config) @@ambiguous_width = nil + HISTORY = Class.new(Array) { + 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 push(*val) + super(*(val.map{ |v| String.new(v, encoding: Encoding::default_external) })) + end + + def <<(val) + 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 + }.new + @basic_quote_characters = '"\'' # TODO implement below #@completer_quote_characters diff --git a/lib/reline/config.rb b/lib/reline/config.rb index 0a6a92fee2..a140959ca9 100644 --- a/lib/reline/config.rb +++ b/lib/reline/config.rb @@ -13,6 +13,7 @@ class Reline::Config enable-keypad expand-tilde history-preserve-point + history-size horizontal-scroll-mode input-meta mark-directories @@ -42,6 +43,7 @@ class Reline::Config @key_actors[:emacs] = Reline::KeyActor::Emacs.new @key_actors[:vi_insert] = Reline::KeyActor::ViInsert.new @key_actors[:vi_command] = Reline::KeyActor::ViCommand.new + @history_size = 500 end def reset