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 NodeJS v10+
|
10 |
|
11 | ## Usage
|
12 |
|
13 | ```
|
14 | npm install --save cli-width
|
15 | ```
|
16 |
|
17 | ```js
|
18 | "use strict";
|
19 |
|
20 | const cliWidth = require("cli-width");
|
21 |
|
22 | cliWidth(); // maybe 204 :)
|
23 | ```
|
24 |
|
25 | You can also set the `CLI_WIDTH` environment variable.
|
26 |
|
27 | If none of the methods are supported, and the environment variable isn't set,
|
28 | the default width value is going to be `0`, that can be changed using the configurable `options`.
|
29 |
|
30 | ## API
|
31 |
|
32 | ### cliWidth([options])
|
33 |
|
34 | `cliWidth` can be configured using an `options` parameter, the possible properties are:
|
35 |
|
36 | - **defaultWidth**\<number\> Defines a default value to be used if none of the methods are available, defaults to `0`
|
37 | - **output**\<object\> A stream to be used to read width values from, defaults to `process.stdout`
|
38 | - **tty**\<object\> TTY module to try to read width from as a fallback, defaults to `require('tty')`
|
39 |
|
40 | ### Examples
|
41 |
|
42 | Defining both a default width value and a stream output to try to read from:
|
43 |
|
44 | ```js
|
45 | const cliWidth = require("cli-width");
|
46 | const ttys = require("ttys");
|
47 |
|
48 | cliWidth({
|
49 | defaultWidth: 80,
|
50 | output: ttys.output,
|
51 | });
|
52 | ```
|
53 |
|
54 | Defines a different tty module to read width from:
|
55 |
|
56 | ```js
|
57 | const cliWidth = require("cli-width");
|
58 | const ttys = require("ttys");
|
59 |
|
60 | cliWidth({
|
61 | tty: ttys,
|
62 | });
|
63 | ```
|
64 |
|
65 | ## Tests
|
66 |
|
67 | ```bash
|
68 | npm install
|
69 | npm test
|
70 | ```
|
71 |
|
72 | Coverage can be generated with `npm run coverage`.
|