Skip to content
Permalink
Browse files

Use Reline.encoding_system_needs if exists

  • Loading branch information
aycabta committed Jan 14, 2020
1 parent a2638c0 commit 8c3efa494091e6e0001f4a708fb7568c242387b9
Showing with 20 additions and 7 deletions.
  1. +2 −2 lib/irb/ext/save-history.rb
  2. +6 −2 lib/irb/init.rb
  3. +4 −0 lib/irb/input-method.rb
  4. +8 −3 lib/irb/locale.rb
@@ -72,7 +72,7 @@ def load_history
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 @@ def save_history
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
@@ -296,14 +296,18 @@ def IRB.load_modules
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
@@ -133,6 +133,9 @@ class ReadlineInputMethod < InputMethod
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 @@ class ReidlineInputMethod < InputMethod
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
@@ -24,6 +24,7 @@ class Locale
@@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 @@ def initialize(locale = nil)
@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

0 comments on commit 8c3efa4

Please sign in to comment.
You can’t perform that action at this time.