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.

115 lines
2.6 KiB

4 years ago
  1. # cliui
  2. [![Build Status](https://travis-ci.org/yargs/cliui.svg)](https://travis-ci.org/yargs/cliui)
  3. [![Coverage Status](https://coveralls.io/repos/yargs/cliui/badge.svg?branch=)](https://coveralls.io/r/yargs/cliui?branch=)
  4. [![NPM version](https://img.shields.io/npm/v/cliui.svg)](https://www.npmjs.com/package/cliui)
  5. [![Standard Version](https://img.shields.io/badge/release-standard%20version-brightgreen.svg)](https://github.com/conventional-changelog/standard-version)
  6. easily create complex multi-column command-line-interfaces.
  7. ## Example
  8. ```js
  9. var ui = require('cliui')()
  10. ui.div('Usage: $0 [command] [options]')
  11. ui.div({
  12. text: 'Options:',
  13. padding: [2, 0, 2, 0]
  14. })
  15. ui.div(
  16. {
  17. text: "-f, --file",
  18. width: 20,
  19. padding: [0, 4, 0, 4]
  20. },
  21. {
  22. text: "the file to load." +
  23. chalk.green("(if this description is long it wraps).")
  24. ,
  25. width: 20
  26. },
  27. {
  28. text: chalk.red("[required]"),
  29. align: 'right'
  30. }
  31. )
  32. console.log(ui.toString())
  33. ```
  34. <img width="500" src="screenshot.png">
  35. ## Layout DSL
  36. cliui exposes a simple layout DSL:
  37. If you create a single `ui.div`, passing a string rather than an
  38. object:
  39. * `\n`: characters will be interpreted as new rows.
  40. * `\t`: characters will be interpreted as new columns.
  41. * `\s`: characters will be interpreted as padding.
  42. **as an example...**
  43. ```js
  44. var ui = require('./')({
  45. width: 60
  46. })
  47. ui.div(
  48. 'Usage: node ./bin/foo.js\n' +
  49. ' <regex>\t provide a regex\n' +
  50. ' <glob>\t provide a glob\t [required]'
  51. )
  52. console.log(ui.toString())
  53. ```
  54. **will output:**
  55. ```shell
  56. Usage: node ./bin/foo.js
  57. <regex> provide a regex
  58. <glob> provide a glob [required]
  59. ```
  60. ## Methods
  61. ```js
  62. cliui = require('cliui')
  63. ```
  64. ### cliui({width: integer})
  65. Specify the maximum width of the UI being generated.
  66. If no width is provided, cliui will try to get the current window's width and use it, and if that doesn't work, width will be set to `80`.
  67. ### cliui({wrap: boolean})
  68. Enable or disable the wrapping of text in a column.
  69. ### cliui.div(column, column, column)
  70. Create a row with any number of columns, a column
  71. can either be a string, or an object with the following
  72. options:
  73. * **text:** some text to place in the column.
  74. * **width:** the width of a column.
  75. * **align:** alignment, `right` or `center`.
  76. * **padding:** `[top, right, bottom, left]`.
  77. * **border:** should a border be placed around the div?
  78. ### cliui.span(column, column, column)
  79. Similar to `div`, except the next row will be appended without
  80. a new line being created.
  81. ### cliui.resetOutput()
  82. Resets the UI elements of the current cliui instance, maintaining the values
  83. set for `width` and `wrap`.