Skip to content

Commit

Permalink
Merge pull request #260 from abinoam/issue_43
Browse files Browse the repository at this point in the history
Fix #43 - Ctrl-U (erase line) handling
  • Loading branch information
abinoam committed Jan 7, 2023
2 parents 8e5f773 + 8caf3eb commit b617304
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/highline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,9 @@ def get_line_raw_no_echo_mode(question)
if character == "\b" || character == "\u007F"
chopped = line.chop!
output_erase_char if chopped && question.echo
elsif character == "\cU"
line.size.times { output_erase_char } if question.echo
line = ""
elsif character == "\e"
ignore_arrow_key
else
Expand Down
38 changes: 38 additions & 0 deletions test/test_highline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,44 @@ def test_after_some_chars_backspace_does_not_enter_prompt_when_utf8
assert_equal(4, @output.string.count("\b"))
end

def test_erase_line_does_not_enter_prompt
@input << "\cU\cU\cU\cU\cU\cU\cU\cU\cU"
@input.rewind
answer = @terminal.ask("Please enter your password: ") do |q|
q.echo = "*"
end
assert_equal("", answer)
assert_equal("Please enter your password: \n", @output.string)

# There's no backspaces as the line is already empty
assert_equal(0, @output.string.count("\b"))
end

def test_after_some_chars_erase_line_does_not_enter_prompt_when_ascii
@input << "apple\cU\cU\cU\cU\cU\cU\cU\cU"
@input.rewind
answer = @terminal.ask("Please enter your password: ") do |q|
q.echo = "*"
end
assert_equal("", answer)

# There's only enough backspaces to clear the given string
assert_equal(5, @output.string.count("\b"))
end


def test_after_some_chars_erase_line_does_not_enter_prompt_when_utf8
@input << "maçã\cU\cU\cU\cU\cU\cU\cU\cU"
@input.rewind
answer = @terminal.ask("Please enter your password: ") do |q|
q.echo = "*"
end
assert_equal("", answer)

# There's only enough backspaces to clear the given string
assert_equal(4, @output.string.count("\b"))
end

def test_readline_mode
#
# Rubinius (and JRuby) seems to be ignoring
Expand Down

0 comments on commit b617304

Please sign in to comment.