0e86754f0353161eefb36d205d3eb4660740a07a808aaea496743b3e46b3202147ed2305233dea6afb6fe5eba3daab02a88aef215bb4fd2a6e6da8ecd0df72 1.8 KB

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