1 | "use strict";
|
2 |
|
3 | exports = module.exports = cliWidth;
|
4 |
|
5 | function normalizeOpts(options) {
|
6 | let defaultOpts = {
|
7 | defaultWidth: 0,
|
8 | output: process.stdout,
|
9 | tty: require("tty"),
|
10 | };
|
11 |
|
12 | if (!options) {
|
13 | return defaultOpts;
|
14 | }
|
15 |
|
16 | Object.keys(defaultOpts).forEach(function (key) {
|
17 | if (!options[key]) {
|
18 | options[key] = defaultOpts[key];
|
19 | }
|
20 | });
|
21 |
|
22 | return options;
|
23 | }
|
24 |
|
25 | function cliWidth(options) {
|
26 | let opts = normalizeOpts(options);
|
27 |
|
28 | if (opts.output.getWindowSize) {
|
29 | return opts.output.getWindowSize()[0] || opts.defaultWidth;
|
30 | }
|
31 |
|
32 | if (opts.tty.getWindowSize) {
|
33 | return opts.tty.getWindowSize()[1] || opts.defaultWidth;
|
34 | }
|
35 |
|
36 | if (opts.output.columns) {
|
37 | return opts.output.columns;
|
38 | }
|
39 |
|
40 | if (process.env.CLI_WIDTH) {
|
41 | let width = parseInt(process.env.CLI_WIDTH, 10);
|
42 |
|
43 | if (!isNaN(width) && width !== 0) {
|
44 | return width;
|
45 | }
|
46 | }
|
47 |
|
48 | return opts.defaultWidth;
|
49 | }
|