diff --git a/lib/readline.js b/lib/readline.js index d08cc5c13a..b52994c3fb 100644 --- a/lib/readline.js +++ b/lib/readline.js @@ -49,11 +49,14 @@ function Interface(output, completer) { this.history = []; 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) { process.on('SIGWINCH', function() { - exports.columns = tty.getColumns(); + var winSize = tty.getWindowSize(0); + exports.columns = winSize[1]; }); } } diff --git a/lib/tty.js b/lib/tty.js index 714031b2c9..5cdd088e8b 100644 --- a/lib/tty.js +++ b/lib/tty.js @@ -4,7 +4,8 @@ var binding = process.binding('stdio'); exports.isatty = binding.isatty; exports.setRawMode = binding.setRawMode; -exports.getColumns = binding.getColumns; +exports.getWindowSize = binding.getWindowSize; +exports.setWindowSize = binding.setWindowSize; exports.open = function(path, args) { diff --git a/src/node_stdio.cc b/src/node_stdio.cc index 9c87dc1fb6..f5af576f32 100644 --- a/src/node_stdio.cc +++ b/src/node_stdio.cc @@ -94,30 +94,47 @@ static Handle SetRawMode (const Arguments& args) { } -// process.binding('stdio').getColumns(); -static Handle GetColumns (const Arguments& args) { +// process.binding('stdio').getWindowSize(fd); +// returns [row, col] +static Handle GetWindowSize (const Arguments& args) { HandleScope scope; + int fd = args[0]->IntegerValue(); + struct winsize ws; - if (ioctl(1, TIOCGWINSZ, &ws) == -1) { - return scope.Close(Integer::New(80)); + if (ioctl(fd, TIOCGWINSZ, &ws) < 0) { + return ThrowException(ErrnoException(errno, "ioctl")); } - return scope.Close(Integer::NewFromUnsigned(ws.ws_col)); + Local 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 GetRows (const Arguments& args) { + +// process.binding('stdio').setWindowSize(fd, row, col); +static Handle SetWindowSize (const Arguments& args) { HandleScope scope; + int fd = args[0]->IntegerValue(); + int row = args[1]->IntegerValue(); + int col = args[2]->IntegerValue(); + struct winsize ws; - if (ioctl(1, TIOCGWINSZ, &ws) == -1) { - return scope.Close(Integer::New(132)); + ws.ws_row = row; + 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 target) { NODE_SET_METHOD(target, "isStdoutBlocking", IsStdoutBlocking); NODE_SET_METHOD(target, "isStdinBlocking", IsStdinBlocking); NODE_SET_METHOD(target, "setRawMode", SetRawMode); - NODE_SET_METHOD(target, "getColumns", GetColumns); - NODE_SET_METHOD(target, "getRows", GetRows); + NODE_SET_METHOD(target, "getWindowSize", GetWindowSize); + NODE_SET_METHOD(target, "setWindowSize", GetWindowSize); NODE_SET_METHOD(target, "isatty", IsATTY); NODE_SET_METHOD(target, "openpty", OpenPTY);