tis-cli前端项目快速搭建命令行工具
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.8 KiB

4 years ago
  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`.