Skip to content

Commit

Permalink
Remove instance variable @first_char (#717)
Browse files Browse the repository at this point in the history
When Reline reads EOF, Reline.readline should return nil if and only if input is empty
  • Loading branch information
tompng authored Jun 6, 2024
1 parent 64deec1 commit cc74b36
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
15 changes: 7 additions & 8 deletions lib/reline/line_editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ def reset_variables(prompt = '', encoding:)
@perfect_matched = nil
@menu_info = nil
@searching_prompt = nil
@first_char = true
@just_cursor_moving = false
@eof = false
@continuous_insertion_buffer = String.new(encoding: @encoding)
Expand Down Expand Up @@ -1110,13 +1109,10 @@ def input_key(key)
end
if key.char.nil?
process_insert(force: true)
if @first_char
@eof = true
end
@eof = buffer_empty?
finish
return
end
@first_char = false
@completion_occurs = false

if key.char.is_a?(Symbol)
Expand Down Expand Up @@ -1409,6 +1405,10 @@ def whole_buffer
whole_lines.join("\n")
end

private def buffer_empty?
current_line.empty? and @buffer_of_lines.size == 1
end

def finished?
@finished
end
Expand Down Expand Up @@ -1937,7 +1937,7 @@ def finish
alias_method :kill_whole_line, :em_kill_line

private def em_delete(key)
if current_line.empty? and @buffer_of_lines.size == 1 and key == "\C-d".ord
if buffer_empty? and key == "\C-d".ord
@eof = true
finish
elsif @byte_pointer < current_line.bytesize
Expand Down Expand Up @@ -2285,8 +2285,7 @@ def finish
end

private def vi_list_or_eof(key)
if current_line.empty? and @buffer_of_lines.size == 1
set_current_line('', 0)
if buffer_empty?
@eof = true
finish
else
Expand Down
24 changes: 24 additions & 0 deletions test/reline/test_reline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,30 @@ def test_print_prompt_before_everything_else
assert_match(/\AReline::ANSI\nprompt> /, out)
end

def test_read_eof_returns_input
pend if win?
lib = File.expand_path("../../lib", __dir__)
code = "p result: Reline.readline"
out = IO.popen([Reline.test_rubybin, "-I#{lib}", "-rreline", "-e", code], "r+") do |io|
io.write "a\C-a"
io.close_write
io.read
end
assert_include(out, '{:result=>"a"}')
end

def test_read_eof_returns_nil_if_empty
pend if win?
lib = File.expand_path("../../lib", __dir__)
code = "p result: Reline.readline"
out = IO.popen([Reline.test_rubybin, "-I#{lib}", "-rreline", "-e", code], "r+") do |io|
io.write "a\C-h"
io.close_write
io.read
end
assert_include(out, '{:result=>nil}')
end

def test_require_reline_should_not_trigger_winsize
pend if win?
lib = File.expand_path("../../lib", __dir__)
Expand Down

0 comments on commit cc74b36

Please sign in to comment.