summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbinoam P. Marques Jr <abinoam@gmail.com>2023-01-07 07:42:08 -0300
committerGitHub <noreply@github.com>2023-01-07 07:42:08 -0300
commitb617304aef659d778bd0624e76640f4eaece9bda (patch)
tree98ed94130b312474c4686e1853708267409633cd
parent8e5f773e9e4180f30f850a6ec364a082b8bc8f3f (diff)
parent8caf3eb173c125355906fe4904d274f178abf59f (diff)
downloadhighline-b617304aef659d778bd0624e76640f4eaece9bda.tar.gz
Merge pull request #260 from abinoam/issue_43
Fix #43 - Ctrl-U (erase line) handling
-rw-r--r--lib/highline.rb3
-rwxr-xr-xtest/test_highline.rb38
2 files changed, 41 insertions, 0 deletions
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
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