Skip to content

Commit b9cfd95

Browse files
committed
getWindowSize/setWindowSize
1 parent 63bd237 commit b9cfd95

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

lib/readline.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,14 @@ function Interface(output, completer) {
4949
this.history = [];
5050
this.historyIndex = -1;
5151

52-
exports.columns = tty.getColumns();
52+
// 0 for stdin
53+
var winSize = tty.getWindowSize(0);
54+
exports.columns = winSize[1];
5355

5456
if (process.listeners('SIGWINCH').length === 0) {
5557
process.on('SIGWINCH', function() {
56-
exports.columns = tty.getColumns();
58+
var winSize = tty.getWindowSize(0);
59+
exports.columns = winSize[1];
5760
});
5861
}
5962
}

lib/tty.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ var binding = process.binding('stdio');
44

55
exports.isatty = binding.isatty;
66
exports.setRawMode = binding.setRawMode;
7-
exports.getColumns = binding.getColumns;
7+
exports.getWindowSize = binding.getWindowSize;
8+
exports.setWindowSize = binding.setWindowSize;
89

910

1011
exports.open = function(path, args) {

src/node_stdio.cc

+29-12
Original file line numberDiff line numberDiff line change
@@ -94,30 +94,47 @@ static Handle<Value> SetRawMode (const Arguments& args) {
9494
}
9595

9696

97-
// process.binding('stdio').getColumns();
98-
static Handle<Value> GetColumns (const Arguments& args) {
97+
// process.binding('stdio').getWindowSize(fd);
98+
// returns [row, col]
99+
static Handle<Value> GetWindowSize (const Arguments& args) {
99100
HandleScope scope;
100101

102+
int fd = args[0]->IntegerValue();
103+
101104
struct winsize ws;
102105

103-
if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
104-
return scope.Close(Integer::New(80));
106+
if (ioctl(fd, TIOCGWINSZ, &ws) < 0) {
107+
return ThrowException(ErrnoException(errno, "ioctl"));
105108
}
106109

107-
return scope.Close(Integer::NewFromUnsigned(ws.ws_col));
110+
Local<Array> ret = Array::New(2);
111+
ret->Set(0, Integer::NewFromUnsigned(ws.ws_row));
112+
ret->Set(1, Integer::NewFromUnsigned(ws.ws_col));
113+
114+
return scope.Close(ret);
108115
}
109116

110-
// process.binding('stdio').getRows();
111-
static Handle<Value> GetRows (const Arguments& args) {
117+
118+
// process.binding('stdio').setWindowSize(fd, row, col);
119+
static Handle<Value> SetWindowSize (const Arguments& args) {
112120
HandleScope scope;
113121

122+
int fd = args[0]->IntegerValue();
123+
int row = args[1]->IntegerValue();
124+
int col = args[2]->IntegerValue();
125+
114126
struct winsize ws;
115127

116-
if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
117-
return scope.Close(Integer::New(132));
128+
ws.ws_row = row;
129+
ws.ws_col = col;
130+
ws.ws_xpixel = 0;
131+
ws.ws_ypixel = 0;
132+
133+
if (ioctl(fd, TIOCSWINSZ, &ws) < 0) {
134+
return ThrowException(ErrnoException(errno, "ioctl"));
118135
}
119136

120-
return scope.Close(Integer::NewFromUnsigned(ws.ws_row));
137+
return True();
121138
}
122139

123140

@@ -283,8 +300,8 @@ void Stdio::Initialize(v8::Handle<v8::Object> target) {
283300
NODE_SET_METHOD(target, "isStdoutBlocking", IsStdoutBlocking);
284301
NODE_SET_METHOD(target, "isStdinBlocking", IsStdinBlocking);
285302
NODE_SET_METHOD(target, "setRawMode", SetRawMode);
286-
NODE_SET_METHOD(target, "getColumns", GetColumns);
287-
NODE_SET_METHOD(target, "getRows", GetRows);
303+
NODE_SET_METHOD(target, "getWindowSize", GetWindowSize);
304+
NODE_SET_METHOD(target, "setWindowSize", GetWindowSize);
288305
NODE_SET_METHOD(target, "isatty", IsATTY);
289306
NODE_SET_METHOD(target, "openpty", OpenPTY);
290307

0 commit comments

Comments
 (0)