Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use IO's encoding instead of Encoding.default_external #765

Merged
merged 4 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/reline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ def readline(prompt = '', add_hist = false)
otio = io_gate.prep

may_req_ambiguous_char_width
line_editor.reset(prompt, encoding: encoding)
line_editor.reset(prompt)
if multiline
line_editor.multiline_on
if block_given?
Expand Down Expand Up @@ -487,7 +487,7 @@ def self.core
@core ||= Core.new { |core|
core.config = Reline::Config.new
core.key_stroke = Reline::KeyStroke.new(core.config)
core.line_editor = Reline::LineEditor.new(core.config, core.encoding)
core.line_editor = Reline::LineEditor.new(core.config)

core.basic_word_break_characters = " \t\n`><=;|&{("
core.completer_word_break_characters = " \t\n`><=;|&{("
Expand Down
2 changes: 1 addition & 1 deletion lib/reline/io/ansi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def initialize
end

def encoding
Encoding.default_external
@input.external_encoding || Encoding.default_external
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change fails one of irb's test. Should be fixed by ruby/irb#1022

end

def set_default_key_bindings(config, allow_terminfo: true)
Expand Down
2 changes: 1 addition & 1 deletion lib/reline/io/dumb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def encoding
elsif RUBY_PLATFORM =~ /mswin|mingw/
Encoding::UTF_8
else
Encoding::default_external
@input.external_encoding || Encoding::default_external
end
end

Expand Down
49 changes: 26 additions & 23 deletions lib/reline/line_editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,21 @@ def lines(screen_width)

MINIMUM_SCROLLBAR_HEIGHT = 1

def initialize(config, encoding)
def initialize(config)
@config = config
@completion_append_character = ''
@screen_size = [0, 0] # Should be initialized with actual winsize in LineEditor#reset
reset_variables(encoding: encoding)
reset_variables
end

def io_gate
Reline::IOGate
end

def encoding
io_gate.encoding
end

def set_pasting_state(in_pasting)
# While pasting, text to be inserted is stored to @continuous_insertion_buffer.
# After pasting, this buffer should be force inserted.
Expand Down Expand Up @@ -136,9 +140,9 @@ def set_pasting_state(in_pasting)
end
end

def reset(prompt = '', encoding:)
def reset(prompt = '')
@screen_size = Reline::IOGate.get_screen_size
reset_variables(prompt, encoding: encoding)
reset_variables(prompt)
@rendered_screen.base_y = Reline::IOGate.cursor_pos.y
if ENV.key?('RELINE_ALT_SCROLLBAR')
@full_block = '::'
Expand All @@ -150,7 +154,7 @@ def reset(prompt = '', encoding:)
@upper_half_block = '▀'
@lower_half_block = '▄'
@block_elem_width = 1
elsif @encoding == Encoding::UTF_8
elsif encoding == Encoding::UTF_8
@full_block = '█'
@upper_half_block = '▀'
@lower_half_block = '▄'
Expand Down Expand Up @@ -219,10 +223,9 @@ def eof?
@eof
end

def reset_variables(prompt = '', encoding:)
def reset_variables(prompt = '')
@prompt = prompt.gsub("\n", "\\n")
@mark_pointer = nil
@encoding = encoding
@is_multiline = false
@finished = false
@history_pointer = nil
Expand All @@ -239,7 +242,7 @@ def reset_variables(prompt = '', encoding:)
@searching_prompt = nil
@just_cursor_moving = false
@eof = false
@continuous_insertion_buffer = String.new(encoding: @encoding)
@continuous_insertion_buffer = String.new(encoding: encoding)
@scroll_partial_screen = 0
@drop_terminate_spaces = false
@in_pasting = false
Expand All @@ -259,7 +262,7 @@ def reset_variables(prompt = '', encoding:)

def reset_line
@byte_pointer = 0
@buffer_of_lines = [String.new(encoding: @encoding)]
@buffer_of_lines = [String.new(encoding: encoding)]
@line_index = 0
@cache.clear
@line_backup_in_history = nil
Expand All @@ -275,7 +278,7 @@ def multiline_off
end

private def insert_new_line(cursor_line, next_line)
@buffer_of_lines.insert(@line_index + 1, String.new(next_line, encoding: @encoding))
@buffer_of_lines.insert(@line_index + 1, String.new(next_line, encoding: encoding))
@buffer_of_lines[@line_index] = cursor_line
@line_index += 1
@byte_pointer = 0
Expand All @@ -298,7 +301,7 @@ def multiline_off
end

private def split_by_width(str, max_width, offset: 0)
Reline::Unicode.split_by_width(str, max_width, @encoding, offset: offset)
Reline::Unicode.split_by_width(str, max_width, encoding, offset: offset)
end

def current_byte_pointer_cursor
Expand Down Expand Up @@ -882,8 +885,8 @@ def editing_mode
perform_completion(list, true) if @config.show_all_if_ambiguous
end
if not just_show_list and target < completed
@buffer_of_lines[@line_index] = (preposing + completed + completion_append_character.to_s + postposing).split("\n")[@line_index] || String.new(encoding: @encoding)
line_to_pointer = (preposing + completed + completion_append_character.to_s).split("\n")[@line_index] || String.new(encoding: @encoding)
@buffer_of_lines[@line_index] = (preposing + completed + completion_append_character.to_s + postposing).split("\n")[@line_index] || String.new(encoding: encoding)
line_to_pointer = (preposing + completed + completion_append_character.to_s).split("\n")[@line_index] || String.new(encoding: encoding)
@byte_pointer = line_to_pointer.bytesize
end
end
Expand Down Expand Up @@ -1058,8 +1061,8 @@ def wrap_method_call(method_symbol, method_obj, key, with_operator = false)
private def normal_char(key)
@multibyte_buffer << key.combined_char
if @multibyte_buffer.size > 1
if @multibyte_buffer.dup.force_encoding(@encoding).valid_encoding?
process_key(@multibyte_buffer.dup.force_encoding(@encoding), nil)
if @multibyte_buffer.dup.force_encoding(encoding).valid_encoding?
process_key(@multibyte_buffer.dup.force_encoding(encoding), nil)
@multibyte_buffer.clear
else
# invalid
Expand Down Expand Up @@ -1317,7 +1320,7 @@ def retrieve_completion_block(set_completion_quote_character = false)
if (lines.size - 1) > @line_index
postposing = postposing + "\n" + lines[(@line_index + 1)..-1].join("\n")
end
[preposing.encode(@encoding), target.encode(@encoding), postposing.encode(@encoding)]
[preposing.encode(encoding), target.encode(encoding), postposing.encode(encoding)]
end

def confirm_multiline_termination
Expand All @@ -1329,7 +1332,7 @@ def insert_multiline_text(text)
save_old_buffer
pre = @buffer_of_lines[@line_index].byteslice(0, @byte_pointer)
post = @buffer_of_lines[@line_index].byteslice(@byte_pointer..)
lines = (pre + Reline::Unicode.safe_encode(text, @encoding).gsub(/\r\n?/, "\n") + post).split("\n", -1)
lines = (pre + Reline::Unicode.safe_encode(text, encoding).gsub(/\r\n?/, "\n") + post).split("\n", -1)
lines << '' if lines.empty?
@buffer_of_lines[@line_index, 1] = lines
@line_index += lines.size - 1
Expand Down Expand Up @@ -1374,7 +1377,7 @@ def delete_text(start = nil, length = nil)
last += current_line.bytesize if last < 0
first += current_line.bytesize if first < 0
range = range.exclude_end? ? first...last : first..last
line = current_line.bytes.reject.with_index{ |c, i| range.include?(i) }.map{ |c| c.chr(Encoding::ASCII_8BIT) }.join.force_encoding(@encoding)
line = current_line.bytes.reject.with_index{ |c, i| range.include?(i) }.map{ |c| c.chr(Encoding::ASCII_8BIT) }.join.force_encoding(encoding)
set_current_line(line)
else
set_current_line(current_line.byteslice(0, start))
Expand Down Expand Up @@ -1585,7 +1588,7 @@ def finish
alias_method :end_of_line, :ed_move_to_end

private def generate_searcher(search_key)
search_word = String.new(encoding: @encoding)
search_word = String.new(encoding: encoding)
multibyte_buf = String.new(encoding: 'ASCII-8BIT')
hit_pointer = nil
lambda do |key|
Expand All @@ -1602,8 +1605,8 @@ def finish
search_key = key
else
multibyte_buf << key
if multibyte_buf.dup.force_encoding(@encoding).valid_encoding?
search_word << multibyte_buf.dup.force_encoding(@encoding)
if multibyte_buf.dup.force_encoding(encoding).valid_encoding?
search_word << multibyte_buf.dup.force_encoding(encoding)
multibyte_buf.clear
end
end
Expand Down Expand Up @@ -1763,7 +1766,7 @@ def finish
@history_pointer = history_pointer
end
@buffer_of_lines = buf.split("\n")
@buffer_of_lines = [String.new(encoding: @encoding)] if @buffer_of_lines.empty?
@buffer_of_lines = [String.new(encoding: encoding)] if @buffer_of_lines.empty?
@line_index = line == :start ? 0 : line == :end ? @buffer_of_lines.size - 1 : line
@byte_pointer = cursor == :start ? 0 : cursor == :end ? current_line.bytesize : cursor
end
Expand Down Expand Up @@ -2288,7 +2291,7 @@ def finish
}
system("#{ENV['EDITOR']} #{path}")
@buffer_of_lines = File.read(path).split("\n")
@buffer_of_lines = [String.new(encoding: @encoding)] if @buffer_of_lines.empty?
@buffer_of_lines = [String.new(encoding: encoding)] if @buffer_of_lines.empty?
@line_index = 0
finish
end
Expand Down
2 changes: 1 addition & 1 deletion test/reline/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class Reline::TestCase < Test::Unit::TestCase
if Reline::Unicode::EscapedChars.include?(c.ord)
c
else
c.encode(@line_editor.instance_variable_get(:@encoding), Encoding::UTF_8, **options)
c.encode(@line_editor.encoding, Encoding::UTF_8, **options)
end
}.join
rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError
Expand Down
4 changes: 2 additions & 2 deletions test/reline/test_key_actor_emacs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ def setup
Reline::HISTORY.instance_variable_set(:@config, @config)
Reline::HISTORY.clear
@encoding = Reline.core.encoding
@line_editor = Reline::LineEditor.new(@config, @encoding)
@line_editor.reset(@prompt, encoding: @encoding)
@line_editor = Reline::LineEditor.new(@config)
@line_editor.reset(@prompt)
end

def teardown
Expand Down
4 changes: 2 additions & 2 deletions test/reline/test_key_actor_vi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ def setup
set editing-mode vi
LINES
@encoding = Reline.core.encoding
@line_editor = Reline::LineEditor.new(@config, @encoding)
@line_editor.reset(@prompt, encoding: @encoding)
@line_editor = Reline::LineEditor.new(@config)
@line_editor.reset(@prompt)
end

def editing_mode_label
Expand Down
2 changes: 1 addition & 1 deletion test/reline/test_line_editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def erase_after_cursor

def setup
verbose, $VERBOSE = $VERBOSE, nil
@line_editor = Reline::LineEditor.new(nil, Encoding::UTF_8)
@line_editor = Reline::LineEditor.new(nil)
@original_iogate = Reline::IOGate
@output = StringIO.new
@line_editor.instance_variable_set(:@screen_size, [24, 80])
Expand Down
2 changes: 1 addition & 1 deletion test/reline/test_macro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def setup
Reline.send(:test_mode)
@config = Reline::Config.new
@encoding = Reline.core.encoding
@line_editor = Reline::LineEditor.new(@config, @encoding)
@line_editor = Reline::LineEditor.new(@config)
@output = @line_editor.output = File.open(IO::NULL, "w")
end

Expand Down
5 changes: 2 additions & 3 deletions test/reline/test_string_processing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ def setup
@prompt = '> '
@config = Reline::Config.new
Reline::HISTORY.instance_variable_set(:@config, @config)
@encoding = Reline.core.encoding
@line_editor = Reline::LineEditor.new(@config, @encoding)
@line_editor.reset(@prompt, encoding: @encoding)
@line_editor = Reline::LineEditor.new(@config)
@line_editor.reset(@prompt)
end

def teardown
Expand Down
Loading