Socket
Socket
Sign inDemoInstall

cli-width

Package Overview
Dependencies
0
Maintainers
2
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Protect your apps from supply chain attacks

Install

cli-width

Get stdout window width, with two fallbacks, tty and then a default.

    4.1.0latest
    GitHub
    npm

Version published
Maintainers
2
Weekly downloads
23,359,596
increased by3.4%

Weekly downloads

Package description

What is cli-width?

The cli-width package is a simple utility that allows you to retrieve the current width of the command line interface (CLI) in which your node.js application is running. It is useful for creating CLI tools that need to format output according to the width of the terminal window.

What are cli-width's main functionalities?

Get CLI width

This feature allows you to get the current width of the CLI. The function returns a number representing the number of columns in the terminal, or a default value if it can't be determined.

const cliWidth = require('cli-width');
console.log(cliWidth());

Set default width

This feature allows you to set a default width that cli-width will return if it cannot determine the actual width of the terminal. This is useful for ensuring your application has a fallback width to use for formatting output.

const cliWidth = require('cli-width');
cliWidth.defaultWidth = 100;
console.log(cliWidth());

Other packages similar to cli-width

Changelog

Source

4.1.0 (2023-08-05)

Features

  • ts types and test against node 18 and 20 (bc2884f)

Readme

Source

cli-width

Get stdout window width, with four fallbacks, tty, output.columns, a custom environment variable and then a default.

npm version Build Status Coverage Status

Tested against Node v12 to v20. Includes TypeScript types.

Usage

npm install --save cli-width
const cliWidth = require('cli-width');

cliWidth(); // maybe 204 :)

You can also set the CLI_WIDTH environment variable.

If none of the methods are supported, and the environment variable isn't set, 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:

const cliWidth = require('cli-width');
const ttys = require('ttys');

cliWidth({
  defaultWidth: 80,
  output: ttys.output,
});

Defines a different tty module to read width from:

const cliWidth = require('cli-width');
const ttys = require('ttys');

cliWidth({
  tty: ttys,
});

Tests

npm install
npm test

Coverage can be generated with npm run coverage.

FAQs

Last updated on 05 Aug 2023

Did you know?

Socket installs a GitHub app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc