mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
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.
This commit is contained in:
parent
c94025b630
commit
f8ea2860b0
10 changed files with 48 additions and 30 deletions
|
@ -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'
|
||||
|
|
|
@ -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, # ↓
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
require 'timeout'
|
||||
|
||||
class Reline::GeneralIO
|
||||
def self.encoding
|
||||
Encoding.default_external
|
||||
end
|
||||
|
||||
RAW_KEYSTROKE_CONFIG = {}
|
||||
|
||||
@@buf = []
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -8,8 +8,8 @@ class Reline::KeyActor::Emacs::Test < Reline::TestCase
|
|||
Reline::HISTORY.instance_variable_set(:@config, @config)
|
||||
Reline::HISTORY.clear
|
||||
@encoding = (RELINE_TEST_ENCODING rescue Encoding.default_external)
|
||||
@line_editor = Reline::LineEditor.new(@config)
|
||||
@line_editor.reset(@prompt, @encoding)
|
||||
@line_editor = Reline::LineEditor.new(@config, @encoding)
|
||||
@line_editor.reset(@prompt, encoding: @encoding)
|
||||
end
|
||||
|
||||
def test_ed_insert_one
|
||||
|
|
|
@ -9,8 +9,8 @@ class Reline::KeyActor::ViInsert::Test < Reline::TestCase
|
|||
set editing-mode vi
|
||||
LINES
|
||||
@encoding = (RELINE_TEST_ENCODING rescue Encoding.default_external)
|
||||
@line_editor = Reline::LineEditor.new(@config)
|
||||
@line_editor.reset(@prompt, @encoding)
|
||||
@line_editor = Reline::LineEditor.new(@config, @encoding)
|
||||
@line_editor.reset(@prompt, encoding: @encoding)
|
||||
end
|
||||
|
||||
def test_vi_command_mode
|
||||
|
|
|
@ -3,7 +3,8 @@ require_relative 'helper'
|
|||
class Reline::MacroTest < Reline::TestCase
|
||||
def setup
|
||||
@config = Reline::Config.new
|
||||
@line_editor = Reline::LineEditor.new(@config)
|
||||
@encoding = (RELINE_TEST_ENCODING rescue Encoding.default_external)
|
||||
@line_editor = Reline::LineEditor.new(@config, @encoding)
|
||||
@line_editor.instance_variable_set(:@screen_size, [24, 80])
|
||||
@output = @line_editor.output = File.open(IO::NULL, "w")
|
||||
end
|
||||
|
|
|
@ -7,8 +7,8 @@ class Reline::LineEditor::StringProcessingTest < Reline::TestCase
|
|||
@config = Reline::Config.new
|
||||
Reline::HISTORY.instance_variable_set(:@config, @config)
|
||||
@encoding = (RELINE_TEST_ENCODING rescue Encoding.default_external)
|
||||
@line_editor = Reline::LineEditor.new(@config)
|
||||
@line_editor.reset(@prompt, @encoding)
|
||||
@line_editor = Reline::LineEditor.new(@config, @encoding)
|
||||
@line_editor.reset(@prompt, encoding: @encoding)
|
||||
end
|
||||
|
||||
def test_calculate_width
|
||||
|
|
Loading…
Add table
Reference in a new issue