├── .prettierrc
├── .gitignore
├── .github
├── FUNDING.yml
└── workflows
│ └── node.js.yml
├── index.d.ts
├── LICENSE
├── index.js
├── package.json
├── README.md
├── CHANGELOG.md
└── test
└── index.js
/.prettierrc:
--------------------------------------------------------------------------------
1 | {
2 | "singleQuote": true
3 | }
4 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | coverage
3 | *.log
4 | *.swp
5 | .DS_Store
6 | .nyc_output
--------------------------------------------------------------------------------
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | # These are supported funding model platforms
2 |
3 | github: knownasilya
4 |
--------------------------------------------------------------------------------
/index.d.ts:
--------------------------------------------------------------------------------
1 | // Type definitions for cli-width 4.0
2 | ///
3 |
4 | import { Stream } from 'stream';
5 | import tty = require('tty');
6 |
7 | declare function cliWidth(options?: {
8 | defaultWidth?: number;
9 | output?: Stream;
10 | tty?: typeof tty;
11 | }): number;
12 |
13 | export = cliWidth;
14 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2015, Ilya Radchenko
2 |
3 | Permission to use, copy, modify, and/or distribute this software for any
4 | purpose with or without fee is hereby granted, provided that the above
5 | copyright notice and this permission notice appear in all copies.
6 |
7 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10 | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14 |
--------------------------------------------------------------------------------
/.github/workflows/node.js.yml:
--------------------------------------------------------------------------------
1 | # This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3 |
4 | name: Node.js CI
5 |
6 | on:
7 | push:
8 | branches: [master]
9 | pull_request:
10 | branches: [master]
11 |
12 | jobs:
13 | build:
14 | runs-on: ubuntu-latest
15 |
16 | strategy:
17 | matrix:
18 | node-version: [12.x, 14.x, 16.x, 18.x, 20.x]
19 | # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
20 |
21 | steps:
22 | - uses: actions/checkout@v3
23 | - name: Use Node.js ${{ matrix.node-version }}
24 | uses: actions/setup-node@v3
25 | with:
26 | node-version: ${{ matrix.node-version }}
27 | cache: 'npm'
28 | - run: npm ci
29 | - run: npm run build --if-present
30 | - run: npm test
31 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | module.exports = cliWidth;
4 |
5 | function normalizeOpts(options) {
6 | const 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 | const 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 | const 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 | }
50 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "cli-width",
3 | "version": "4.1.0",
4 | "description": "Get stdout window width, with two fallbacks, tty and then a default.",
5 | "main": "index.js",
6 | "scripts": {
7 | "test": "node test | tspec",
8 | "coverage": "nyc node test | tspec",
9 | "coveralls": "npm run coverage -s && coveralls < coverage/lcov.info",
10 | "release": "standard-version"
11 | },
12 | "repository": {
13 | "type": "git",
14 | "url": "git@github.com:knownasilya/cli-width.git"
15 | },
16 | "author": "Ilya Radchenko ",
17 | "license": "ISC",
18 | "bugs": {
19 | "url": "https://github.com/knownasilya/cli-width/issues"
20 | },
21 | "homepage": "https://github.com/knownasilya/cli-width",
22 | "engines": {
23 | "node": ">= 12"
24 | },
25 | "devDependencies": {
26 | "coveralls": "^3.1.1",
27 | "nyc": "^15.1.0",
28 | "standard-version": "^9.3.2",
29 | "tap-spec": "^5.0.0",
30 | "tape": "^5.5.2"
31 | },
32 | "volta": {
33 | "node": "12.22.11",
34 | "npm": "8.5.5"
35 | },
36 | "files": [
37 | "index.js",
38 | "index.d.ts"
39 | ]
40 | }
41 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
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 | [](http://badge.fury.io/js/cli-width)
6 | [](https://coveralls.io/github/knownasilya/cli-width?branch=master)
7 |
8 | Tested against Node v12 to v20.
9 | Includes TypeScript types.
10 |
11 | ## Usage
12 |
13 | ```
14 | npm install --save cli-width
15 | ```
16 |
17 | ```js
18 | const cliWidth = require('cli-width');
19 |
20 | cliWidth(); // maybe 204 :)
21 | ```
22 |
23 | You can also set the `CLI_WIDTH` environment variable.
24 |
25 | If none of the methods are supported, and the environment variable isn't set,
26 | the default width value is going to be `0`, that can be changed using the configurable `options`.
27 |
28 | ## API
29 |
30 | ### cliWidth([options])
31 |
32 | `cliWidth` can be configured using an `options` parameter, the possible properties are:
33 |
34 | - **defaultWidth**\ Defines a default value to be used if none of the methods are available, defaults to `0`
35 | - **output**\