diff options
author | aycabta <aycabta@gmail.com> | 2020-01-12 22:24:17 +0900 |
---|---|---|
committer | aycabta <aycabta@gmail.com> | 2020-01-14 15:40:38 +0900 |
commit | f8ea2860b0cac1aec79978e6c44168802958e8af (patch) | |
tree | c34b0ee9cd9d6b2b7a4fc494d0f9563a7ce97968 /lib/reline | |
parent | c94025b63091be5b5e83a2f5ab5dc8d6c6147b84 (diff) | |
download | ruby-f8ea2860b0cac1aec79978e6c44168802958e8af.tar.gz |
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/reline')
-rw-r--r-- | lib/reline/ansi.rb | 4 | ||||
-rw-r--r-- | lib/reline/general_io.rb | 4 | ||||
-rw-r--r-- | lib/reline/history.rb | 6 | ||||
-rw-r--r-- | lib/reline/line_editor.rb | 10 | ||||
-rw-r--r-- | lib/reline/windows.rb | 6 |
5 files changed, 21 insertions, 9 deletions
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 |