1 | # cli-width
|
2 |
|
3 | Get stdout window width, with four fallbacks, `tty`, `output.columns`, a custom environment variable and then a default.
|
4 |
|
5 | [![npm version](https://badge.fury.io/js/cli-width.svg)](http://badge.fury.io/js/cli-width)
|
6 | [![Build Status](https://travis-ci.org/knownasilya/cli-width.svg)](https://travis-ci.org/knownasilya/cli-width)
|
7 | [![Coverage Status](https://coveralls.io/repos/knownasilya/cli-width/badge.svg?branch=master&service=github)](https://coveralls.io/github/knownasilya/cli-width?branch=master)
|
8 |
|
9 | Tested against Node v12 to v20.
|
10 | Includes TypeScript types.
|
11 |
|
12 | ## Usage
|
13 |
|
14 | ```
|
15 | npm install --save cli-width
|
16 | ```
|
17 |
|
18 | ```js
|
19 | const cliWidth = require('cli-width');
|
20 |
|
21 | cliWidth(); // maybe 204 :)
|
22 | ```
|
23 |
|
24 | You can also set the `CLI_WIDTH` environment variable.
|
25 |
|
26 | If none of the methods are supported, and the environment variable isn't set,
|
27 | the default width value is going to be `0`, that can be changed using the configurable `options`.
|
28 |
|
29 | ## API
|
30 |
|
31 | ### cliWidth([options])
|
32 |
|
33 | `cliWidth` can be configured using an `options` parameter, the possible properties are:
|
34 |
|
35 | - **defaultWidth**\<number\> Defines a default value to be used if none of the methods are available, defaults to `0`
|
36 | - **output**\<object\> A stream to be used to read width values from, defaults to `process.stdout`
|
37 | - **tty**\<object\> TTY module to try to read width from as a fallback, defaults to `require('tty')`
|
38 |
|
39 | ### Examples
|
40 |
|
41 | Defining both a default width value and a stream output to try to read from:
|
42 |
|
43 | ```js
|
44 | const cliWidth = require('cli-width');
|
45 | const ttys = require('ttys');
|
46 |
|
47 | cliWidth({
|
48 | defaultWidth: 80,
|
49 | output: ttys.output,
|
50 | });
|
51 | ```
|
52 |
|
53 | Defines a different tty module to read width from:
|
54 |
|
55 | ```js
|
56 | const cliWidth = require('cli-width');
|
57 | const ttys = require('ttys');
|
58 |
|
59 | cliWidth({
|
60 | tty: ttys,
|
61 | });
|
62 | ```
|
63 |
|
64 | ## Tests
|
65 |
|
66 | ```bash
|
67 | npm install
|
68 | npm test
|
69 | ```
|
70 |
|
71 | Coverage can be generated with `npm run coverage`.
|