|  | 
|  | 1 | +'use strict'; | 
|  | 2 | +const common = require('../common'); | 
|  | 3 | +const assert = require('assert'); | 
|  | 4 | +const { WriteStream } = require('tty'); | 
|  | 5 | +const { TTY } = process.binding('tty_wrap'); | 
|  | 6 | +const getWindowSize = TTY.prototype.getWindowSize; | 
|  | 7 | + | 
|  | 8 | +function monkeyPatchGetWindowSize(fn) { | 
|  | 9 | +  TTY.prototype.getWindowSize = function() { | 
|  | 10 | +    TTY.prototype.getWindowSize = getWindowSize; | 
|  | 11 | +    return fn.apply(this, arguments); | 
|  | 12 | +  }; | 
|  | 13 | +} | 
|  | 14 | + | 
|  | 15 | +{ | 
|  | 16 | +  // tty.WriteStream constructor does not define columns and rows if an error | 
|  | 17 | +  // occurs while retrieving the window size from the handle. | 
|  | 18 | +  monkeyPatchGetWindowSize(function() { | 
|  | 19 | +    return -1; | 
|  | 20 | +  }); | 
|  | 21 | + | 
|  | 22 | +  const stream = WriteStream(1); | 
|  | 23 | + | 
|  | 24 | +  assert(stream instanceof WriteStream); | 
|  | 25 | +  assert.strictEqual(stream.columns, undefined); | 
|  | 26 | +  assert.strictEqual(stream.rows, undefined); | 
|  | 27 | +} | 
|  | 28 | + | 
|  | 29 | +{ | 
|  | 30 | +  // _refreshSize() emits an error if an error occurs while retrieving the | 
|  | 31 | +  // window size from the handle. | 
|  | 32 | +  const stream = WriteStream(1); | 
|  | 33 | + | 
|  | 34 | +  stream.on('error', common.mustCall((err) => { | 
|  | 35 | +    assert.strictEqual(err.syscall, 'getWindowSize'); | 
|  | 36 | +  })); | 
|  | 37 | + | 
|  | 38 | +  monkeyPatchGetWindowSize(function() { | 
|  | 39 | +    return -1; | 
|  | 40 | +  }); | 
|  | 41 | + | 
|  | 42 | +  stream._refreshSize(); | 
|  | 43 | +} | 
|  | 44 | + | 
|  | 45 | +{ | 
|  | 46 | +  // _refreshSize() emits a 'resize' event when the window size changes. | 
|  | 47 | +  monkeyPatchGetWindowSize(function(size) { | 
|  | 48 | +    size[0] = 80; | 
|  | 49 | +    size[1] = 24; | 
|  | 50 | +    return 0; | 
|  | 51 | +  }); | 
|  | 52 | + | 
|  | 53 | +  const stream = WriteStream(1); | 
|  | 54 | + | 
|  | 55 | +  stream.on('resize', common.mustCall(() => { | 
|  | 56 | +    assert.strictEqual(stream.columns, 82); | 
|  | 57 | +    assert.strictEqual(stream.rows, 26); | 
|  | 58 | +  })); | 
|  | 59 | + | 
|  | 60 | +  assert.strictEqual(stream.columns, 80); | 
|  | 61 | +  assert.strictEqual(stream.rows, 24); | 
|  | 62 | + | 
|  | 63 | +  monkeyPatchGetWindowSize(function(size) { | 
|  | 64 | +    size[0] = 82; | 
|  | 65 | +    size[1] = 26; | 
|  | 66 | +    return 0; | 
|  | 67 | +  }); | 
|  | 68 | + | 
|  | 69 | +  stream._refreshSize(); | 
|  | 70 | +} | 
|  | 71 | + | 
|  | 72 | +{ | 
|  | 73 | +  // WriteStream.prototype.getWindowSize() returns the current columns and rows. | 
|  | 74 | +  monkeyPatchGetWindowSize(function(size) { | 
|  | 75 | +    size[0] = 99; | 
|  | 76 | +    size[1] = 32; | 
|  | 77 | +    return 0; | 
|  | 78 | +  }); | 
|  | 79 | + | 
|  | 80 | +  const stream = WriteStream(1); | 
|  | 81 | + | 
|  | 82 | +  assert.strictEqual(stream.columns, 99); | 
|  | 83 | +  assert.strictEqual(stream.rows, 32); | 
|  | 84 | +  assert.deepStrictEqual(stream.getWindowSize(), [99, 32]); | 
|  | 85 | +} | 
0 commit comments