summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authoraycabta <aycabta@gmail.com>2020-01-12 22:24:17 +0900
committeraycabta <aycabta@gmail.com>2020-01-14 15:40:38 +0900
commitf8ea2860b0cac1aec79978e6c44168802958e8af (patch)
treec34b0ee9cd9d6b2b7a4fc494d0f9563a7ce97968 /lib
parentc94025b63091be5b5e83a2f5ab5dc8d6c6147b84 (diff)
Introduce an abstracted structure about the encoding of Reline
The command prompt on Windows always uses Unicode to take input and print output but most Reline implementation depends on Encoding.default_external. This commit introduces an abstracted structure about the encoding of Reline.
Diffstat (limited to 'lib')
-rw-r--r--lib/reline.rb33
-rw-r--r--lib/reline/ansi.rb4
-rw-r--r--lib/reline/general_io.rb4
-rw-r--r--lib/reline/history.rb6
-rw-r--r--lib/reline/line_editor.rb10
-rw-r--r--lib/reline/windows.rb6
6 files changed, 40 insertions, 23 deletions
diff --git a/lib/reline.rb b/lib/reline.rb
index 9a9f742909..606dd4645b 100644
--- a/lib/reline.rb
+++ b/lib/reline.rb
@@ -38,8 +38,10 @@ module Reline
attr_accessor :ambiguous_width
attr_accessor :last_incremental_search
attr_reader :output
+ attr_reader :encoding
- def initialize
+ def initialize(encoding)
+ @encoding = encoding
self.output = STDOUT
yield self
@completion_quote_character = nil
@@ -49,36 +51,36 @@ module Reline
if val.nil?
@completion_append_character = nil
elsif val.size == 1
- @completion_append_character = val.encode(Encoding::default_external)
+ @completion_append_character = val.encode(@encoding)
elsif val.size > 1
- @completion_append_character = val[0].encode(Encoding::default_external)
+ @completion_append_character = val[0].encode(@encoding)
else
@completion_append_character = nil
end
end
def basic_word_break_characters=(v)
- @basic_word_break_characters = v.encode(Encoding::default_external)
+ @basic_word_break_characters = v.encode(@encoding)
end
def completer_word_break_characters=(v)
- @completer_word_break_characters = v.encode(Encoding::default_external)
+ @completer_word_break_characters = v.encode(@encoding)
end
def basic_quote_characters=(v)
- @basic_quote_characters = v.encode(Encoding::default_external)
+ @basic_quote_characters = v.encode(@encoding)
end
def completer_quote_characters=(v)
- @completer_quote_characters = v.encode(Encoding::default_external)
+ @completer_quote_characters = v.encode(@encoding)
end
def filename_quote_characters=(v)
- @filename_quote_characters = v.encode(Encoding::default_external)
+ @filename_quote_characters = v.encode(@encoding)
end
def special_prefixes=(v)
- @special_prefixes = v.encode(Encoding::default_external)
+ @special_prefixes = v.encode(@encoding)
end
def completion_case_fold=(v)
@@ -201,7 +203,7 @@ module Reline
otio = Reline::IOGate.prep
may_req_ambiguous_char_width
- line_editor.reset(prompt)
+ line_editor.reset(prompt, encoding: @encoding)
if multiline
line_editor.multiline_on
if block_given?
@@ -387,11 +389,15 @@ module Reline
def_instance_delegators self, :readmultiline
private :readmultiline
+ def self.encoding_system_needs
+ self.core.encoding
+ end
+
def self.core
- @core ||= Core.new { |core|
+ @core ||= Core.new(Reline::IOGate.encoding) { |core|
core.config = Reline::Config.new
core.key_stroke = Reline::KeyStroke.new(core.config)
- core.line_editor = Reline::LineEditor.new(core.config)
+ core.line_editor = Reline::LineEditor.new(core.config, Reline::IOGate.encoding)
core.basic_word_break_characters = " \t\n`><=;|&{("
core.completer_word_break_characters = " \t\n`><=;|&{("
@@ -405,8 +411,6 @@ module Reline
def self.line_editor
core.line_editor
end
-
- HISTORY = History.new(core.config)
end
if RbConfig::CONFIG['host_os'] =~ /mswin|msys|mingw|cygwin|bccwin|wince|emc/
@@ -422,4 +426,5 @@ else
require 'reline/ansi'
Reline::IOGate = Reline::ANSI
end
+Reline::HISTORY = Reline::History.new(Reline.core.config)
require 'reline/general_io'
diff --git a/lib/reline/ansi.rb b/lib/reline/ansi.rb
index cd780c6189..395721ea81 100644
--- a/lib/reline/ansi.rb
+++ b/lib/reline/ansi.rb
@@ -1,6 +1,10 @@
require 'io/console'
class Reline::ANSI
+ def self.encoding
+ Encoding.default_external
+ end
+
RAW_KEYSTROKE_CONFIG = {
[27, 91, 65] => :ed_prev_history, # ↑
[27, 91, 66] => :ed_next_history, # ↓
diff --git a/lib/reline/general_io.rb b/lib/reline/general_io.rb
index 291c14c7b3..6281d5fbf6 100644
--- a/lib/reline/general_io.rb
+++ b/lib/reline/general_io.rb
@@ -1,6 +1,10 @@
require 'timeout'
class Reline::GeneralIO
+ def self.encoding
+ Encoding.default_external
+ end
+
RAW_KEYSTROKE_CONFIG = {}
@@buf = []
diff --git a/lib/reline/history.rb b/lib/reline/history.rb
index 238fcf2a76..d95f1cebc3 100644
--- a/lib/reline/history.rb
+++ b/lib/reline/history.rb
@@ -19,7 +19,7 @@ class Reline::History < Array
def []=(index, val)
index = check_index(index)
- super(index, String.new(val, encoding: Encoding::default_external))
+ super(index, String.new(val, encoding: Reline.encoding_system_needs))
end
def concat(*val)
@@ -39,12 +39,12 @@ class Reline::History < Array
val.shift(diff)
end
end
- super(*(val.map{ |v| String.new(v, encoding: Encoding::default_external) }))
+ super(*(val.map{ |v| String.new(v, encoding: Reline.encoding_system_needs) }))
end
def <<(val)
shift if size + 1 > @config.history_size
- super(String.new(val, encoding: Encoding::default_external))
+ super(String.new(val, encoding: Reline.encoding_system_needs))
end
private def check_index(index)
diff --git a/lib/reline/line_editor.rb b/lib/reline/line_editor.rb
index 475f76fd65..dda602e155 100644
--- a/lib/reline/line_editor.rb
+++ b/lib/reline/line_editor.rb
@@ -57,10 +57,10 @@ class Reline::LineEditor
NON_PRINTING_END = "\2"
WIDTH_SCANNER = /\G(?:#{NON_PRINTING_START}|#{NON_PRINTING_END}|#{CSI_REGEXP}|#{OSC_REGEXP}|\X)/
- def initialize(config)
+ def initialize(config, encoding)
@config = config
@completion_append_character = ''
- reset_variables
+ reset_variables(encoding: encoding)
end
private def check_multiline_prompt(buffer, prompt)
@@ -85,10 +85,10 @@ class Reline::LineEditor
end
end
- def reset(prompt = '', encoding = Encoding.default_external)
+ def reset(prompt = '', encoding:)
@rest_height = (Reline::IOGate.get_screen_size.first - 1) - Reline::IOGate.cursor_pos.y
@screen_size = Reline::IOGate.get_screen_size
- reset_variables(prompt, encoding)
+ reset_variables(prompt, encoding: encoding)
@old_trap = Signal.trap('SIGINT') {
@old_trap.call if @old_trap.respond_to?(:call) # can also be string, ex: "DEFAULT"
raise Interrupt
@@ -139,7 +139,7 @@ class Reline::LineEditor
@eof
end
- def reset_variables(prompt = '', encoding = Encoding.default_external)
+ def reset_variables(prompt = '', encoding:)
@prompt = prompt
@mark_pointer = nil
@encoding = encoding
diff --git a/lib/reline/windows.rb b/lib/reline/windows.rb
index aef3073a7e..4fbf243c37 100644
--- a/lib/reline/windows.rb
+++ b/lib/reline/windows.rb
@@ -1,6 +1,10 @@
require 'fiddle/import'
class Reline::Windows
+ def self.encoding
+ Encoding::UTF_8
+ end
+
RAW_KEYSTROKE_CONFIG = {
[224, 72] => :ed_prev_history, # ↑
[224, 80] => :ed_next_history, # ↓
@@ -99,7 +103,7 @@ class Reline::Windows
return @@input_buf.shift
end
begin
- bytes = ret.chr(Encoding::UTF_8).encode(Encoding.default_external).bytes
+ bytes = ret.chr(Encoding::UTF_8).bytes
@@input_buf.push(*bytes)
rescue Encoding::UndefinedConversionError
@@input_buf << ret