io-console 0.6.0 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.document +4 -0
- data/ext/io/console/console.c +314 -341
- data/ext/io/console/extconf.rb +12 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3e3b733596bf1321d1707591ae4432f9b376ca67926612396c2ea70af6623298
|
4
|
+
data.tar.gz: 4f9af1c89378179febe0bb08b50ac381e2e691cc00db4aac14b4b3b28984e924
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a59066b0ef365b48506ece88aa529c497ba3168d8e1c4448d8be5d14ce4e5068da6d47f65211caa19fdb129514105b253ebaef4f545dbf29057fad772ea950c9
|
7
|
+
data.tar.gz: '0068b743595457e23d0607635c3746f4b5cc8731bcfc0f729077b6fd2e23fa84e07024536753cc0eefeffa0a8953d3eccff627c08fe6adb8c529021840ce5d30'
|
data/.document
ADDED
data/ext/io/console/console.c
CHANGED
@@ -76,9 +76,7 @@ getattr(int fd, conmode *t)
|
|
76
76
|
#endif
|
77
77
|
|
78
78
|
static ID id_getc, id_console, id_close;
|
79
|
-
|
80
|
-
static ID id_gets, id_chomp_bang;
|
81
|
-
#endif
|
79
|
+
static ID id_gets, id_flush, id_chomp_bang;
|
82
80
|
|
83
81
|
#if defined HAVE_RUBY_FIBER_SCHEDULER_H
|
84
82
|
# include "ruby/fiber/scheduler.h"
|
@@ -87,7 +85,41 @@ extern VALUE rb_scheduler_timeout(struct timeval *timeout);
|
|
87
85
|
# define rb_fiber_scheduler_make_timeout rb_scheduler_timeout
|
88
86
|
#endif
|
89
87
|
|
90
|
-
#
|
88
|
+
#ifndef HAVE_RB_IO_DESCRIPTOR
|
89
|
+
static int
|
90
|
+
io_descriptor_fallback(VALUE io)
|
91
|
+
{
|
92
|
+
rb_io_t *fptr;
|
93
|
+
GetOpenFile(io, fptr);
|
94
|
+
return fptr->fd;
|
95
|
+
}
|
96
|
+
#define rb_io_descriptor io_descriptor_fallback
|
97
|
+
#endif
|
98
|
+
|
99
|
+
#ifndef HAVE_RB_IO_PATH
|
100
|
+
static VALUE
|
101
|
+
io_path_fallback(VALUE io)
|
102
|
+
{
|
103
|
+
rb_io_t *fptr;
|
104
|
+
GetOpenFile(io, fptr);
|
105
|
+
return fptr->pathv;
|
106
|
+
}
|
107
|
+
#define rb_io_path io_path_fallback
|
108
|
+
#endif
|
109
|
+
|
110
|
+
#ifndef HAVE_RB_IO_GET_WRITE_IO
|
111
|
+
static VALUE
|
112
|
+
io_get_write_io_fallback(VALUE io)
|
113
|
+
{
|
114
|
+
rb_io_t *fptr;
|
115
|
+
GetOpenFile(io, fptr);
|
116
|
+
VALUE wio = fptr->tied_io_for_writing;
|
117
|
+
return wio ? wio : io;
|
118
|
+
}
|
119
|
+
#define rb_io_get_write_io io_get_write_io_fallback
|
120
|
+
#endif
|
121
|
+
|
122
|
+
#define sys_fail(io) rb_sys_fail_str(rb_io_path(io))
|
91
123
|
|
92
124
|
#ifndef HAVE_RB_F_SEND
|
93
125
|
#ifndef RB_PASS_CALLED_KEYWORDS
|
@@ -293,33 +325,21 @@ set_ttymode(int fd, conmode *t, void (*setter)(conmode *, void *), void *arg)
|
|
293
325
|
return setattr(fd, &r);
|
294
326
|
}
|
295
327
|
|
296
|
-
#define GetReadFD(
|
297
|
-
|
298
|
-
static inline int
|
299
|
-
get_write_fd(const rb_io_t *fptr)
|
300
|
-
{
|
301
|
-
VALUE wio = fptr->tied_io_for_writing;
|
302
|
-
rb_io_t *ofptr;
|
303
|
-
if (!wio) return fptr->fd;
|
304
|
-
GetOpenFile(wio, ofptr);
|
305
|
-
return ofptr->fd;
|
306
|
-
}
|
307
|
-
#define GetWriteFD(fptr) get_write_fd(fptr)
|
328
|
+
#define GetReadFD(io) rb_io_descriptor(io)
|
329
|
+
#define GetWriteFD(io) rb_io_descriptor(rb_io_get_write_io(io))
|
308
330
|
|
309
331
|
#define FD_PER_IO 2
|
310
332
|
|
311
333
|
static VALUE
|
312
334
|
ttymode(VALUE io, VALUE (*func)(VALUE), VALUE farg, void (*setter)(conmode *, void *), void *arg)
|
313
335
|
{
|
314
|
-
rb_io_t *fptr;
|
315
336
|
int status = -1;
|
316
337
|
int error = 0;
|
317
338
|
int fd[FD_PER_IO];
|
318
339
|
conmode t[FD_PER_IO];
|
319
340
|
VALUE result = Qnil;
|
320
341
|
|
321
|
-
|
322
|
-
fd[0] = GetReadFD(fptr);
|
342
|
+
fd[0] = GetReadFD(io);
|
323
343
|
if (fd[0] != -1) {
|
324
344
|
if (set_ttymode(fd[0], t+0, setter, arg)) {
|
325
345
|
status = 0;
|
@@ -329,7 +349,7 @@ ttymode(VALUE io, VALUE (*func)(VALUE), VALUE farg, void (*setter)(conmode *, vo
|
|
329
349
|
fd[0] = -1;
|
330
350
|
}
|
331
351
|
}
|
332
|
-
fd[1] = GetWriteFD(
|
352
|
+
fd[1] = GetWriteFD(io);
|
333
353
|
if (fd[1] != -1 && fd[1] != fd[0]) {
|
334
354
|
if (set_ttymode(fd[1], t+1, setter, arg)) {
|
335
355
|
status = 0;
|
@@ -342,14 +362,13 @@ ttymode(VALUE io, VALUE (*func)(VALUE), VALUE farg, void (*setter)(conmode *, vo
|
|
342
362
|
if (status == 0) {
|
343
363
|
result = rb_protect(func, farg, &status);
|
344
364
|
}
|
345
|
-
|
346
|
-
if (fd[0] != -1 && fd[0] == GetReadFD(fptr)) {
|
365
|
+
if (fd[0] != -1 && fd[0] == GetReadFD(io)) {
|
347
366
|
if (!setattr(fd[0], t+0)) {
|
348
367
|
error = errno;
|
349
368
|
status = -1;
|
350
369
|
}
|
351
370
|
}
|
352
|
-
if (fd[1] != -1 && fd[1] != fd[0] && fd[1] == GetWriteFD(
|
371
|
+
if (fd[1] != -1 && fd[1] != fd[0] && fd[1] == GetWriteFD(io)) {
|
353
372
|
if (!setattr(fd[1], t+1)) {
|
354
373
|
error = errno;
|
355
374
|
status = -1;
|
@@ -435,15 +454,11 @@ static VALUE
|
|
435
454
|
console_set_raw(int argc, VALUE *argv, VALUE io)
|
436
455
|
{
|
437
456
|
conmode t;
|
438
|
-
rb_io_t *fptr;
|
439
|
-
int fd;
|
440
457
|
rawmode_arg_t opts, *optp = rawmode_opt(&argc, argv, 0, 0, &opts);
|
441
|
-
|
442
|
-
|
443
|
-
fd = GetReadFD(fptr);
|
444
|
-
if (!getattr(fd, &t)) sys_fail_fptr(fptr);
|
458
|
+
int fd = GetReadFD(io);
|
459
|
+
if (!getattr(fd, &t)) sys_fail(io);
|
445
460
|
set_rawmode(&t, optp);
|
446
|
-
if (!setattr(fd, &t))
|
461
|
+
if (!setattr(fd, &t)) sys_fail(io);
|
447
462
|
return io;
|
448
463
|
}
|
449
464
|
|
@@ -479,14 +494,10 @@ static VALUE
|
|
479
494
|
console_set_cooked(VALUE io)
|
480
495
|
{
|
481
496
|
conmode t;
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
GetOpenFile(io, fptr);
|
486
|
-
fd = GetReadFD(fptr);
|
487
|
-
if (!getattr(fd, &t)) sys_fail_fptr(fptr);
|
497
|
+
int fd = GetReadFD(io);
|
498
|
+
if (!getattr(fd, &t)) sys_fail(io);
|
488
499
|
set_cookedmode(&t, NULL);
|
489
|
-
if (!setattr(fd, &t))
|
500
|
+
if (!setattr(fd, &t)) sys_fail(io);
|
490
501
|
return io;
|
491
502
|
}
|
492
503
|
|
@@ -638,17 +649,17 @@ static VALUE
|
|
638
649
|
console_set_echo(VALUE io, VALUE f)
|
639
650
|
{
|
640
651
|
conmode t;
|
641
|
-
|
642
|
-
|
652
|
+
int fd = GetReadFD(io);
|
653
|
+
|
654
|
+
if (!getattr(fd, &t)) sys_fail(io);
|
643
655
|
|
644
|
-
GetOpenFile(io, fptr);
|
645
|
-
fd = GetReadFD(fptr);
|
646
|
-
if (!getattr(fd, &t)) sys_fail_fptr(fptr);
|
647
656
|
if (RTEST(f))
|
648
|
-
|
657
|
+
set_echo(&t, NULL);
|
649
658
|
else
|
650
|
-
|
651
|
-
|
659
|
+
set_noecho(&t, NULL);
|
660
|
+
|
661
|
+
if (!setattr(fd, &t)) sys_fail(io);
|
662
|
+
|
652
663
|
return io;
|
653
664
|
}
|
654
665
|
|
@@ -664,12 +675,9 @@ static VALUE
|
|
664
675
|
console_echo_p(VALUE io)
|
665
676
|
{
|
666
677
|
conmode t;
|
667
|
-
|
668
|
-
int fd;
|
678
|
+
int fd = GetReadFD(io);
|
669
679
|
|
670
|
-
|
671
|
-
fd = GetReadFD(fptr);
|
672
|
-
if (!getattr(fd, &t)) sys_fail_fptr(fptr);
|
680
|
+
if (!getattr(fd, &t)) sys_fail(io);
|
673
681
|
return echo_p(&t) ? Qtrue : Qfalse;
|
674
682
|
}
|
675
683
|
|
@@ -748,12 +756,9 @@ static VALUE
|
|
748
756
|
console_conmode_get(VALUE io)
|
749
757
|
{
|
750
758
|
conmode t;
|
751
|
-
|
752
|
-
int fd;
|
759
|
+
int fd = GetReadFD(io);
|
753
760
|
|
754
|
-
|
755
|
-
fd = GetReadFD(fptr);
|
756
|
-
if (!getattr(fd, &t)) sys_fail_fptr(fptr);
|
761
|
+
if (!getattr(fd, &t)) sys_fail(io);
|
757
762
|
|
758
763
|
return conmode_new(cConmode, &t);
|
759
764
|
}
|
@@ -770,14 +775,12 @@ static VALUE
|
|
770
775
|
console_conmode_set(VALUE io, VALUE mode)
|
771
776
|
{
|
772
777
|
conmode *t, r;
|
773
|
-
|
774
|
-
int fd;
|
778
|
+
int fd = GetReadFD(io);
|
775
779
|
|
776
780
|
TypedData_Get_Struct(mode, conmode, &conmode_type, t);
|
777
781
|
r = *t;
|
778
|
-
|
779
|
-
fd
|
780
|
-
if (!setattr(fd, &r)) sys_fail_fptr(fptr);
|
782
|
+
|
783
|
+
if (!setattr(fd, &r)) sys_fail(io);
|
781
784
|
|
782
785
|
return mode;
|
783
786
|
}
|
@@ -813,13 +816,9 @@ typedef CONSOLE_SCREEN_BUFFER_INFO rb_console_size_t;
|
|
813
816
|
static VALUE
|
814
817
|
console_winsize(VALUE io)
|
815
818
|
{
|
816
|
-
rb_io_t *fptr;
|
817
|
-
int fd;
|
818
819
|
rb_console_size_t ws;
|
819
|
-
|
820
|
-
|
821
|
-
fd = GetWriteFD(fptr);
|
822
|
-
if (!getwinsize(fd, &ws)) sys_fail_fptr(fptr);
|
820
|
+
int fd = GetWriteFD(io);
|
821
|
+
if (!getwinsize(fd, &ws)) sys_fail(io);
|
823
822
|
return rb_assoc_new(INT2NUM(winsize_row(&ws)), INT2NUM(winsize_col(&ws)));
|
824
823
|
}
|
825
824
|
|
@@ -835,7 +834,6 @@ console_winsize(VALUE io)
|
|
835
834
|
static VALUE
|
836
835
|
console_set_winsize(VALUE io, VALUE size)
|
837
836
|
{
|
838
|
-
rb_io_t *fptr;
|
839
837
|
rb_console_size_t ws;
|
840
838
|
#if defined _WIN32
|
841
839
|
HANDLE wh;
|
@@ -844,20 +842,17 @@ console_set_winsize(VALUE io, VALUE size)
|
|
844
842
|
#endif
|
845
843
|
VALUE row, col, xpixel, ypixel;
|
846
844
|
const VALUE *sz;
|
847
|
-
int fd;
|
848
845
|
long sizelen;
|
846
|
+
int fd;
|
849
847
|
|
850
|
-
GetOpenFile(io, fptr);
|
851
848
|
size = rb_Array(size);
|
852
849
|
if ((sizelen = RARRAY_LEN(size)) != 2 && sizelen != 4) {
|
853
|
-
|
854
|
-
"wrong number of arguments (given %ld, expected 2 or 4)",
|
855
|
-
sizelen);
|
850
|
+
rb_raise(rb_eArgError, "wrong number of arguments (given %ld, expected 2 or 4)", sizelen);
|
856
851
|
}
|
857
852
|
sz = RARRAY_CONST_PTR(size);
|
858
853
|
row = sz[0], col = sz[1], xpixel = ypixel = Qnil;
|
859
854
|
if (sizelen == 4) xpixel = sz[2], ypixel = sz[3];
|
860
|
-
fd = GetWriteFD(
|
855
|
+
fd = GetWriteFD(io);
|
861
856
|
#if defined TIOCSWINSZ
|
862
857
|
ws.ws_row = ws.ws_col = ws.ws_xpixel = ws.ws_ypixel = 0;
|
863
858
|
#define SET(m) ws.ws_##m = NIL_P(m) ? 0 : (unsigned short)NUM2UINT(m)
|
@@ -866,7 +861,7 @@ console_set_winsize(VALUE io, VALUE size)
|
|
866
861
|
SET(xpixel);
|
867
862
|
SET(ypixel);
|
868
863
|
#undef SET
|
869
|
-
if (!setwinsize(fd, &ws))
|
864
|
+
if (!setwinsize(fd, &ws)) sys_fail(io);
|
870
865
|
#elif defined _WIN32
|
871
866
|
wh = (HANDLE)rb_w32_get_osfhandle(fd);
|
872
867
|
#define SET(m) new##m = NIL_P(m) ? 0 : (unsigned short)NUM2UINT(m)
|
@@ -904,12 +899,10 @@ console_set_winsize(VALUE io, VALUE size)
|
|
904
899
|
static VALUE
|
905
900
|
console_check_winsize_changed(VALUE io)
|
906
901
|
{
|
907
|
-
rb_io_t *fptr;
|
908
902
|
HANDLE h;
|
909
903
|
DWORD num;
|
910
904
|
|
911
|
-
|
912
|
-
h = (HANDLE)rb_w32_get_osfhandle(GetReadFD(fptr));
|
905
|
+
h = (HANDLE)rb_w32_get_osfhandle(GetReadFD(io));
|
913
906
|
while (GetNumberOfConsoleInputEvents(h, &num) && num > 0) {
|
914
907
|
INPUT_RECORD rec;
|
915
908
|
if (ReadConsoleInput(h, &rec, 1, &num)) {
|
@@ -935,15 +928,11 @@ console_check_winsize_changed(VALUE io)
|
|
935
928
|
static VALUE
|
936
929
|
console_iflush(VALUE io)
|
937
930
|
{
|
938
|
-
rb_io_t *fptr;
|
939
|
-
int fd;
|
940
|
-
|
941
|
-
GetOpenFile(io, fptr);
|
942
|
-
fd = GetReadFD(fptr);
|
943
931
|
#if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
|
944
|
-
|
932
|
+
int fd = GetReadFD(io);
|
933
|
+
if (tcflush(fd, TCIFLUSH)) sys_fail(io);
|
945
934
|
#endif
|
946
|
-
|
935
|
+
|
947
936
|
return io;
|
948
937
|
}
|
949
938
|
|
@@ -958,13 +947,9 @@ console_iflush(VALUE io)
|
|
958
947
|
static VALUE
|
959
948
|
console_oflush(VALUE io)
|
960
949
|
{
|
961
|
-
|
962
|
-
int fd;
|
963
|
-
|
964
|
-
GetOpenFile(io, fptr);
|
965
|
-
fd = GetWriteFD(fptr);
|
950
|
+
int fd = GetWriteFD(io);
|
966
951
|
#if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
|
967
|
-
if (tcflush(fd, TCOFLUSH))
|
952
|
+
if (tcflush(fd, TCOFLUSH)) sys_fail(io);
|
968
953
|
#endif
|
969
954
|
(void)fd;
|
970
955
|
return io;
|
@@ -981,40 +966,30 @@ console_oflush(VALUE io)
|
|
981
966
|
static VALUE
|
982
967
|
console_ioflush(VALUE io)
|
983
968
|
{
|
984
|
-
rb_io_t *fptr;
|
985
969
|
#if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
|
986
|
-
int fd1
|
987
|
-
|
970
|
+
int fd1 = GetReadFD(io);
|
971
|
+
int fd2 = GetWriteFD(io);
|
988
972
|
|
989
|
-
GetOpenFile(io, fptr);
|
990
|
-
#if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
|
991
|
-
fd1 = GetReadFD(fptr);
|
992
|
-
fd2 = GetWriteFD(fptr);
|
993
973
|
if (fd2 != -1 && fd1 != fd2) {
|
994
|
-
|
995
|
-
|
974
|
+
if (tcflush(fd1, TCIFLUSH)) sys_fail(io);
|
975
|
+
if (tcflush(fd2, TCOFLUSH)) sys_fail(io);
|
996
976
|
}
|
997
977
|
else {
|
998
|
-
|
978
|
+
if (tcflush(fd1, TCIOFLUSH)) sys_fail(io);
|
999
979
|
}
|
1000
980
|
#endif
|
981
|
+
|
1001
982
|
return io;
|
1002
983
|
}
|
1003
984
|
|
1004
985
|
static VALUE
|
1005
986
|
console_beep(VALUE io)
|
1006
987
|
{
|
1007
|
-
rb_io_t *fptr;
|
1008
|
-
int fd;
|
1009
|
-
|
1010
|
-
GetOpenFile(io, fptr);
|
1011
|
-
fd = GetWriteFD(fptr);
|
1012
988
|
#ifdef _WIN32
|
1013
|
-
(void)fd;
|
1014
989
|
MessageBeep(0);
|
1015
990
|
#else
|
1016
|
-
|
1017
|
-
|
991
|
+
int fd = GetWriteFD(io);
|
992
|
+
if (write(fd, "\a", 1) < 0) sys_fail(io);
|
1018
993
|
#endif
|
1019
994
|
return io;
|
1020
995
|
}
|
@@ -1035,79 +1010,6 @@ mode_in_range(VALUE val, int high, const char *modename)
|
|
1035
1010
|
}
|
1036
1011
|
|
1037
1012
|
#if defined _WIN32
|
1038
|
-
static VALUE
|
1039
|
-
console_goto(VALUE io, VALUE y, VALUE x)
|
1040
|
-
{
|
1041
|
-
rb_io_t *fptr;
|
1042
|
-
int fd;
|
1043
|
-
COORD pos;
|
1044
|
-
|
1045
|
-
GetOpenFile(io, fptr);
|
1046
|
-
fd = GetWriteFD(fptr);
|
1047
|
-
pos.X = NUM2UINT(x);
|
1048
|
-
pos.Y = NUM2UINT(y);
|
1049
|
-
if (!SetConsoleCursorPosition((HANDLE)rb_w32_get_osfhandle(fd), pos)) {
|
1050
|
-
rb_syserr_fail(LAST_ERROR, 0);
|
1051
|
-
}
|
1052
|
-
return io;
|
1053
|
-
}
|
1054
|
-
|
1055
|
-
static VALUE
|
1056
|
-
console_cursor_pos(VALUE io)
|
1057
|
-
{
|
1058
|
-
rb_io_t *fptr;
|
1059
|
-
int fd;
|
1060
|
-
rb_console_size_t ws;
|
1061
|
-
|
1062
|
-
GetOpenFile(io, fptr);
|
1063
|
-
fd = GetWriteFD(fptr);
|
1064
|
-
if (!GetConsoleScreenBufferInfo((HANDLE)rb_w32_get_osfhandle(fd), &ws)) {
|
1065
|
-
rb_syserr_fail(LAST_ERROR, 0);
|
1066
|
-
}
|
1067
|
-
return rb_assoc_new(UINT2NUM(ws.dwCursorPosition.Y), UINT2NUM(ws.dwCursorPosition.X));
|
1068
|
-
}
|
1069
|
-
|
1070
|
-
static VALUE
|
1071
|
-
console_move(VALUE io, int y, int x)
|
1072
|
-
{
|
1073
|
-
rb_io_t *fptr;
|
1074
|
-
HANDLE h;
|
1075
|
-
rb_console_size_t ws;
|
1076
|
-
COORD *pos = &ws.dwCursorPosition;
|
1077
|
-
|
1078
|
-
GetOpenFile(io, fptr);
|
1079
|
-
h = (HANDLE)rb_w32_get_osfhandle(GetWriteFD(fptr));
|
1080
|
-
if (!GetConsoleScreenBufferInfo(h, &ws)) {
|
1081
|
-
rb_syserr_fail(LAST_ERROR, 0);
|
1082
|
-
}
|
1083
|
-
pos->X += x;
|
1084
|
-
pos->Y += y;
|
1085
|
-
if (!SetConsoleCursorPosition(h, *pos)) {
|
1086
|
-
rb_syserr_fail(LAST_ERROR, 0);
|
1087
|
-
}
|
1088
|
-
return io;
|
1089
|
-
}
|
1090
|
-
|
1091
|
-
static VALUE
|
1092
|
-
console_goto_column(VALUE io, VALUE val)
|
1093
|
-
{
|
1094
|
-
rb_io_t *fptr;
|
1095
|
-
HANDLE h;
|
1096
|
-
rb_console_size_t ws;
|
1097
|
-
COORD *pos = &ws.dwCursorPosition;
|
1098
|
-
|
1099
|
-
GetOpenFile(io, fptr);
|
1100
|
-
h = (HANDLE)rb_w32_get_osfhandle(GetWriteFD(fptr));
|
1101
|
-
if (!GetConsoleScreenBufferInfo(h, &ws)) {
|
1102
|
-
rb_syserr_fail(LAST_ERROR, 0);
|
1103
|
-
}
|
1104
|
-
pos->X = NUM2INT(val);
|
1105
|
-
if (!SetConsoleCursorPosition(h, *pos)) {
|
1106
|
-
rb_syserr_fail(LAST_ERROR, 0);
|
1107
|
-
}
|
1108
|
-
return io;
|
1109
|
-
}
|
1110
|
-
|
1111
1013
|
static void
|
1112
1014
|
constat_clear(HANDLE handle, WORD attr, DWORD len, COORD pos)
|
1113
1015
|
{
|
@@ -1117,87 +1019,13 @@ constat_clear(HANDLE handle, WORD attr, DWORD len, COORD pos)
|
|
1117
1019
|
FillConsoleOutputCharacterW(handle, L' ', len, pos, &written);
|
1118
1020
|
}
|
1119
1021
|
|
1120
|
-
static VALUE
|
1121
|
-
console_erase_line(VALUE io, VALUE val)
|
1122
|
-
{
|
1123
|
-
rb_io_t *fptr;
|
1124
|
-
HANDLE h;
|
1125
|
-
rb_console_size_t ws;
|
1126
|
-
COORD *pos = &ws.dwCursorPosition;
|
1127
|
-
DWORD w;
|
1128
|
-
int mode = mode_in_range(val, 2, "line erase");
|
1129
|
-
|
1130
|
-
GetOpenFile(io, fptr);
|
1131
|
-
h = (HANDLE)rb_w32_get_osfhandle(GetWriteFD(fptr));
|
1132
|
-
if (!GetConsoleScreenBufferInfo(h, &ws)) {
|
1133
|
-
rb_syserr_fail(LAST_ERROR, 0);
|
1134
|
-
}
|
1135
|
-
w = winsize_col(&ws);
|
1136
|
-
switch (mode) {
|
1137
|
-
case 0: /* after cursor */
|
1138
|
-
w -= pos->X;
|
1139
|
-
break;
|
1140
|
-
case 1: /* before *and* cursor */
|
1141
|
-
w = pos->X + 1;
|
1142
|
-
pos->X = 0;
|
1143
|
-
break;
|
1144
|
-
case 2: /* entire line */
|
1145
|
-
pos->X = 0;
|
1146
|
-
break;
|
1147
|
-
}
|
1148
|
-
constat_clear(h, ws.wAttributes, w, *pos);
|
1149
|
-
return io;
|
1150
|
-
}
|
1151
|
-
|
1152
|
-
static VALUE
|
1153
|
-
console_erase_screen(VALUE io, VALUE val)
|
1154
|
-
{
|
1155
|
-
rb_io_t *fptr;
|
1156
|
-
HANDLE h;
|
1157
|
-
rb_console_size_t ws;
|
1158
|
-
COORD *pos = &ws.dwCursorPosition;
|
1159
|
-
DWORD w;
|
1160
|
-
int mode = mode_in_range(val, 3, "screen erase");
|
1161
|
-
|
1162
|
-
GetOpenFile(io, fptr);
|
1163
|
-
h = (HANDLE)rb_w32_get_osfhandle(GetWriteFD(fptr));
|
1164
|
-
if (!GetConsoleScreenBufferInfo(h, &ws)) {
|
1165
|
-
rb_syserr_fail(LAST_ERROR, 0);
|
1166
|
-
}
|
1167
|
-
w = winsize_col(&ws);
|
1168
|
-
switch (mode) {
|
1169
|
-
case 0: /* erase after cursor */
|
1170
|
-
w = (w * (ws.srWindow.Bottom - pos->Y + 1) - pos->X);
|
1171
|
-
break;
|
1172
|
-
case 1: /* erase before *and* cursor */
|
1173
|
-
w = (w * (pos->Y - ws.srWindow.Top) + pos->X + 1);
|
1174
|
-
pos->X = 0;
|
1175
|
-
pos->Y = ws.srWindow.Top;
|
1176
|
-
break;
|
1177
|
-
case 2: /* erase entire screen */
|
1178
|
-
w = (w * winsize_row(&ws));
|
1179
|
-
pos->X = 0;
|
1180
|
-
pos->Y = ws.srWindow.Top;
|
1181
|
-
break;
|
1182
|
-
case 3: /* erase entire screen */
|
1183
|
-
w = (w * ws.dwSize.Y);
|
1184
|
-
pos->X = 0;
|
1185
|
-
pos->Y = 0;
|
1186
|
-
break;
|
1187
|
-
}
|
1188
|
-
constat_clear(h, ws.wAttributes, w, *pos);
|
1189
|
-
return io;
|
1190
|
-
}
|
1191
|
-
|
1192
1022
|
static VALUE
|
1193
1023
|
console_scroll(VALUE io, int line)
|
1194
1024
|
{
|
1195
|
-
rb_io_t *fptr;
|
1196
1025
|
HANDLE h;
|
1197
1026
|
rb_console_size_t ws;
|
1198
1027
|
|
1199
|
-
|
1200
|
-
h = (HANDLE)rb_w32_get_osfhandle(GetWriteFD(fptr));
|
1028
|
+
h = (HANDLE)rb_w32_get_osfhandle(GetWriteFD(io));
|
1201
1029
|
if (!GetConsoleScreenBufferInfo(h, &ws)) {
|
1202
1030
|
rb_syserr_fail(LAST_ERROR, 0);
|
1203
1031
|
}
|
@@ -1256,23 +1084,11 @@ static int
|
|
1256
1084
|
direct_query(VALUE io, const struct query_args *query)
|
1257
1085
|
{
|
1258
1086
|
if (RB_TYPE_P(io, T_FILE)) {
|
1259
|
-
|
1260
|
-
|
1261
|
-
|
1262
|
-
|
1263
|
-
|
1264
|
-
VALUE s = rb_str_new_cstr(query->qstr);
|
1265
|
-
rb_io_write(wio, s);
|
1266
|
-
rb_io_flush(wio);
|
1267
|
-
return 1;
|
1268
|
-
}
|
1269
|
-
if (write(fptr->fd, query->qstr, strlen(query->qstr)) != -1) {
|
1270
|
-
return 1;
|
1271
|
-
}
|
1272
|
-
if (fptr->fd == 0 &&
|
1273
|
-
write(1, query->qstr, strlen(query->qstr)) != -1) {
|
1274
|
-
return 1;
|
1275
|
-
}
|
1087
|
+
VALUE wio = rb_io_get_write_io(io);
|
1088
|
+
VALUE s = rb_str_new_cstr(query->qstr);
|
1089
|
+
rb_io_write(wio, s);
|
1090
|
+
rb_io_flush(wio);
|
1091
|
+
return 1;
|
1276
1092
|
}
|
1277
1093
|
return 0;
|
1278
1094
|
}
|
@@ -1322,9 +1138,41 @@ console_vt_response(int argc, VALUE *argv, VALUE io, const struct query_args *qa
|
|
1322
1138
|
return ret;
|
1323
1139
|
}
|
1324
1140
|
|
1141
|
+
static VALUE
|
1142
|
+
console_scroll(VALUE io, int line)
|
1143
|
+
{
|
1144
|
+
if (line) {
|
1145
|
+
VALUE s = rb_sprintf("\x1b[%d%c", line < 0 ? -line : line,
|
1146
|
+
line < 0 ? 'T' : 'S');
|
1147
|
+
rb_io_write(io, s);
|
1148
|
+
}
|
1149
|
+
return io;
|
1150
|
+
}
|
1151
|
+
|
1152
|
+
# define console_key_pressed_p rb_f_notimplement
|
1153
|
+
#endif
|
1154
|
+
|
1155
|
+
/*
|
1156
|
+
* call-seq:
|
1157
|
+
* io.cursor -> [row, column]
|
1158
|
+
*
|
1159
|
+
* Returns the current cursor position as a two-element array of integers (row, column)
|
1160
|
+
*
|
1161
|
+
* io.cursor # => [3, 5]
|
1162
|
+
*
|
1163
|
+
* You must require 'io/console' to use this method.
|
1164
|
+
*/
|
1325
1165
|
static VALUE
|
1326
1166
|
console_cursor_pos(VALUE io)
|
1327
1167
|
{
|
1168
|
+
#ifdef _WIN32
|
1169
|
+
rb_console_size_t ws;
|
1170
|
+
int fd = GetWriteFD(io);
|
1171
|
+
if (!GetConsoleScreenBufferInfo((HANDLE)rb_w32_get_osfhandle(fd), &ws)) {
|
1172
|
+
rb_syserr_fail(LAST_ERROR, 0);
|
1173
|
+
}
|
1174
|
+
return rb_assoc_new(UINT2NUM(ws.dwCursorPosition.Y), UINT2NUM(ws.dwCursorPosition.X));
|
1175
|
+
#else
|
1328
1176
|
static const struct query_args query = {"\033[6n", 0};
|
1329
1177
|
VALUE resp = console_vt_response(0, 0, io, &query);
|
1330
1178
|
VALUE row, column, term;
|
@@ -1341,18 +1189,44 @@ console_cursor_pos(VALUE io)
|
|
1341
1189
|
RARRAY_ASET(resp, 0, INT2NUM(r));
|
1342
1190
|
RARRAY_ASET(resp, 1, INT2NUM(c));
|
1343
1191
|
return resp;
|
1192
|
+
#endif
|
1344
1193
|
}
|
1345
1194
|
|
1346
1195
|
static VALUE
|
1347
1196
|
console_goto(VALUE io, VALUE y, VALUE x)
|
1348
1197
|
{
|
1198
|
+
#ifdef _WIN32
|
1199
|
+
COORD pos;
|
1200
|
+
int fd = GetWriteFD(io);
|
1201
|
+
pos.X = NUM2UINT(x);
|
1202
|
+
pos.Y = NUM2UINT(y);
|
1203
|
+
if (!SetConsoleCursorPosition((HANDLE)rb_w32_get_osfhandle(fd), pos)) {
|
1204
|
+
rb_syserr_fail(LAST_ERROR, 0);
|
1205
|
+
}
|
1206
|
+
#else
|
1349
1207
|
rb_io_write(io, rb_sprintf("\x1b[%d;%dH", NUM2UINT(y)+1, NUM2UINT(x)+1));
|
1208
|
+
#endif
|
1350
1209
|
return io;
|
1351
1210
|
}
|
1352
1211
|
|
1353
1212
|
static VALUE
|
1354
1213
|
console_move(VALUE io, int y, int x)
|
1355
1214
|
{
|
1215
|
+
#ifdef _WIN32
|
1216
|
+
HANDLE h;
|
1217
|
+
rb_console_size_t ws;
|
1218
|
+
COORD *pos = &ws.dwCursorPosition;
|
1219
|
+
|
1220
|
+
h = (HANDLE)rb_w32_get_osfhandle(GetWriteFD(io));
|
1221
|
+
if (!GetConsoleScreenBufferInfo(h, &ws)) {
|
1222
|
+
rb_syserr_fail(LAST_ERROR, 0);
|
1223
|
+
}
|
1224
|
+
pos->X += x;
|
1225
|
+
pos->Y += y;
|
1226
|
+
if (!SetConsoleCursorPosition(h, *pos)) {
|
1227
|
+
rb_syserr_fail(LAST_ERROR, 0);
|
1228
|
+
}
|
1229
|
+
#else
|
1356
1230
|
if (x || y) {
|
1357
1231
|
VALUE s = rb_str_new_cstr("");
|
1358
1232
|
if (y) rb_str_catf(s, "\x1b[%d%c", y < 0 ? -y : y, y < 0 ? 'A' : 'B');
|
@@ -1360,13 +1234,29 @@ console_move(VALUE io, int y, int x)
|
|
1360
1234
|
rb_io_write(io, s);
|
1361
1235
|
rb_io_flush(io);
|
1362
1236
|
}
|
1237
|
+
#endif
|
1363
1238
|
return io;
|
1364
1239
|
}
|
1365
1240
|
|
1366
1241
|
static VALUE
|
1367
1242
|
console_goto_column(VALUE io, VALUE val)
|
1368
1243
|
{
|
1244
|
+
#ifdef _WIN32
|
1245
|
+
HANDLE h;
|
1246
|
+
rb_console_size_t ws;
|
1247
|
+
COORD *pos = &ws.dwCursorPosition;
|
1248
|
+
|
1249
|
+
h = (HANDLE)rb_w32_get_osfhandle(GetWriteFD(io));
|
1250
|
+
if (!GetConsoleScreenBufferInfo(h, &ws)) {
|
1251
|
+
rb_syserr_fail(LAST_ERROR, 0);
|
1252
|
+
}
|
1253
|
+
pos->X = NUM2INT(val);
|
1254
|
+
if (!SetConsoleCursorPosition(h, *pos)) {
|
1255
|
+
rb_syserr_fail(LAST_ERROR, 0);
|
1256
|
+
}
|
1257
|
+
#else
|
1369
1258
|
rb_io_write(io, rb_sprintf("\x1b[%dG", NUM2UINT(val)+1));
|
1259
|
+
#endif
|
1370
1260
|
return io;
|
1371
1261
|
}
|
1372
1262
|
|
@@ -1374,7 +1264,34 @@ static VALUE
|
|
1374
1264
|
console_erase_line(VALUE io, VALUE val)
|
1375
1265
|
{
|
1376
1266
|
int mode = mode_in_range(val, 2, "line erase");
|
1267
|
+
#ifdef _WIN32
|
1268
|
+
HANDLE h;
|
1269
|
+
rb_console_size_t ws;
|
1270
|
+
COORD *pos = &ws.dwCursorPosition;
|
1271
|
+
DWORD w;
|
1272
|
+
|
1273
|
+
h = (HANDLE)rb_w32_get_osfhandle(GetWriteFD(io));
|
1274
|
+
if (!GetConsoleScreenBufferInfo(h, &ws)) {
|
1275
|
+
rb_syserr_fail(LAST_ERROR, 0);
|
1276
|
+
}
|
1277
|
+
w = winsize_col(&ws);
|
1278
|
+
switch (mode) {
|
1279
|
+
case 0: /* after cursor */
|
1280
|
+
w -= pos->X;
|
1281
|
+
break;
|
1282
|
+
case 1: /* before *and* cursor */
|
1283
|
+
w = pos->X + 1;
|
1284
|
+
pos->X = 0;
|
1285
|
+
break;
|
1286
|
+
case 2: /* entire line */
|
1287
|
+
pos->X = 0;
|
1288
|
+
break;
|
1289
|
+
}
|
1290
|
+
constat_clear(h, ws.wAttributes, w, *pos);
|
1291
|
+
return io;
|
1292
|
+
#else
|
1377
1293
|
rb_io_write(io, rb_sprintf("\x1b[%dK", mode));
|
1294
|
+
#endif
|
1378
1295
|
return io;
|
1379
1296
|
}
|
1380
1297
|
|
@@ -1382,22 +1299,43 @@ static VALUE
|
|
1382
1299
|
console_erase_screen(VALUE io, VALUE val)
|
1383
1300
|
{
|
1384
1301
|
int mode = mode_in_range(val, 3, "screen erase");
|
1385
|
-
|
1386
|
-
|
1387
|
-
|
1302
|
+
#ifdef _WIN32
|
1303
|
+
HANDLE h;
|
1304
|
+
rb_console_size_t ws;
|
1305
|
+
COORD *pos = &ws.dwCursorPosition;
|
1306
|
+
DWORD w;
|
1388
1307
|
|
1389
|
-
|
1390
|
-
|
1391
|
-
|
1392
|
-
if (line) {
|
1393
|
-
VALUE s = rb_sprintf("\x1b[%d%c", line < 0 ? -line : line,
|
1394
|
-
line < 0 ? 'T' : 'S');
|
1395
|
-
rb_io_write(io, s);
|
1308
|
+
h = (HANDLE)rb_w32_get_osfhandle(GetWriteFD(io));
|
1309
|
+
if (!GetConsoleScreenBufferInfo(h, &ws)) {
|
1310
|
+
rb_syserr_fail(LAST_ERROR, 0);
|
1396
1311
|
}
|
1312
|
+
w = winsize_col(&ws);
|
1313
|
+
switch (mode) {
|
1314
|
+
case 0: /* erase after cursor */
|
1315
|
+
w = (w * (ws.srWindow.Bottom - pos->Y + 1) - pos->X);
|
1316
|
+
break;
|
1317
|
+
case 1: /* erase before *and* cursor */
|
1318
|
+
w = (w * (pos->Y - ws.srWindow.Top) + pos->X + 1);
|
1319
|
+
pos->X = 0;
|
1320
|
+
pos->Y = ws.srWindow.Top;
|
1321
|
+
break;
|
1322
|
+
case 2: /* erase entire screen */
|
1323
|
+
w = (w * winsize_row(&ws));
|
1324
|
+
pos->X = 0;
|
1325
|
+
pos->Y = ws.srWindow.Top;
|
1326
|
+
break;
|
1327
|
+
case 3: /* erase entire screen */
|
1328
|
+
w = (w * ws.dwSize.Y);
|
1329
|
+
pos->X = 0;
|
1330
|
+
pos->Y = 0;
|
1331
|
+
break;
|
1332
|
+
}
|
1333
|
+
constat_clear(h, ws.wAttributes, w, *pos);
|
1334
|
+
#else
|
1335
|
+
rb_io_write(io, rb_sprintf("\x1b[%dJ", mode));
|
1336
|
+
#endif
|
1397
1337
|
return io;
|
1398
1338
|
}
|
1399
|
-
# define console_key_pressed_p rb_f_notimplement
|
1400
|
-
#endif
|
1401
1339
|
|
1402
1340
|
static VALUE
|
1403
1341
|
console_cursor_set(VALUE io, VALUE cpos)
|
@@ -1451,6 +1389,38 @@ console_clear_screen(VALUE io)
|
|
1451
1389
|
return io;
|
1452
1390
|
}
|
1453
1391
|
|
1392
|
+
#ifndef HAVE_RB_IO_OPEN_DESCRIPTOR
|
1393
|
+
static VALUE
|
1394
|
+
io_open_descriptor_fallback(VALUE klass, int descriptor, int mode, VALUE path, VALUE timeout, void *encoding)
|
1395
|
+
{
|
1396
|
+
rb_update_max_fd(descriptor);
|
1397
|
+
|
1398
|
+
VALUE arguments[2] = {
|
1399
|
+
INT2NUM(descriptor),
|
1400
|
+
INT2FIX(mode),
|
1401
|
+
};
|
1402
|
+
|
1403
|
+
VALUE self = rb_class_new_instance(2, arguments, klass);
|
1404
|
+
|
1405
|
+
rb_io_t *fptr;
|
1406
|
+
GetOpenFile(self, fptr);
|
1407
|
+
fptr->pathv = path;
|
1408
|
+
fptr->mode |= mode;
|
1409
|
+
|
1410
|
+
return self;
|
1411
|
+
}
|
1412
|
+
#define rb_io_open_descriptor io_open_descriptor_fallback
|
1413
|
+
#endif
|
1414
|
+
|
1415
|
+
#ifndef HAVE_RB_IO_CLOSED_P
|
1416
|
+
static VALUE
|
1417
|
+
rb_io_closed_p(VALUE io)
|
1418
|
+
{
|
1419
|
+
rb_io_t *fptr = RFILE(io)->fptr;
|
1420
|
+
return fptr->fd == -1 ? Qtrue : Qfalse;
|
1421
|
+
}
|
1422
|
+
#endif
|
1423
|
+
|
1454
1424
|
/*
|
1455
1425
|
* call-seq:
|
1456
1426
|
* IO.console -> #<File:/dev/tty>
|
@@ -1468,34 +1438,37 @@ static VALUE
|
|
1468
1438
|
console_dev(int argc, VALUE *argv, VALUE klass)
|
1469
1439
|
{
|
1470
1440
|
VALUE con = 0;
|
1471
|
-
rb_io_t *fptr;
|
1472
1441
|
VALUE sym = 0;
|
1473
1442
|
|
1474
1443
|
rb_check_arity(argc, 0, UNLIMITED_ARGUMENTS);
|
1444
|
+
|
1475
1445
|
if (argc) {
|
1476
|
-
|
1446
|
+
Check_Type(sym = argv[0], T_SYMBOL);
|
1477
1447
|
}
|
1448
|
+
|
1449
|
+
// Force the class to be File.
|
1478
1450
|
if (klass == rb_cIO) klass = rb_cFile;
|
1451
|
+
|
1479
1452
|
if (rb_const_defined(klass, id_console)) {
|
1480
|
-
|
1481
|
-
|
1482
|
-
|
1483
|
-
|
1484
|
-
|
1485
|
-
}
|
1453
|
+
con = rb_const_get(klass, id_console);
|
1454
|
+
if (!RB_TYPE_P(con, T_FILE) || RTEST(rb_io_closed_p(con))) {
|
1455
|
+
rb_const_remove(klass, id_console);
|
1456
|
+
con = 0;
|
1457
|
+
}
|
1486
1458
|
}
|
1459
|
+
|
1487
1460
|
if (sym) {
|
1488
|
-
|
1489
|
-
|
1490
|
-
|
1491
|
-
|
1492
|
-
|
1493
|
-
|
1494
|
-
|
1495
|
-
|
1461
|
+
if (sym == ID2SYM(id_close) && argc == 1) {
|
1462
|
+
if (con) {
|
1463
|
+
rb_io_close(con);
|
1464
|
+
rb_const_remove(klass, id_console);
|
1465
|
+
con = 0;
|
1466
|
+
}
|
1467
|
+
return Qnil;
|
1468
|
+
}
|
1496
1469
|
}
|
1470
|
+
|
1497
1471
|
if (!con) {
|
1498
|
-
VALUE args[2];
|
1499
1472
|
#if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H || defined HAVE_SGTTY_H
|
1500
1473
|
# define CONSOLE_DEVICE "/dev/tty"
|
1501
1474
|
#elif defined _WIN32
|
@@ -1507,44 +1480,36 @@ console_dev(int argc, VALUE *argv, VALUE klass)
|
|
1507
1480
|
# define CONSOLE_DEVICE_FOR_READING CONSOLE_DEVICE
|
1508
1481
|
#endif
|
1509
1482
|
#ifdef CONSOLE_DEVICE_FOR_WRITING
|
1510
|
-
|
1511
|
-
|
1483
|
+
VALUE out;
|
1484
|
+
rb_io_t *ofptr;
|
1512
1485
|
#endif
|
1513
|
-
|
1486
|
+
int fd;
|
1487
|
+
VALUE path = rb_obj_freeze(rb_str_new2(CONSOLE_DEVICE));
|
1514
1488
|
|
1515
1489
|
#ifdef CONSOLE_DEVICE_FOR_WRITING
|
1516
|
-
|
1517
|
-
|
1518
|
-
|
1519
|
-
args[1] = INT2FIX(O_WRONLY);
|
1520
|
-
args[0] = INT2NUM(fd);
|
1521
|
-
out = rb_class_new_instance(2, args, klass);
|
1490
|
+
fd = rb_cloexec_open(CONSOLE_DEVICE_FOR_WRITING, O_RDWR, 0);
|
1491
|
+
if (fd < 0) return Qnil;
|
1492
|
+
out = rb_io_open_descriptor(klass, fd, FMODE_WRITABLE | FMODE_SYNC, path, Qnil, NULL);
|
1522
1493
|
#endif
|
1523
|
-
|
1524
|
-
|
1494
|
+
fd = rb_cloexec_open(CONSOLE_DEVICE_FOR_READING, O_RDWR, 0);
|
1495
|
+
if (fd < 0) {
|
1525
1496
|
#ifdef CONSOLE_DEVICE_FOR_WRITING
|
1526
|
-
|
1497
|
+
rb_io_close(out);
|
1527
1498
|
#endif
|
1528
|
-
|
1529
|
-
|
1530
|
-
|
1531
|
-
|
1532
|
-
args[0] = INT2NUM(fd);
|
1533
|
-
con = rb_class_new_instance(2, args, klass);
|
1534
|
-
GetOpenFile(con, fptr);
|
1535
|
-
fptr->pathv = rb_obj_freeze(rb_str_new2(CONSOLE_DEVICE));
|
1499
|
+
return Qnil;
|
1500
|
+
}
|
1501
|
+
|
1502
|
+
con = rb_io_open_descriptor(klass, fd, FMODE_READWRITE | FMODE_SYNC, path, Qnil, NULL);
|
1536
1503
|
#ifdef CONSOLE_DEVICE_FOR_WRITING
|
1537
|
-
|
1538
|
-
ofptr->pathv = fptr->pathv;
|
1539
|
-
fptr->tied_io_for_writing = out;
|
1540
|
-
ofptr->mode |= FMODE_SYNC;
|
1504
|
+
rb_io_set_write_io(con, out);
|
1541
1505
|
#endif
|
1542
|
-
|
1543
|
-
rb_const_set(klass, id_console, con);
|
1506
|
+
rb_const_set(klass, id_console, con);
|
1544
1507
|
}
|
1508
|
+
|
1545
1509
|
if (sym) {
|
1546
|
-
|
1510
|
+
return rb_f_send(argc, argv, con);
|
1547
1511
|
}
|
1512
|
+
|
1548
1513
|
return con;
|
1549
1514
|
}
|
1550
1515
|
|
@@ -1560,13 +1525,18 @@ io_getch(int argc, VALUE *argv, VALUE io)
|
|
1560
1525
|
return rb_funcallv(io, id_getc, argc, argv);
|
1561
1526
|
}
|
1562
1527
|
|
1563
|
-
#if ENABLE_IO_GETPASS
|
1564
1528
|
static VALUE
|
1565
1529
|
puts_call(VALUE io)
|
1566
1530
|
{
|
1567
1531
|
return rb_io_write(io, rb_default_rs);
|
1568
1532
|
}
|
1569
1533
|
|
1534
|
+
static VALUE
|
1535
|
+
gets_call(VALUE io)
|
1536
|
+
{
|
1537
|
+
return rb_funcallv(io, id_gets, 0, 0);
|
1538
|
+
}
|
1539
|
+
|
1570
1540
|
static VALUE
|
1571
1541
|
getpass_call(VALUE io)
|
1572
1542
|
{
|
@@ -1587,7 +1557,8 @@ static VALUE
|
|
1587
1557
|
str_chomp(VALUE str)
|
1588
1558
|
{
|
1589
1559
|
if (!NIL_P(str)) {
|
1590
|
-
|
1560
|
+
const VALUE rs = rb_default_rs; /* rvalue in TruffleRuby */
|
1561
|
+
rb_funcallv(str, id_chomp_bang, 1, &rs);
|
1591
1562
|
}
|
1592
1563
|
return str;
|
1593
1564
|
}
|
@@ -1604,6 +1575,12 @@ str_chomp(VALUE str)
|
|
1604
1575
|
* see String#chomp!.
|
1605
1576
|
*
|
1606
1577
|
* You must require 'io/console' to use this method.
|
1578
|
+
*
|
1579
|
+
* require 'io/console'
|
1580
|
+
* IO::console.getpass("Enter password:")
|
1581
|
+
* Enter password:
|
1582
|
+
* # => "mypassword"
|
1583
|
+
*
|
1607
1584
|
*/
|
1608
1585
|
static VALUE
|
1609
1586
|
console_getpass(int argc, VALUE *argv, VALUE io)
|
@@ -1614,6 +1591,7 @@ console_getpass(int argc, VALUE *argv, VALUE io)
|
|
1614
1591
|
wio = rb_io_get_write_io(io);
|
1615
1592
|
if (wio == io && io == rb_stdin) wio = rb_stderr;
|
1616
1593
|
prompt(argc, argv, wio);
|
1594
|
+
rb_io_flush(wio);
|
1617
1595
|
str = rb_ensure(getpass_call, io, puts_call, wio);
|
1618
1596
|
return str_chomp(str);
|
1619
1597
|
}
|
@@ -1631,11 +1609,10 @@ io_getpass(int argc, VALUE *argv, VALUE io)
|
|
1631
1609
|
|
1632
1610
|
rb_check_arity(argc, 0, 1);
|
1633
1611
|
prompt(argc, argv, io);
|
1634
|
-
|
1635
|
-
|
1636
|
-
return str;
|
1612
|
+
rb_check_funcall(io, id_flush, 0, 0);
|
1613
|
+
str = rb_ensure(gets_call, io, puts_call, io);
|
1614
|
+
return str_chomp(str);
|
1637
1615
|
}
|
1638
|
-
#endif
|
1639
1616
|
|
1640
1617
|
/*
|
1641
1618
|
* IO console methods
|
@@ -1645,10 +1622,9 @@ Init_console(void)
|
|
1645
1622
|
{
|
1646
1623
|
#undef rb_intern
|
1647
1624
|
id_getc = rb_intern("getc");
|
1648
|
-
#if ENABLE_IO_GETPASS
|
1649
1625
|
id_gets = rb_intern("gets");
|
1626
|
+
id_flush = rb_intern("flush");
|
1650
1627
|
id_chomp_bang = rb_intern("chomp!");
|
1651
|
-
#endif
|
1652
1628
|
id_console = rb_intern("console");
|
1653
1629
|
id_close = rb_intern("close");
|
1654
1630
|
#define init_rawmode_opt_id(name) \
|
@@ -1696,20 +1672,17 @@ InitVM_console(void)
|
|
1696
1672
|
rb_define_method(rb_cIO, "clear_screen", console_clear_screen, 0);
|
1697
1673
|
rb_define_method(rb_cIO, "pressed?", console_key_pressed_p, 1);
|
1698
1674
|
rb_define_method(rb_cIO, "check_winsize_changed", console_check_winsize_changed, 0);
|
1699
|
-
#if ENABLE_IO_GETPASS
|
1700
1675
|
rb_define_method(rb_cIO, "getpass", console_getpass, -1);
|
1701
|
-
#endif
|
1702
1676
|
rb_define_singleton_method(rb_cIO, "console", console_dev, -1);
|
1703
1677
|
{
|
1704
1678
|
VALUE mReadable = rb_define_module_under(rb_cIO, "generic_readable");
|
1705
1679
|
rb_define_method(mReadable, "getch", io_getch, -1);
|
1706
|
-
#if ENABLE_IO_GETPASS
|
1707
1680
|
rb_define_method(mReadable, "getpass", io_getpass, -1);
|
1708
|
-
#endif
|
1709
1681
|
}
|
1710
1682
|
{
|
1711
1683
|
/* :stopdoc: */
|
1712
1684
|
cConmode = rb_define_class_under(rb_cIO, "ConsoleMode", rb_cObject);
|
1685
|
+
rb_define_const(cConmode, "VERSION", rb_str_new_cstr(STRINGIZE(IO_CONSOLE_VERSION)));
|
1713
1686
|
rb_define_alloc_func(cConmode, conmode_alloc);
|
1714
1687
|
rb_undef_method(cConmode, "initialize");
|
1715
1688
|
rb_define_method(cConmode, "initialize_copy", conmode_init_copy, 1);
|
data/ext/io/console/extconf.rb
CHANGED
@@ -1,6 +1,17 @@
|
|
1
1
|
# frozen_string_literal: false
|
2
2
|
require 'mkmf'
|
3
3
|
|
4
|
+
version = ["../../..", "."].find do |dir|
|
5
|
+
break File.read(File.join(__dir__, dir, "io-console.gemspec"))[/^_VERSION\s*=\s*"(.*?)"/, 1]
|
6
|
+
rescue
|
7
|
+
end
|
8
|
+
|
9
|
+
have_func("rb_io_path")
|
10
|
+
have_func("rb_io_descriptor")
|
11
|
+
have_func("rb_io_get_write_io")
|
12
|
+
have_func("rb_io_closed_p")
|
13
|
+
have_func("rb_io_open_descriptor")
|
14
|
+
|
4
15
|
ok = true if RUBY_ENGINE == "ruby" || RUBY_ENGINE == "truffleruby"
|
5
16
|
hdr = nil
|
6
17
|
case
|
@@ -29,7 +40,7 @@ when true
|
|
29
40
|
elsif have_func("rb_scheduler_timeout") # 3.0
|
30
41
|
have_func("rb_io_wait")
|
31
42
|
end
|
32
|
-
$defs << "-D""
|
43
|
+
$defs << "-D""IO_CONSOLE_VERSION=#{version}"
|
33
44
|
create_makefile("io/console") {|conf|
|
34
45
|
conf << "\n""VK_HEADER = #{vk_header}\n"
|
35
46
|
}
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: io-console
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nobu Nakada
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-12-16 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: add console capabilities to IO instances.
|
14
14
|
email: nobu@ruby-lang.org
|
@@ -17,6 +17,7 @@ extensions:
|
|
17
17
|
- ext/io/console/extconf.rb
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
|
+
- ".document"
|
20
21
|
- LICENSE.txt
|
21
22
|
- README.md
|
22
23
|
- ext/io/console/console.c
|
@@ -44,7 +45,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
44
45
|
- !ruby/object:Gem::Version
|
45
46
|
version: '0'
|
46
47
|
requirements: []
|
47
|
-
rubygems_version: 3.
|
48
|
+
rubygems_version: 3.5.0.dev
|
48
49
|
signing_key:
|
49
50
|
specification_version: 4
|
50
51
|
summary: Console interface
|