From f7ce562d6c89a102d5b29c213dd178d9ea02b2e4 Mon Sep 17 00:00:00 2001 From: Piotr Murach Date: Wed, 23 Dec 2020 12:17:29 +0100 Subject: [PATCH] Change to finish input with a newline when enter is pressed inside the line --- lib/tty/reader.rb | 2 +- spec/unit/read_line_spec.rb | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/tty/reader.rb b/lib/tty/reader.rb index fa0a8a1..df899fb 100644 --- a/lib/tty/reader.rb +++ b/lib/tty/reader.rb @@ -287,7 +287,7 @@ def read_line(prompt = "", value: "", echo: true, raw: true, nonblock: false) elsif console.keys[char] == :end line.move_to_end else - if raw && code == CARRIAGE_RETURN + if raw && [CARRIAGE_RETURN, NEWLINE].include?(code) char = "\n" line.move_to_end end diff --git a/spec/unit/read_line_spec.rb b/spec/unit/read_line_spec.rb index c7d4b15..ae3314f 100644 --- a/spec/unit/read_line_spec.rb +++ b/spec/unit/read_line_spec.rb @@ -85,6 +85,15 @@ expect(answer).to eq("acc\n") end + it "finishes input with enter pressed inside the line" do + input << "aaa" << "\e[D" << "\e[D" << "\n" + input.rewind + + answer = reader.read_line + + expect(answer).to eq("aaa\n") + end + it "reads multibyte line" do input << "한글" input.rewind