Add Stdin.isTerminal.

This commit is contained in:
Bob Nystrom
2016-05-21 12:51:11 -07:00
parent 06731f66d4
commit 6002cc4d65
6 changed files with 20 additions and 0 deletions

View File

@ -16,6 +16,12 @@ Defaults to `false`.
Sets raw mode on or off.
### **isTerminal**
Returns `true` if Stdin is connected to a "TTY". This is true when the user is
running Wren in an interactive terminal, and false if it its input is coming
from a pipe.
### **readByte**()
Reads one byte of input from stdin. Blocks the current fiber until a byte has

View File

@ -39,6 +39,7 @@ extern void statIsDirectory(WrenVM* vm);
extern void statIsFile(WrenVM* vm);
extern void stdinIsRaw(WrenVM* vm);
extern void stdinIsRawSet(WrenVM* vm);
extern void stdinIsTerminal(WrenVM* vm);
extern void stdinReadStart(WrenVM* vm);
extern void stdinReadStop(WrenVM* vm);
extern void schedulerCaptureMethods(WrenVM* vm);
@ -147,6 +148,7 @@ static ModuleRegistry modules[] =
CLASS(Stdin)
STATIC_METHOD("isRaw", stdinIsRaw)
STATIC_METHOD("isRaw=(_)", stdinIsRawSet)
STATIC_METHOD("isTerminal", stdinIsTerminal)
STATIC_METHOD("readStart_()", stdinReadStart)
STATIC_METHOD("readStop_()", stdinReadStop)
END_CLASS

View File

@ -539,6 +539,12 @@ void stdinIsRawSet(WrenVM* vm)
}
}
void stdinIsTerminal(WrenVM* vm)
{
initStdin();
wrenSetSlotBool(vm, 0, uv_guess_handle(stdinDescriptor) == UV_TTY);
}
static void allocCallback(uv_handle_t* handle, size_t suggestedSize,
uv_buf_t* buf)
{

View File

@ -217,6 +217,7 @@ foreign class Stat {
class Stdin {
foreign static isRaw
foreign static isRaw=(value)
foreign static isTerminal
static readByte() {
return read_ {

View File

@ -219,6 +219,7 @@ static const char* ioModuleSource =
"class Stdin {\n"
" foreign static isRaw\n"
" foreign static isRaw=(value)\n"
" foreign static isTerminal\n"
"\n"
" static readByte() {\n"
" return read_ {\n"

View File

@ -0,0 +1,4 @@
import "io" for Stdin
// The tests aren't run in a terminal.
System.print(Stdin.isTerminal) // expect: false