Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
IO#isatty
() -> bool
isatty(*args)
ext/io/console/console.c
View on GitHub ↗
static VALUE
console_platform_tty_p(int argc, VALUE *argv, VALUE io)
{
VALUE ret = Qfalse;
int mode = 0;
if (argc > 0) {
int i;
for (i = 0; i < argc; ++i) {
VALUE m = argv[i];
if (NIL_P(m)) {
mode |= platform_default_bit;
continue;
}
Check_Type(m, T_SYMBOL);
if (m == ID2SYM(rb_intern("any"))) {
mode |= platform_any_bit;
}
#if defined _WIN32
else if (m == ID2SYM(rb_intern("cygwin"))) {
mode |= platform_cygwin_bit;
}
else if (m == ID2SYM(rb_intern("msys"))) {
mode |= platform_msys_bit;
}
#endif
else {
rb_raise(rb_eArgError, "unknown tty type: %+" PRIsVALUE, m);
}
}
}
if ((mode & platform_default_bit) || (mode == 0)) {
ret = rb_call_super(0, 0);
}
if ((mode & ~platform_default_bit) && !RTEST(ret)) {
#if defined _WIN32
if (mode & (platform_cygwin_bit | platform_msys_bit)) {
struct {
FILE_NAME_INFO info;
WCHAR rest[MAX_PATH];
} buffer;
HANDLE h = (HANDLE)rb_w32_get_osfhandle(GetReadFD(io));
if ((GetFileType(h) == FILE_TYPE_PIPE) &&
GetFileInformationByHandleEx(h, FileNameInfo, &buffer, sizeof(buffer))) {
WCHAR *const name = buffer.info.FileName;
DWORD len = buffer.info.FileNameLength / sizeof(WCHAR);
name[len] = L'\0';
# define tty_pipe_p(type) \
(memcmp(name, L"\\" #type "-", sizeof(L"\\" #type)) == 0 && \
wcsstr(&name[rb_strlen_lit("\\" #type "-")], L"-pty") != NULL)
if (!ret && (mode & platform_cygwin_bit)) {
ret = tty_pipe_p(cygwin);
}
if (!ret && (mode & platform_msys_bit)) {
ret = tty_pipe_p(msys);
}
}
}
#endif
}
return ret;
}
Alias of #tty?
Returns true if the stream is associated with a terminal device (tty), false otherwise.
If one or more types are given, returns true if the stream is associated with any of the specified tty types.
-
nil: Returns the result of the default tty check, as if no type were given. It can be combined with other types. -
:any: Returnstruefor any known kind of tty, including the default tty. -
:cygwin: Returnstruefor cygwin tty, on Windows. -
:msys: Returnstruefor msys2 tty, on Windows.