Skip to content

Commit

Permalink
The config file must accept any character encoding
Browse files Browse the repository at this point in the history
In Japan, so many programmers used EUC-JP to write text files that contain
Japanese. Many .inputrc files which contain EUC-JP are still being copied and
used. This commit supports the whole encoding of what user set including UTF-8.

ref. #280
  • Loading branch information
aycabta committed Apr 20, 2021
1 parent e4ac748 commit 0b45022
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 9 deletions.
5 changes: 4 additions & 1 deletion lib/reline/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ def reset_default_key_bindings
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 = []
Expand Down Expand Up @@ -292,7 +295,7 @@ def bind_variable(name, value)

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)
Expand Down
11 changes: 9 additions & 2 deletions lib/reline/general_io.rb
Original file line number Diff line number Diff line change
@@ -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?
Expand Down
3 changes: 2 additions & 1 deletion test/reline/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class <<self
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
Expand Down
17 changes: 14 additions & 3 deletions test/reline/test_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,25 @@ def test_inputrc
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
Expand Down
4 changes: 3 additions & 1 deletion test/reline/test_history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,9 @@ def assert_external_string_equal(expected, actual)
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")
Expand Down
8 changes: 7 additions & 1 deletion test/reline/test_reline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,12 @@ def test_may_req_ambiguous_char_width
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

0 comments on commit 0b45022

Please sign in to comment.