diff --git a/lib/reline/config.rb b/lib/reline/config.rb index dd81a23ea7..ed5061f802 100644 --- a/lib/reline/config.rb +++ b/lib/reline/config.rb @@ -158,6 +158,9 @@ class Reline::Config end def read_lines(lines, file = nil) + if lines.first.encoding != Reline.encoding_system_needs + lines = lines.map { |l| l.encode(Reline.encoding_system_needs) } + end conditions = [@skip_section, @if_stack] @skip_section = nil @if_stack = [] @@ -293,7 +296,7 @@ class Reline::Config def retrieve_string(str) str = $1 if str =~ /\A"(.*)"\z/ - parse_keyseq(str).map { |c| c.chr(Reline::IOGate.encoding) }.join + parse_keyseq(str).map { |c| c.chr(Reline.encoding_system_needs) }.join end def bind_key(key, func_name) diff --git a/lib/reline/general_io.rb b/lib/reline/general_io.rb index 8c8e22d2e6..4f605628a3 100644 --- a/lib/reline/general_io.rb +++ b/lib/reline/general_io.rb @@ -1,12 +1,19 @@ require 'timeout' class Reline::GeneralIO - def self.reset + def self.reset(encoding: nil) @@pasting = false + @@encoding = encoding end def self.encoding - RUBY_PLATFORM =~ /mswin|mingw/ ? Encoding::UTF_8 : Encoding::default_external + if @@encoding + @@encoding + elsif RUBY_PLATFORM =~ /mswin|mingw/ + Encoding::UTF_8 + else + Encoding::default_external + end end def self.win? diff --git a/test/reline/helper.rb b/test/reline/helper.rb index 9712dde6c6..9963b93690 100644 --- a/test/reline/helper.rb +++ b/test/reline/helper.rb @@ -7,7 +7,8 @@ module Reline def test_mode remove_const('IOGate') if const_defined?('IOGate') const_set('IOGate', Reline::GeneralIO) - Reline::GeneralIO.reset + encoding = (RELINE_TEST_ENCODING rescue nil) + Reline::GeneralIO.reset(encoding: encoding) send(:core).config.instance_variable_set(:@test_mode, true) send(:core).config.reset end diff --git a/test/reline/test_config.rb b/test/reline/test_config.rb index 2ada00c154..56fc787b65 100644 --- a/test/reline/test_config.rb +++ b/test/reline/test_config.rb @@ -286,14 +286,25 @@ class Reline::Config::Test < Reline::TestCase ENV['INPUTRC'] = inputrc_backup end - def test_inputrc_with_utf + def test_inputrc_with_utf8 + # This file is encoded by UTF-8 so this heredoc string is also UTF-8. @config.read_lines(<<~'LINES'.lines) set editing-mode vi set vi-cmd-mode-string 🍸 set vi-ins-mode-string 🍶 LINES - assert_equal @config.vi_cmd_mode_string, "🍸" - assert_equal @config.vi_ins_mode_string, "🍶" + assert_equal '🍸', @config.vi_cmd_mode_string + assert_equal '🍶', @config.vi_ins_mode_string + end + + def test_inputrc_with_eucjp + @config.read_lines(<<~"LINES".encode(Encoding::EUC_JP).lines) + set editing-mode vi + set vi-cmd-mode-string ォャッ + set vi-ins-mode-string 能 + LINES + assert_equal 'ォャッ'.encode(Reline.encoding_system_needs), @config.vi_cmd_mode_string + assert_equal '能'.encode(Reline.encoding_system_needs), @config.vi_ins_mode_string end def test_xdg_config_home diff --git a/test/reline/test_history.rb b/test/reline/test_history.rb index 58c240fc96..849e50cb0a 100644 --- a/test/reline/test_history.rb +++ b/test/reline/test_history.rb @@ -292,7 +292,9 @@ class Reline::History::Test < Reline::TestCase end def get_default_internal_encoding - if RUBY_PLATFORM =~ /mswin|mingw/ + if encoding = (RELINE_TEST_ENCODING rescue nil) + encoding + elsif RUBY_PLATFORM =~ /mswin|mingw/ Encoding.default_internal || Encoding::UTF_8 else Encoding.default_internal || Encoding.find("locale") diff --git a/test/reline/test_reline.rb b/test/reline/test_reline.rb index 0f32ec4421..ac182bbb14 100644 --- a/test/reline/test_reline.rb +++ b/test/reline/test_reline.rb @@ -314,6 +314,12 @@ class Reline::Test < Reline::TestCase end def get_reline_encoding - RUBY_PLATFORM =~ /mswin|mingw/ ? Encoding::UTF_8 : Encoding::default_external + if encoding = (RELINE_TEST_ENCODING rescue nil) + encoding + elsif RUBY_PLATFORM =~ /mswin|mingw/ + Encoding::UTF_8 + else + Encoding::default_external + end end end