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.

3 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. gauge
  2. =====
  3. A nearly stateless terminal based horizontal gauge / progress bar.
  4. ```javascript
  5. var Gauge = require("gauge")
  6. var gauge = new Gauge()
  7. gauge.show("test", 0.20)
  8. gauge.pulse("this")
  9. gauge.hide()
  10. ```
  11. ![](gauge-demo.gif)
  12. ### CHANGES FROM 1.x
  13. Gauge 2.x is breaking release, please see the [changelog] for details on
  14. what's changed if you were previously a user of this module.
  15. [changelog]: CHANGELOG.md
  16. ### THE GAUGE CLASS
  17. This is the typical interface to the module– it provides a pretty
  18. fire-and-forget interface to displaying your status information.
  19. ```
  20. var Gauge = require("gauge")
  21. var gauge = new Gauge([stream], [options])
  22. ```
  23. * **stream** – *(optional, default STDERR)* A stream that progress bar
  24. updates are to be written to. Gauge honors backpressure and will pause
  25. most writing if it is indicated.
  26. * **options** – *(optional)* An option object.
  27. Constructs a new gauge. Gauges are drawn on a single line, and are not drawn
  28. if **stream** isn't a tty and a tty isn't explicitly provided.
  29. If **stream** is a terminal or if you pass in **tty** to **options** then we
  30. will detect terminal resizes and redraw to fit. We do this by watching for
  31. `resize` events on the tty. (To work around a bug in verisons of Node prior
  32. to 2.5.0, we watch for them on stdout if the tty is stderr.) Resizes to
  33. larger window sizes will be clean, but shrinking the window will always
  34. result in some cruft.
  35. **IMPORTANT:** If you prevously were passing in a non-tty stream but you still
  36. want output (for example, a stream wrapped by the `ansi` module) then you
  37. need to pass in the **tty** option below, as `gauge` needs access to
  38. the underlying tty in order to do things like terminal resizes and terminal
  39. width detection.
  40. The **options** object can have the following properties, all of which are
  41. optional:
  42. * **updateInterval**: How often gauge updates should be drawn, in miliseconds.
  43. * **fixedFramerate**: Defaults to false on node 0.8, true on everything
  44. else. When this is true a timer is created to trigger once every
  45. `updateInterval` ms, when false, updates are printed as soon as they come
  46. in but updates more often than `updateInterval` are ignored. The reason
  47. 0.8 doesn't have this set to true is that it can't `unref` its timer and
  48. so it would stop your program from exiting– if you want to use this
  49. feature with 0.8 just make sure you call `gauge.disable()` before you
  50. expect your program to exit.
  51. * **themes**: A themeset to use when selecting the theme to use. Defaults
  52. to `gauge/themes`, see the [themes] documentation for details.
  53. * **theme**: Select a theme for use, it can be a:
  54. * Theme object, in which case the **themes** is not used.
  55. * The name of a theme, which will be looked up in the current *themes*
  56. object.
  57. * A configuration object with any of `hasUnicode`, `hasColor` or
  58. `platform` keys, which if wlll be used to override our guesses when making
  59. a default theme selection.
  60. If no theme is selected then a default is picked using a combination of our
  61. best guesses at your OS, color support and unicode support.
  62. * **template**: Describes what you want your gauge to look like. The
  63. default is what npm uses. Detailed [documentation] is later in this
  64. document.
  65. * **hideCursor**: Defaults to true. If true, then the cursor will be hidden
  66. while the gauge is displayed.
  67. * **tty**: The tty that you're ultimately writing to. Defaults to the same
  68. as **stream**. This is used for detecting the width of the terminal and
  69. resizes. The width used is `tty.columns - 1`. If no tty is available then
  70. a width of `79` is assumed.
  71. * **enabled**: Defaults to true if `tty` is a TTY, false otherwise. If true
  72. the gauge starts enabled. If disabled then all update commands are
  73. ignored and no gauge will be printed until you call `.enable()`.
  74. * **Plumbing**: The class to use to actually generate the gauge for
  75. printing. This defaults to `require('gauge/plumbing')` and ordinarly you
  76. shouldn't need to override this.
  77. * **cleanupOnExit**: Defaults to true. Ordinarily we register an exit
  78. handler to make sure your cursor is turned back on and the progress bar
  79. erased when your process exits, even if you Ctrl-C out or otherwise exit
  80. unexpectedly. You can disable this and it won't register the exit handler.
  81. [has-unicode]: https://www.npmjs.com/package/has-unicode
  82. [themes]: #themes
  83. [documentation]: #templates
  84. #### `gauge.show(section | status, [completed])`
  85. The first argument is either the section, the name of the current thing
  86. contributing to progress, or an object with keys like **section**,
  87. **subsection** & **completed** (or any others you have types for in a custom
  88. template). If you don't want to update or set any of these you can pass
  89. `null` and it will be ignored.
  90. The second argument is the percent completed as a value between 0 and 1.
  91. Without it, completion is just not updated. You'll also note that completion
  92. can be passed in as part of a status object as the first argument. If both
  93. it and the completed argument are passed in, the completed argument wins.
  94. #### `gauge.hide([cb])`
  95. Removes the gauge from the terminal. Optionally, callback `cb` after IO has
  96. had an opportunity to happen (currently this just means after `setImmediate`
  97. has called back.)
  98. It turns out this is important when you're pausing the progress bar on one
  99. filehandle and printing to another– otherwise (with a big enough print) node
  100. can end up printing the "end progress bar" bits to the progress bar filehandle
  101. while other stuff is printing to another filehandle. These getting interleaved
  102. can cause corruption in some terminals.
  103. #### `gauge.pulse([subsection])`
  104. * **subsection** – *(optional)* The specific thing that triggered this pulse
  105. Spins the spinner in the gauge to show output. If **subsection** is
  106. included then it will be combined with the last name passed to `gauge.show`.
  107. #### `gauge.disable()`
  108. Hides the gauge and ignores further calls to `show` or `pulse`.
  109. #### `gauge.enable()`
  110. Shows the gauge and resumes updating when `show` or `pulse` is called.
  111. #### `gauge.isEnabled()`
  112. Returns true if the gauge is enabled.
  113. #### `gauge.setThemeset(themes)`
  114. Change the themeset to select a theme from. The same as the `themes` option
  115. used in the constructor. The theme will be reselected from this themeset.
  116. #### `gauge.setTheme(theme)`
  117. Change the active theme, will be displayed with the next show or pulse. This can be:
  118. * Theme object, in which case the **themes** is not used.
  119. * The name of a theme, which will be looked up in the current *themes*
  120. object.
  121. * A configuration object with any of `hasUnicode`, `hasColor` or
  122. `platform` keys, which if wlll be used to override our guesses when making
  123. a default theme selection.
  124. If no theme is selected then a default is picked using a combination of our
  125. best guesses at your OS, color support and unicode support.
  126. #### `gauge.setTemplate(template)`
  127. Change the active template, will be displayed with the next show or pulse
  128. ### Tracking Completion
  129. If you have more than one thing going on that you want to track completion
  130. of, you may find the related [are-we-there-yet] helpful. It's `change`
  131. event can be wired up to the `show` method to get a more traditional
  132. progress bar interface.
  133. [are-we-there-yet]: https://www.npmjs.com/package/are-we-there-yet
  134. ### THEMES
  135. ```
  136. var themes = require('gauge/themes')
  137. // fetch the default color unicode theme for this platform
  138. var ourTheme = themes({hasUnicode: true, hasColor: true})
  139. // fetch the default non-color unicode theme for osx
  140. var ourTheme = themes({hasUnicode: true, hasColor: false, platform: 'darwin'})
  141. // create a new theme based on the color ascii theme for this platform
  142. // that brackets the progress bar with arrows
  143. var ourTheme = themes.newTheme(theme(hasUnicode: false, hasColor: true}), {
  144. preProgressbar: '→',
  145. postProgressbar: '←'
  146. })
  147. ```
  148. The object returned by `gauge/themes` is an instance of the `ThemeSet` class.
  149. ```
  150. var ThemeSet = require('gauge/theme-set')
  151. var themes = new ThemeSet()
  152. // or
  153. var themes = require('gauge/themes')
  154. var mythemes = themes.newThemeset() // creates a new themeset based on the default themes
  155. ```
  156. #### themes(opts)
  157. #### themes.getDefault(opts)
  158. Theme objects are a function that fetches the default theme based on
  159. platform, unicode and color support.
  160. Options is an object with the following properties:
  161. * **hasUnicode** - If true, fetch a unicode theme, if no unicode theme is
  162. available then a non-unicode theme will be used.
  163. * **hasColor** - If true, fetch a color theme, if no color theme is
  164. available a non-color theme will be used.
  165. * **platform** (optional) - Defaults to `process.platform`. If no
  166. platform match is available then `fallback` is used instead.
  167. If no compatible theme can be found then an error will be thrown with a
  168. `code` of `EMISSINGTHEME`.
  169. #### themes.addTheme(themeName, themeObj)
  170. #### themes.addTheme(themeName, [parentTheme], newTheme)
  171. Adds a named theme to the themeset. You can pass in either a theme object,
  172. as returned by `themes.newTheme` or the arguments you'd pass to
  173. `themes.newTheme`.
  174. #### themes.getThemeNames()
  175. Return a list of all of the names of the themes in this themeset. Suitable
  176. for use in `themes.getTheme(…)`.
  177. #### themes.getTheme(name)
  178. Returns the theme object from this theme set named `name`.
  179. If `name` does not exist in this themeset an error will be thrown with
  180. a `code` of `EMISSINGTHEME`.
  181. #### themes.setDefault([opts], themeName)
  182. `opts` is an object with the following properties.
  183. * **platform** - Defaults to `'fallback'`. If your theme is platform
  184. specific, specify that here with the platform from `process.platform`, eg,
  185. `win32`, `darwin`, etc.
  186. * **hasUnicode** - Defaults to `false`. If your theme uses unicode you
  187. should set this to true.
  188. * **hasColor** - Defaults to `false`. If your theme uses color you should
  189. set this to true.
  190. `themeName` is the name of the theme (as given to `addTheme`) to use for
  191. this set of `opts`.
  192. #### themes.newTheme([parentTheme,] newTheme)
  193. Create a new theme object based on `parentTheme`. If no `parentTheme` is
  194. provided then a minimal parentTheme that defines functions for rendering the
  195. activity indicator (spinner) and progress bar will be defined. (This
  196. fallback parent is defined in `gauge/base-theme`.)
  197. newTheme should be a bare object– we'll start by discussing the properties
  198. defined by the default themes:
  199. * **preProgressbar** - displayed prior to the progress bar, if the progress
  200. bar is displayed.
  201. * **postProgressbar** - displayed after the progress bar, if the progress bar
  202. is displayed.
  203. * **progressBarTheme** - The subtheme passed through to the progress bar
  204. renderer, it's an object with `complete` and `remaining` properties
  205. that are the strings you want repeated for those sections of the progress
  206. bar.
  207. * **activityIndicatorTheme** - The theme for the activity indicator (spinner),
  208. this can either be a string, in which each character is a different step, or
  209. an array of strings.
  210. * **preSubsection** - Displayed as a separator between the `section` and
  211. `subsection` when the latter is printed.
  212. More generally, themes can have any value that would be a valid value when rendering
  213. templates. The properties in the theme are used when their name matches a type in
  214. the template. Their values can be:
  215. * **strings & numbers** - They'll be included as is
  216. * **function (values, theme, width)** - Should return what you want in your output.
  217. *values* is an object with values provided via `gauge.show`,
  218. *theme* is the theme specific to this item (see below) or this theme object,
  219. and *width* is the number of characters wide your result should be.
  220. There are a couple of special prefixes:
  221. * **pre** - Is shown prior to the property, if its displayed.
  222. * **post** - Is shown after the property, if its displayed.
  223. And one special suffix:
  224. * **Theme** - Its value is passed to a function-type item as the theme.
  225. #### themes.addToAllThemes(theme)
  226. This *mixes-in* `theme` into all themes currently defined. It also adds it
  227. to the default parent theme for this themeset, so future themes added to
  228. this themeset will get the values from `theme` by default.
  229. #### themes.newThemeset()
  230. Copy the current themeset into a new one. This allows you to easily inherit
  231. one themeset from another.
  232. ### TEMPLATES
  233. A template is an array of objects and strings that, after being evaluated,
  234. will be turned into the gauge line. The default template is:
  235. ```javascript
  236. [
  237. {type: 'progressbar', length: 20},
  238. {type: 'activityIndicator', kerning: 1, length: 1},
  239. {type: 'section', kerning: 1, default: ''},
  240. {type: 'subsection', kerning: 1, default: ''}
  241. ]
  242. ```
  243. The various template elements can either be **plain strings**, in which case they will
  244. be be included verbatum in the output, or objects with the following properties:
  245. * *type* can be any of the following plus any keys you pass into `gauge.show` plus
  246. any keys you have on a custom theme.
  247. * `section` – What big thing you're working on now.
  248. * `subsection` – What component of that thing is currently working.
  249. * `activityIndicator` – Shows a spinner using the `activityIndicatorTheme`
  250. from your active theme.
  251. * `progressbar` – A progress bar representing your current `completed`
  252. using the `progressbarTheme` from your active theme.
  253. * *kerning* – Number of spaces that must be between this item and other
  254. items, if this item is displayed at all.
  255. * *maxLength* – The maximum length for this element. If its value is longer it
  256. will be truncated.
  257. * *minLength* – The minimum length for this element. If its value is shorter it
  258. will be padded according to the *align* value.
  259. * *align* – (Default: left) Possible values "left", "right" and "center". Works
  260. as you'd expect from word processors.
  261. * *length* – Provides a single value for both *minLength* and *maxLength*. If both
  262. *length* and *minLength or *maxLength* are specifed then the latter take precedence.
  263. * *value* – A literal value to use for this template item.
  264. * *default* – A default value to use for this template item if a value
  265. wasn't otherwise passed in.
  266. ### PLUMBING
  267. This is the super simple, assume nothing, do no magic internals used by gauge to
  268. implement its ordinary interface.
  269. ```
  270. var Plumbing = require('gauge/plumbing')
  271. var gauge = new Plumbing(theme, template, width)
  272. ```
  273. * **theme**: The theme to use.
  274. * **template**: The template to use.
  275. * **width**: How wide your gauge should be
  276. #### `gauge.setTheme(theme)`
  277. Change the active theme.
  278. #### `gauge.setTemplate(template)`
  279. Change the active template.
  280. #### `gauge.setWidth(width)`
  281. Change the width to render at.
  282. #### `gauge.hide()`
  283. Return the string necessary to hide the progress bar
  284. #### `gauge.hideCursor()`
  285. Return a string to hide the cursor.
  286. #### `gauge.showCursor()`
  287. Return a string to show the cursor.
  288. #### `gauge.show(status)`
  289. Using `status` for values, render the provided template with the theme and return
  290. a string that is suitable for printing to update the gauge.