1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Backport io-console 0.5.7 to Ruby 3.0 (#4252)

* [ruby/io-console] [DOC] Note that IO#getpass returns a chomped string

IO#getpass uses String#chomp! on the read input line.

1e98c93bc8

* [ruby/io-console] Ignore chomp! result and return the modified string

09e5ccc729

* [ruby/io-console] Pre-define chomp! ID

028e1c9497

* [ruby/io-console] Shrink struct query_args

720be0a3e5

* [ruby/io-console] bump up to 0.5.7

f55d7ebff6

Co-authored-by: Marcus Stollsteimer <sto.mar@web.de>
Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
This commit is contained in:
Jeremy Evans 2021-03-13 00:55:49 -08:00 committed by GitHub
parent 6f9e007729
commit b20e2c3f2a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 5 deletions

View file

@ -77,7 +77,7 @@ getattr(int fd, conmode *t)
static ID id_getc, id_console, id_close, id_min, id_time, id_intr;
#if ENABLE_IO_GETPASS
static ID id_gets;
static ID id_gets, id_chomp_bang;
#endif
#ifdef HAVE_RB_SCHEDULER_TIMEOUT
@ -1223,8 +1223,8 @@ console_key_pressed_p(VALUE io, VALUE k)
}
#else
struct query_args {
const char *qstr;
int opt;
char qstr[6];
unsigned char opt;
};
static int
@ -1562,7 +1562,7 @@ static VALUE
str_chomp(VALUE str)
{
if (!NIL_P(str)) {
str = rb_funcallv(str, rb_intern("chomp!"), 0, 0);
rb_funcallv(str, id_chomp_bang, 0, 0);
}
return str;
}
@ -1574,6 +1574,10 @@ str_chomp(VALUE str)
* Reads and returns a line without echo back.
* Prints +prompt+ unless it is +nil+.
*
* The newline character that terminates the
* read line is removed from the returned string,
* see String#chomp!.
*
* You must require 'io/console' to use this method.
*/
static VALUE
@ -1618,6 +1622,7 @@ Init_console(void)
id_getc = rb_intern("getc");
#if ENABLE_IO_GETPASS
id_gets = rb_intern("gets");
id_chomp_bang = rb_intern("chomp!");
#endif
id_console = rb_intern("console");
id_close = rb_intern("close");

View file

@ -1,5 +1,5 @@
# -*- ruby -*-
_VERSION = "0.5.6"
_VERSION = "0.5.7"
Gem::Specification.new do |s|
s.name = "io-console"

View file

@ -235,6 +235,15 @@ defined?(PTY) and defined?(IO.console) and TestIO_Console.class_eval do
assert_equal("\r\n", r.gets)
assert_equal("\"asdf\"", r.gets.chomp)
end
run_pty("p IO.console.getpass('> ')") do |r, w|
assert_equal("> ", r.readpartial(10))
sleep 0.1
w.print "asdf\C-D\C-D"
sleep 0.1
assert_equal("\r\n", r.gets)
assert_equal("\"asdf\"", r.gets.chomp)
end
end
def test_iflush