2 const path = require('path');
3 const execa = require('execa');
5 const create = (columns, rows) => ({
6 columns: parseInt(columns, 10),
7 rows: parseInt(rows, 10)
10 module.exports = () => {
11 const env = process.env;
12 const stdout = process.stdout;
13 const stderr = process.stderr;
15 if (stdout && stdout.columns && stdout.rows) {
16 return create(stdout.columns, stdout.rows);
19 if (stderr && stderr.columns && stderr.rows) {
20 return create(stderr.columns, stderr.rows);
23 // These values are static, so not the first choice
24 if (env.COLUMNS && env.LINES) {
25 return create(env.COLUMNS, env.LINES);
28 if (process.platform === 'win32') {
30 // Binary: https://github.com/sindresorhus/win-term-size
31 const size = execa.sync(path.join(__dirname, 'vendor/windows/term-size.exe')).stdout.split(/\r?\n/);
33 if (size.length === 2) {
34 return create(size[0], size[1]);
38 if (process.platform === 'darwin') {
40 // Binary: https://github.com/sindresorhus/macos-term-size
41 const size = execa.shellSync(path.join(__dirname, 'vendor/macos/term-size')).stdout.split(/\r?\n/);
43 if (size.length === 2) {
44 return create(size[0], size[1]);
49 // `resize` is preferred as it works even when all file descriptors are redirected
50 // https://linux.die.net/man/1/resize
52 const size = execa.sync('resize', ['-u']).stdout.match(/\d+/g);
54 if (size.length === 2) {
55 return create(size[0], size[1]);
60 const columns = execa.sync('tput', ['cols']).stdout;
61 const rows = execa.sync('tput', ['lines']).stdout;
63 if (columns && rows) {
64 return create(columns, rows);
69 return create(80, 24);