@@ -630,6 +630,43 @@ console_getch(int argc, VALUE *argv, VALUE io)
630630#endif
631631}
632632
633+ /*
634+ * call-seq:
635+ * io.input_pending? -> true or false
636+ *
637+ * Returns whether input can be read without blocking.
638+ *
639+ * You must require 'io/console' to use this method.
640+ */
641+ static VALUE
642+ console_input_pending_p (VALUE io )
643+ {
644+ rb_io_t * fptr ;
645+
646+ GetOpenFile (io , fptr );
647+ if (rb_io_read_pending (fptr )) return Qtrue ;
648+ #ifdef _WIN32
649+ {
650+ DWORD mode ;
651+ HANDLE h = (HANDLE )rb_w32_get_osfhandle (GetReadFD (io ));
652+
653+ if (GetConsoleMode (h , & mode )) return _kbhit () ? Qtrue : Qfalse ;
654+ }
655+ #endif
656+ #if defined HAVE_RB_IO_WAIT
657+ return RTEST (rb_io_wait (io , RB_INT2NUM (RUBY_IO_READABLE ), INT2FIX (0 ))) ? Qtrue : Qfalse ;
658+ #else
659+ {
660+ struct timeval timeout = {0 , 0 };
661+ int result ;
662+
663+ result = rb_wait_for_single_fd (fptr -> fd , RB_WAITFD_IN , & timeout );
664+ if (result < 0 ) sys_fail (io );
665+ return (result & RB_WAITFD_IN ) ? Qtrue : Qfalse ;
666+ }
667+ #endif
668+ }
669+
633670/*
634671 * call-seq:
635672 * io.noecho {|io| }
@@ -2306,6 +2343,7 @@ InitVM_console(void)
23062343 rb_define_method (rb_cIO , "cooked" , console_cooked , 0 );
23072344 rb_define_method (rb_cIO , "cooked!" , console_set_cooked , 0 );
23082345 rb_define_method (rb_cIO , "getch" , console_getch , -1 );
2346+ rb_define_method (rb_cIO , "input_pending?" , console_input_pending_p , 0 );
23092347 rb_define_method (rb_cIO , "echo=" , console_set_echo , 1 );
23102348 rb_define_method (rb_cIO , "echo?" , console_echo_p , 0 );
23112349 rb_define_method (rb_cIO , "console_mode" , console_conmode_get , 0 );
0 commit comments