-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
utils.js
124 lines (110 loc) · 2.58 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*!
* window-size <https://github.com/jonschlinkert/window-size>
*
* Copyright (c) 2014-2017, Jon Schlinkert.
* Released under the MIT License.
*/
'use strict';
var os = require('os');
var isNumber = require('is-number');
var cp = require('child_process');
function windowSize(options) {
options = options || {};
return streamSize(options, 'stdout') ||
streamSize(options, 'stderr') ||
envSize() ||
ttySize(options);
}
function streamSize(options, name) {
var stream = (process && process[name]) || options[name];
var size;
if (!stream) return;
if (typeof stream.getWindowSize === 'function') {
size = stream.getWindowSize();
if (isSize(size)) {
return {
width: size[0],
height: size[1],
type: name
};
}
}
size = [stream.columns, stream.rows];
if (isSize(size)) {
return {
width: Number(size[0]),
height: Number(size[1]),
type: name
};
}
}
function envSize() {
if (process && process.env) {
var size = [process.env.COLUMNS, process.env.ROWS];
if (isSize(size)) {
return {
width: Number(size[0]),
height: Number(size[1]),
type: 'process.env'
};
}
}
}
function ttySize(options, stdout) {
var tty = options.tty || require('tty');
if (tty && typeof tty.getWindowSize === 'function') {
var size = tty.getWindowSize(stdout);
if (isSize(size)) {
return {
width: Number(size[1]),
height: Number(size[0]),
type: 'tty'
};
}
}
}
function winSize() {
if (os.release().startsWith('10')) {
var cmd = 'wmic path Win32_VideoController get CurrentHorizontalResolution,CurrentVerticalResolution';
var numberPattern = /\d+/g;
var code = cp.execSync(cmd).toString();
var size = code.match(numberPattern);
if (isSize(size)) {
return {
width: Number(size[0]),
height: Number(size[1]),
type: 'windows'
};
}
}
}
function tputSize() {
try {
var buf = cp.execSync('tput cols && tput lines', {stdio: ['ignore', 'pipe', process.stderr]});
var size = buf.toString().trim().split('\n');
if (isSize(size)) {
return {
width: Number(size[0]),
height: Number(size[1]),
type: 'tput'
};
}
} catch (err) {}
}
/**
* Returns true if the given size array has
* valid height and width values.
*/
function isSize(size) {
return Array.isArray(size) && isNumber(size[0]) && isNumber(size[1]);
}
/**
* Expose `windowSize`
*/
module.exports = {
get: windowSize,
env: envSize,
tty: ttySize,
tput: tputSize,
win: winSize
};