From 8c3efa494091e6e0001f4a708fb7568c242387b9 Mon Sep 17 00:00:00 2001 From: aycabta Date: Tue, 14 Jan 2020 15:40:03 +0900 Subject: Use Reline.encoding_system_needs if exists --- lib/irb/ext/save-history.rb | 4 ++-- lib/irb/init.rb | 8 ++++++-- lib/irb/input-method.rb | 4 ++++ lib/irb/locale.rb | 11 ++++++++--- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/lib/irb/ext/save-history.rb b/lib/irb/ext/save-history.rb index c0d4d372b7..edc4f234cc 100644 --- a/lib/irb/ext/save-history.rb +++ b/lib/irb/ext/save-history.rb @@ -72,7 +72,7 @@ module IRB end history_file = IRB.rc_file("_history") unless history_file if File.exist?(history_file) - open(history_file) do |f| + open(history_file, "r:#{IRB.conf[:LC_MESSAGES].encoding}") do |f| f.each { |l| l = l.chomp if self.class == ReidlineInputMethod and history.last&.end_with?("\\") @@ -107,7 +107,7 @@ module IRB raise end - open(history_file, 'w', 0600 ) do |f| + open(history_file, "w:#{IRB.conf[:LC_MESSAGES].encoding}", 0600) do |f| hist = history.map{ |l| l.split("\n").join("\\\n") } f.puts(hist[-num..-1] || hist) end diff --git a/lib/irb/init.rb b/lib/irb/init.rb index 11f940da49..37d1f8d609 100644 --- a/lib/irb/init.rb +++ b/lib/irb/init.rb @@ -296,14 +296,18 @@ module IRB # :nodoc: DefaultEncodings = Struct.new(:external, :internal) class << IRB private - def set_encoding(extern, intern = nil) + def set_encoding(extern, intern = nil, override: true) verbose, $VERBOSE = $VERBOSE, nil Encoding.default_external = extern unless extern.nil? || extern.empty? Encoding.default_internal = intern unless intern.nil? || intern.empty? [$stdin, $stdout, $stderr].each do |io| io.set_encoding(extern, intern) end - @CONF[:LC_MESSAGES].instance_variable_set(:@encoding, extern) + if override + @CONF[:LC_MESSAGES].instance_variable_set(:@override_encoding, extern) + else + @CONF[:LC_MESSAGES].instance_variable_set(:@encoding, extern) + end ensure $VERBOSE = verbose end diff --git a/lib/irb/input-method.rb b/lib/irb/input-method.rb index a1777d7904..9fbbaeb0f3 100644 --- a/lib/irb/input-method.rb +++ b/lib/irb/input-method.rb @@ -133,6 +133,9 @@ module IRB include Readline # Creates a new input method object using Readline def initialize + if Readline.respond_to?(:encoding_system_needs) + IRB.__send__(:set_encoding, Readline.encoding_system_needs.name, override: false) + end super @line_no = 0 @@ -207,6 +210,7 @@ module IRB include Reline # Creates a new input method object using Readline def initialize + IRB.__send__(:set_encoding, Reline.encoding_system_needs.name, override: false) super @line_no = 0 diff --git a/lib/irb/locale.rb b/lib/irb/locale.rb index ba833eced4..bb44b41002 100644 --- a/lib/irb/locale.rb +++ b/lib/irb/locale.rb @@ -24,6 +24,7 @@ module IRB # :nodoc: @@loaded = [] def initialize(locale = nil) + @override_encoding = nil @lang = @territory = @encoding_name = @modifier = nil @locale = locale || ENV["IRB_LANG"] || ENV["LC_MESSAGES"] || ENV["LC_ALL"] || ENV["LANG"] || "C" if m = LOCALE_NAME_RE.match(@locale) @@ -40,12 +41,16 @@ module IRB # :nodoc: @encoding ||= (Encoding.find('locale') rescue Encoding::ASCII_8BIT) end - attr_reader :lang, :territory, :encoding, :modifier + attr_reader :lang, :territory, :modifier + + def encoding + @override_encoding || @encoding + end def String(mes) mes = super(mes) - if @encoding - mes.encode(@encoding, undef: :replace) + if encoding + mes.encode(encoding, undef: :replace) else mes end -- cgit v1.2.3