From a5342f160708cc87bcfdc544d10d85c7bcab554d Mon Sep 17 00:00:00 2001 From: "Abinoam P. Marques Jr" Date: Fri, 6 Jan 2023 21:37:03 -0300 Subject: Fix #43 - Handle erase line correctly (CTRL-U) --- lib/highline.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/highline.rb b/lib/highline.rb index 47df4be..7178a84 100644 --- a/lib/highline.rb +++ b/lib/highline.rb @@ -546,6 +546,9 @@ class HighLine 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 -- cgit v1.2.1 From 8caf3eb173c125355906fe4904d274f178abf59f Mon Sep 17 00:00:00 2001 From: "Abinoam P. Marques Jr" Date: Fri, 6 Jan 2023 22:10:09 -0300 Subject: Add tests for Ctrl-U (erase line) handling --- test/test_highline.rb | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/test/test_highline.rb b/test/test_highline.rb index a5c4e26..627f52c 100755 --- a/test/test_highline.rb +++ b/test/test_highline.rb @@ -349,6 +349,44 @@ class TestHighLine < Minitest::Test 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 -- cgit v1.2.1