cli-width
Advanced tools
Comparing version 1.1.1 to 2.0.0
34
index.js
'use strict'; | ||
exports = module.exports = cliWidth; | ||
exports.defaultWidth = 0; | ||
function cliWidth() { | ||
if (process.stdout.getWindowSize) { | ||
return process.stdout.getWindowSize()[0] || exports.defaultWidth; | ||
function normalizeOpts(options) { | ||
var defaultOpts = { | ||
defaultWidth: 0, | ||
output: process.stdout, | ||
tty: require('tty') | ||
}; | ||
if (!options) { | ||
return defaultOpts; | ||
} else { | ||
Object.keys(defaultOpts).forEach(function (key) { | ||
if (!options[key]) { | ||
options[key] = defaultOpts[key]; | ||
} | ||
}); | ||
return options; | ||
} | ||
} | ||
function cliWidth(options) { | ||
var opts = normalizeOpts(options); | ||
if (opts.output.getWindowSize) { | ||
return opts.output.getWindowSize()[0] || opts.defaultWidth; | ||
} | ||
else { | ||
var tty = require('tty'); | ||
if (tty.getWindowSize) { | ||
return tty.getWindowSize()[1] || exports.defaultWidth; | ||
if (opts.tty.getWindowSize) { | ||
return opts.tty.getWindowSize()[1] || opts.defaultWidth; | ||
} | ||
@@ -25,5 +41,5 @@ else { | ||
return exports.defaultWidth; | ||
return opts.defaultWidth; | ||
} | ||
} | ||
}; |
{ | ||
"name": "cli-width", | ||
"version": "1.1.1", | ||
"version": "2.0.0", | ||
"description": "Get stdout window width, with two fallbacks, tty and then a default.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -27,4 +27,40 @@ cli-width | ||
If none of the methods are supported, and the environment variable isn't set, | ||
the default is `0` and can be changed via `cliWidth.defaultWidth = 200;`. | ||
the default width value is going to be `0`, that can be changed using the configurable `options`. | ||
## API | ||
### cliWidth([options]) | ||
`cliWidth` can be configured using an `options` parameter, the possible properties are: | ||
- **defaultWidth**\<number\> Defines a default value to be used if none of the methods are available, defaults to `0` | ||
- **output**\<object\> A stream to be used to read width values from, defaults to `process.stdout` | ||
- **tty**\<object\> TTY module to try to read width from as a fallback, defaults to `require('tty')` | ||
### Examples | ||
Defining both a default width value and a stream output to try to read from: | ||
```js | ||
var cliWidth = require('cli-width'); | ||
var ttys = require('ttys'); | ||
cliWidth({ | ||
defaultWidth: 80, | ||
output: ttys.output | ||
}); | ||
``` | ||
Defines a different tty module to read width from: | ||
```js | ||
var cliWidth = require('cli-width'); | ||
var ttys = require('ttys'); | ||
cliWidth({ | ||
tty: ttys | ||
}); | ||
``` | ||
## Tests | ||
@@ -31,0 +67,0 @@ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
45086
422
73
0