UNPKG

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