Giter VIP home page Giter VIP logo

cli-width's Introduction

cli-width

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

npm version 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.

cli-width's People

Contributors

dawsbot avatar dependabot[bot] avatar eistaa avatar ginden avatar jacksontian avatar knownasilya avatar mkillianey avatar ruyadorno avatar sboudrias avatar vulpivia avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

cli-width's Issues

Environment variable fallback to share width with children processes

Hi, as you might know already, inquirer makes use of this library to perform it's work.

I'm running into some issues when displaying an inquirer widget inside a script elevated with windosu because of cli-width.

The problem is that windosu calls the script as a child process, and inside that context cli-width returns 0, which causes some problems in this particular line: https://github.com/SBoudrias/Inquirer.js/blob/master/lib/utils/screen-manager.js#L108.

Invalid regular expression: /.{1,0}/: numbers out of order in {} quantifier.

The issue is fixed if we somehow inherit the cli-width from the parent process in the child process, given that just setting a non-zero default value is not enough since inquirer relies on an accurate number to correctly re-render it's widgets.

I was thinking that cli-width could attempt to read from a custom environment variable, like CLI_WIDTH or something like that before returning a default value.

This approach would allow processes to share their cli-width with their children.

For example:

var child_process = require('child_process');
var cliWidth = require('cli-width');

child_process.spawn('node', [ 'foo.js' ], {
    env: {
        CLI_WIDTH: cliWidth()
    }
});

What do you think about this?

Inquirer.js now accepts input/output options

Hi @knownasilya, while I was playing with Inquirer's new input/output streams options (SBoudrias/Inquirer.js#280) I stumbled upon some problems with cli-width making direct access to the process.stdout stream - As you can see in the file changes from the linked PR, the output stream now can be user defined and not only process.stdout.

Now, I don't want to force any design/api but it would be really nice if the module could accept an object as the function parameter, that let us set the output stream (it can always default to process.stdout) and even defining the defaultWidth value too - I find it quite non-standard the way we define the default value as a property before invoking the function.

Let me know if a PR for these changes is welcome and most importantly, many thanks for all the good work ๐Ÿ†

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.