getWindowSize/setWindowSize

This commit is contained in:
Ryan Dahl 2011-01-10 17:25:48 -08:00
parent 63bd237892
commit b9cfd9527a
3 changed files with 36 additions and 15 deletions

View File

@ -49,11 +49,14 @@ function Interface(output, completer) {
this.history = []; this.history = [];
this.historyIndex = -1; this.historyIndex = -1;
exports.columns = tty.getColumns(); // 0 for stdin
var winSize = tty.getWindowSize(0);
exports.columns = winSize[1];
if (process.listeners('SIGWINCH').length === 0) { if (process.listeners('SIGWINCH').length === 0) {
process.on('SIGWINCH', function() { process.on('SIGWINCH', function() {
exports.columns = tty.getColumns(); var winSize = tty.getWindowSize(0);
exports.columns = winSize[1];
}); });
} }
} }

View File

@ -4,7 +4,8 @@ var binding = process.binding('stdio');
exports.isatty = binding.isatty; exports.isatty = binding.isatty;
exports.setRawMode = binding.setRawMode; exports.setRawMode = binding.setRawMode;
exports.getColumns = binding.getColumns; exports.getWindowSize = binding.getWindowSize;
exports.setWindowSize = binding.setWindowSize;
exports.open = function(path, args) { exports.open = function(path, args) {

View File

@ -94,30 +94,47 @@ static Handle<Value> SetRawMode (const Arguments& args) {
} }
// process.binding('stdio').getColumns(); // process.binding('stdio').getWindowSize(fd);
static Handle<Value> GetColumns (const Arguments& args) { // returns [row, col]
static Handle<Value> GetWindowSize (const Arguments& args) {
HandleScope scope; HandleScope scope;
int fd = args[0]->IntegerValue();
struct winsize ws; struct winsize ws;
if (ioctl(1, TIOCGWINSZ, &ws) == -1) { if (ioctl(fd, TIOCGWINSZ, &ws) < 0) {
return scope.Close(Integer::New(80)); return ThrowException(ErrnoException(errno, "ioctl"));
} }
return scope.Close(Integer::NewFromUnsigned(ws.ws_col)); Local<Array> ret = Array::New(2);
ret->Set(0, Integer::NewFromUnsigned(ws.ws_row));
ret->Set(1, Integer::NewFromUnsigned(ws.ws_col));
return scope.Close(ret);
} }
// process.binding('stdio').getRows();
static Handle<Value> GetRows (const Arguments& args) { // process.binding('stdio').setWindowSize(fd, row, col);
static Handle<Value> SetWindowSize (const Arguments& args) {
HandleScope scope; HandleScope scope;
int fd = args[0]->IntegerValue();
int row = args[1]->IntegerValue();
int col = args[2]->IntegerValue();
struct winsize ws; struct winsize ws;
if (ioctl(1, TIOCGWINSZ, &ws) == -1) { ws.ws_row = row;
return scope.Close(Integer::New(132)); ws.ws_col = col;
ws.ws_xpixel = 0;
ws.ws_ypixel = 0;
if (ioctl(fd, TIOCSWINSZ, &ws) < 0) {
return ThrowException(ErrnoException(errno, "ioctl"));
} }
return scope.Close(Integer::NewFromUnsigned(ws.ws_row)); return True();
} }
@ -283,8 +300,8 @@ void Stdio::Initialize(v8::Handle<v8::Object> target) {
NODE_SET_METHOD(target, "isStdoutBlocking", IsStdoutBlocking); NODE_SET_METHOD(target, "isStdoutBlocking", IsStdoutBlocking);
NODE_SET_METHOD(target, "isStdinBlocking", IsStdinBlocking); NODE_SET_METHOD(target, "isStdinBlocking", IsStdinBlocking);
NODE_SET_METHOD(target, "setRawMode", SetRawMode); NODE_SET_METHOD(target, "setRawMode", SetRawMode);
NODE_SET_METHOD(target, "getColumns", GetColumns); NODE_SET_METHOD(target, "getWindowSize", GetWindowSize);
NODE_SET_METHOD(target, "getRows", GetRows); NODE_SET_METHOD(target, "setWindowSize", GetWindowSize);
NODE_SET_METHOD(target, "isatty", IsATTY); NODE_SET_METHOD(target, "isatty", IsATTY);
NODE_SET_METHOD(target, "openpty", OpenPTY); NODE_SET_METHOD(target, "openpty", OpenPTY);