Skip to content

Commit f09772a

Browse files
authored
Use IO's encoding instead of Encoding.default_external (#765)
* use IO's encoding * refactoring * remove unused encoding params * (for retriggering CI) remove unused encoding params
1 parent 469a528 commit f09772a

File tree

10 files changed

+39
-37
lines changed

10 files changed

+39
-37
lines changed

lib/reline.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ def readline(prompt = '', add_hist = false)
308308
otio = io_gate.prep
309309

310310
may_req_ambiguous_char_width
311-
line_editor.reset(prompt, encoding: encoding)
311+
line_editor.reset(prompt)
312312
if multiline
313313
line_editor.multiline_on
314314
if block_given?
@@ -487,7 +487,7 @@ def self.core
487487
@core ||= Core.new { |core|
488488
core.config = Reline::Config.new
489489
core.key_stroke = Reline::KeyStroke.new(core.config)
490-
core.line_editor = Reline::LineEditor.new(core.config, core.encoding)
490+
core.line_editor = Reline::LineEditor.new(core.config)
491491

492492
core.basic_word_break_characters = " \t\n`><=;|&{("
493493
core.completer_word_break_characters = " \t\n`><=;|&{("

lib/reline/io/ansi.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def initialize
4141
end
4242

4343
def encoding
44-
Encoding.default_external
44+
@input.external_encoding || Encoding.default_external
4545
end
4646

4747
def set_default_key_bindings(config, allow_terminfo: true)

lib/reline/io/dumb.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def encoding
2121
elsif RUBY_PLATFORM =~ /mswin|mingw/
2222
Encoding::UTF_8
2323
else
24-
Encoding::default_external
24+
@input.external_encoding || Encoding::default_external
2525
end
2626
end
2727

lib/reline/line_editor.rb

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,21 @@ def lines(screen_width)
7272

7373
MINIMUM_SCROLLBAR_HEIGHT = 1
7474

75-
def initialize(config, encoding)
75+
def initialize(config)
7676
@config = config
7777
@completion_append_character = ''
7878
@screen_size = [0, 0] # Should be initialized with actual winsize in LineEditor#reset
79-
reset_variables(encoding: encoding)
79+
reset_variables
8080
end
8181

8282
def io_gate
8383
Reline::IOGate
8484
end
8585

86+
def encoding
87+
io_gate.encoding
88+
end
89+
8690
def set_pasting_state(in_pasting)
8791
# While pasting, text to be inserted is stored to @continuous_insertion_buffer.
8892
# After pasting, this buffer should be force inserted.
@@ -136,9 +140,9 @@ def set_pasting_state(in_pasting)
136140
end
137141
end
138142

139-
def reset(prompt = '', encoding:)
143+
def reset(prompt = '')
140144
@screen_size = Reline::IOGate.get_screen_size
141-
reset_variables(prompt, encoding: encoding)
145+
reset_variables(prompt)
142146
@rendered_screen.base_y = Reline::IOGate.cursor_pos.y
143147
if ENV.key?('RELINE_ALT_SCROLLBAR')
144148
@full_block = '::'
@@ -150,7 +154,7 @@ def reset(prompt = '', encoding:)
150154
@upper_half_block = '▀'
151155
@lower_half_block = '▄'
152156
@block_elem_width = 1
153-
elsif @encoding == Encoding::UTF_8
157+
elsif encoding == Encoding::UTF_8
154158
@full_block = '█'
155159
@upper_half_block = '▀'
156160
@lower_half_block = '▄'
@@ -219,10 +223,9 @@ def eof?
219223
@eof
220224
end
221225

222-
def reset_variables(prompt = '', encoding:)
226+
def reset_variables(prompt = '')
223227
@prompt = prompt.gsub("\n", "\\n")
224228
@mark_pointer = nil
225-
@encoding = encoding
226229
@is_multiline = false
227230
@finished = false
228231
@history_pointer = nil
@@ -239,7 +242,7 @@ def reset_variables(prompt = '', encoding:)
239242
@searching_prompt = nil
240243
@just_cursor_moving = false
241244
@eof = false
242-
@continuous_insertion_buffer = String.new(encoding: @encoding)
245+
@continuous_insertion_buffer = String.new(encoding: encoding)
243246
@scroll_partial_screen = 0
244247
@drop_terminate_spaces = false
245248
@in_pasting = false
@@ -259,7 +262,7 @@ def reset_variables(prompt = '', encoding:)
259262

260263
def reset_line
261264
@byte_pointer = 0
262-
@buffer_of_lines = [String.new(encoding: @encoding)]
265+
@buffer_of_lines = [String.new(encoding: encoding)]
263266
@line_index = 0
264267
@cache.clear
265268
@line_backup_in_history = nil
@@ -275,7 +278,7 @@ def multiline_off
275278
end
276279

277280
private def insert_new_line(cursor_line, next_line)
278-
@buffer_of_lines.insert(@line_index + 1, String.new(next_line, encoding: @encoding))
281+
@buffer_of_lines.insert(@line_index + 1, String.new(next_line, encoding: encoding))
279282
@buffer_of_lines[@line_index] = cursor_line
280283
@line_index += 1
281284
@byte_pointer = 0
@@ -298,7 +301,7 @@ def multiline_off
298301
end
299302

300303
private def split_by_width(str, max_width, offset: 0)
301-
Reline::Unicode.split_by_width(str, max_width, @encoding, offset: offset)
304+
Reline::Unicode.split_by_width(str, max_width, encoding, offset: offset)
302305
end
303306

304307
def current_byte_pointer_cursor
@@ -882,8 +885,8 @@ def editing_mode
882885
perform_completion(list, true) if @config.show_all_if_ambiguous
883886
end
884887
if not just_show_list and target < completed
885-
@buffer_of_lines[@line_index] = (preposing + completed + completion_append_character.to_s + postposing).split("\n")[@line_index] || String.new(encoding: @encoding)
886-
line_to_pointer = (preposing + completed + completion_append_character.to_s).split("\n")[@line_index] || String.new(encoding: @encoding)
888+
@buffer_of_lines[@line_index] = (preposing + completed + completion_append_character.to_s + postposing).split("\n")[@line_index] || String.new(encoding: encoding)
889+
line_to_pointer = (preposing + completed + completion_append_character.to_s).split("\n")[@line_index] || String.new(encoding: encoding)
887890
@byte_pointer = line_to_pointer.bytesize
888891
end
889892
end
@@ -1058,8 +1061,8 @@ def wrap_method_call(method_symbol, method_obj, key, with_operator = false)
10581061
private def normal_char(key)
10591062
@multibyte_buffer << key.combined_char
10601063
if @multibyte_buffer.size > 1
1061-
if @multibyte_buffer.dup.force_encoding(@encoding).valid_encoding?
1062-
process_key(@multibyte_buffer.dup.force_encoding(@encoding), nil)
1064+
if @multibyte_buffer.dup.force_encoding(encoding).valid_encoding?
1065+
process_key(@multibyte_buffer.dup.force_encoding(encoding), nil)
10631066
@multibyte_buffer.clear
10641067
else
10651068
# invalid
@@ -1317,7 +1320,7 @@ def retrieve_completion_block(set_completion_quote_character = false)
13171320
if (lines.size - 1) > @line_index
13181321
postposing = postposing + "\n" + lines[(@line_index + 1)..-1].join("\n")
13191322
end
1320-
[preposing.encode(@encoding), target.encode(@encoding), postposing.encode(@encoding)]
1323+
[preposing.encode(encoding), target.encode(encoding), postposing.encode(encoding)]
13211324
end
13221325

13231326
def confirm_multiline_termination
@@ -1329,7 +1332,7 @@ def insert_multiline_text(text)
13291332
save_old_buffer
13301333
pre = @buffer_of_lines[@line_index].byteslice(0, @byte_pointer)
13311334
post = @buffer_of_lines[@line_index].byteslice(@byte_pointer..)
1332-
lines = (pre + Reline::Unicode.safe_encode(text, @encoding).gsub(/\r\n?/, "\n") + post).split("\n", -1)
1335+
lines = (pre + Reline::Unicode.safe_encode(text, encoding).gsub(/\r\n?/, "\n") + post).split("\n", -1)
13331336
lines << '' if lines.empty?
13341337
@buffer_of_lines[@line_index, 1] = lines
13351338
@line_index += lines.size - 1
@@ -1374,7 +1377,7 @@ def delete_text(start = nil, length = nil)
13741377
last += current_line.bytesize if last < 0
13751378
first += current_line.bytesize if first < 0
13761379
range = range.exclude_end? ? first...last : first..last
1377-
line = current_line.bytes.reject.with_index{ |c, i| range.include?(i) }.map{ |c| c.chr(Encoding::ASCII_8BIT) }.join.force_encoding(@encoding)
1380+
line = current_line.bytes.reject.with_index{ |c, i| range.include?(i) }.map{ |c| c.chr(Encoding::ASCII_8BIT) }.join.force_encoding(encoding)
13781381
set_current_line(line)
13791382
else
13801383
set_current_line(current_line.byteslice(0, start))
@@ -1585,7 +1588,7 @@ def finish
15851588
alias_method :end_of_line, :ed_move_to_end
15861589

15871590
private def generate_searcher(search_key)
1588-
search_word = String.new(encoding: @encoding)
1591+
search_word = String.new(encoding: encoding)
15891592
multibyte_buf = String.new(encoding: 'ASCII-8BIT')
15901593
hit_pointer = nil
15911594
lambda do |key|
@@ -1602,8 +1605,8 @@ def finish
16021605
search_key = key
16031606
else
16041607
multibyte_buf << key
1605-
if multibyte_buf.dup.force_encoding(@encoding).valid_encoding?
1606-
search_word << multibyte_buf.dup.force_encoding(@encoding)
1608+
if multibyte_buf.dup.force_encoding(encoding).valid_encoding?
1609+
search_word << multibyte_buf.dup.force_encoding(encoding)
16071610
multibyte_buf.clear
16081611
end
16091612
end
@@ -1763,7 +1766,7 @@ def finish
17631766
@history_pointer = history_pointer
17641767
end
17651768
@buffer_of_lines = buf.split("\n")
1766-
@buffer_of_lines = [String.new(encoding: @encoding)] if @buffer_of_lines.empty?
1769+
@buffer_of_lines = [String.new(encoding: encoding)] if @buffer_of_lines.empty?
17671770
@line_index = line == :start ? 0 : line == :end ? @buffer_of_lines.size - 1 : line
17681771
@byte_pointer = cursor == :start ? 0 : cursor == :end ? current_line.bytesize : cursor
17691772
end
@@ -2288,7 +2291,7 @@ def finish
22882291
}
22892292
system("#{ENV['EDITOR']} #{path}")
22902293
@buffer_of_lines = File.read(path).split("\n")
2291-
@buffer_of_lines = [String.new(encoding: @encoding)] if @buffer_of_lines.empty?
2294+
@buffer_of_lines = [String.new(encoding: encoding)] if @buffer_of_lines.empty?
22922295
@line_index = 0
22932296
finish
22942297
end

test/reline/helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class Reline::TestCase < Test::Unit::TestCase
9292
if Reline::Unicode::EscapedChars.include?(c.ord)
9393
c
9494
else
95-
c.encode(@line_editor.instance_variable_get(:@encoding), Encoding::UTF_8, **options)
95+
c.encode(@line_editor.encoding, Encoding::UTF_8, **options)
9696
end
9797
}.join
9898
rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError

test/reline/test_key_actor_emacs.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ def setup
99
Reline::HISTORY.instance_variable_set(:@config, @config)
1010
Reline::HISTORY.clear
1111
@encoding = Reline.core.encoding
12-
@line_editor = Reline::LineEditor.new(@config, @encoding)
13-
@line_editor.reset(@prompt, encoding: @encoding)
12+
@line_editor = Reline::LineEditor.new(@config)
13+
@line_editor.reset(@prompt)
1414
end
1515

1616
def teardown

test/reline/test_key_actor_vi.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ def setup
99
set editing-mode vi
1010
LINES
1111
@encoding = Reline.core.encoding
12-
@line_editor = Reline::LineEditor.new(@config, @encoding)
13-
@line_editor.reset(@prompt, encoding: @encoding)
12+
@line_editor = Reline::LineEditor.new(@config)
13+
@line_editor.reset(@prompt)
1414
end
1515

1616
def editing_mode_label

test/reline/test_line_editor.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def erase_after_cursor
1616

1717
def setup
1818
verbose, $VERBOSE = $VERBOSE, nil
19-
@line_editor = Reline::LineEditor.new(nil, Encoding::UTF_8)
19+
@line_editor = Reline::LineEditor.new(nil)
2020
@original_iogate = Reline::IOGate
2121
@output = StringIO.new
2222
@line_editor.instance_variable_set(:@screen_size, [24, 80])

test/reline/test_macro.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ def setup
55
Reline.send(:test_mode)
66
@config = Reline::Config.new
77
@encoding = Reline.core.encoding
8-
@line_editor = Reline::LineEditor.new(@config, @encoding)
8+
@line_editor = Reline::LineEditor.new(@config)
99
@output = @line_editor.output = File.open(IO::NULL, "w")
1010
end
1111

test/reline/test_string_processing.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ def setup
66
@prompt = '> '
77
@config = Reline::Config.new
88
Reline::HISTORY.instance_variable_set(:@config, @config)
9-
@encoding = Reline.core.encoding
10-
@line_editor = Reline::LineEditor.new(@config, @encoding)
11-
@line_editor.reset(@prompt, encoding: @encoding)
9+
@line_editor = Reline::LineEditor.new(@config)
10+
@line_editor.reset(@prompt)
1211
end
1312

1413
def teardown

0 commit comments

Comments
 (0)