1 | gauge
|
2 | =====
|
3 |
|
4 | A nearly stateless terminal based horizontal gauge / progress bar.
|
5 |
|
6 | ```javascript
|
7 | var Gauge = require("gauge")
|
8 |
|
9 | var gauge = new Gauge()
|
10 |
|
11 | gauge.show("working…", 0)
|
12 | setTimeout(() => { gauge.pulse(); gauge.show("working…", 0.25) }, 500)
|
13 | setTimeout(() => { gauge.pulse(); gauge.show("working…", 0.50) }, 1000)
|
14 | setTimeout(() => { gauge.pulse(); gauge.show("working…", 0.75) }, 1500)
|
15 | setTimeout(() => { gauge.pulse(); gauge.show("working…", 0.99) }, 2000)
|
16 | setTimeout(() => gauge.hide(), 2300)
|
17 | ```
|
18 |
|
19 | See also the [demos](demo.js):
|
20 |
|
21 | ![](./docs/gauge-demo.gif)
|
22 |
|
23 |
|
24 | ### CHANGES FROM 1.x
|
25 |
|
26 | Gauge 2.x is breaking release, please see the [changelog] for details on
|
27 | what's changed if you were previously a user of this module.
|
28 |
|
29 | [changelog]: CHANGELOG.md
|
30 |
|
31 | ### THE GAUGE CLASS
|
32 |
|
33 | This is the typical interface to the module– it provides a pretty
|
34 | fire-and-forget interface to displaying your status information.
|
35 |
|
36 | ```
|
37 | var Gauge = require("gauge")
|
38 |
|
39 | var 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 |
|
47 | Constructs a new gauge. Gauges are drawn on a single line, and are not drawn
|
48 | if **stream** isn't a tty and a tty isn't explicitly provided.
|
49 |
|
50 | If **stream** is a terminal or if you pass in **tty** to **options** then we
|
51 | will 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
|
53 | to 2.5.0, we watch for them on stdout if the tty is stderr.) Resizes to
|
54 | larger window sizes will be clean, but shrinking the window will always
|
55 | result in some cruft.
|
56 |
|
57 | **IMPORTANT:** If you previously were passing in a non-tty stream but you still
|
58 | want output (for example, a stream wrapped by the `ansi` module) then you
|
59 | need to pass in the **tty** option below, as `gauge` needs access to
|
60 | the underlying tty in order to do things like terminal resizes and terminal
|
61 | width detection.
|
62 |
|
63 | The **options** object can have the following properties, all of which are
|
64 | optional:
|
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 |
|
113 | The first argument is either the section, the name of the current thing
|
114 | contributing to progress, or an object with keys like **section**,
|
115 | **subsection** & **completed** (or any others you have types for in a custom
|
116 | template). If you don't want to update or set any of these you can pass
|
117 | `null` and it will be ignored.
|
118 |
|
119 | The second argument is the percent completed as a value between 0 and 1.
|
120 | Without it, completion is just not updated. You'll also note that completion
|
121 | can be passed in as part of a status object as the first argument. If both
|
122 | it and the completed argument are passed in, the completed argument wins.
|
123 |
|
124 | #### `gauge.hide([cb])`
|
125 |
|
126 | Removes the gauge from the terminal. Optionally, callback `cb` after IO has
|
127 | had an opportunity to happen (currently this just means after `setImmediate`
|
128 | has called back.)
|
129 |
|
130 | It turns out this is important when you're pausing the progress bar on one
|
131 | filehandle and printing to another– otherwise (with a big enough print) node
|
132 | can end up printing the "end progress bar" bits to the progress bar filehandle
|
133 | while other stuff is printing to another filehandle. These getting interleaved
|
134 | can cause corruption in some terminals.
|
135 |
|
136 | #### `gauge.pulse([subsection])`
|
137 |
|
138 | * **subsection** – *(optional)* The specific thing that triggered this pulse
|
139 |
|
140 | Spins the spinner in the gauge to show output. If **subsection** is
|
141 | included then it will be combined with the last name passed to `gauge.show`.
|
142 |
|
143 | #### `gauge.disable()`
|
144 |
|
145 | Hides the gauge and ignores further calls to `show` or `pulse`.
|
146 |
|
147 | #### `gauge.enable()`
|
148 |
|
149 | Shows the gauge and resumes updating when `show` or `pulse` is called.
|
150 |
|
151 | #### `gauge.isEnabled()`
|
152 |
|
153 | Returns true if the gauge is enabled.
|
154 |
|
155 | #### `gauge.setThemeset(themes)`
|
156 |
|
157 | Change the themeset to select a theme from. The same as the `themes` option
|
158 | used in the constructor. The theme will be reselected from this themeset.
|
159 |
|
160 | #### `gauge.setTheme(theme)`
|
161 |
|
162 | Change 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 |
|
171 | If no theme is selected then a default is picked using a combination of our
|
172 | best guesses at your OS, color support and unicode support.
|
173 |
|
174 | #### `gauge.setTemplate(template)`
|
175 |
|
176 | Change the active template, will be displayed with the next show or pulse
|
177 |
|
178 | ### Tracking Completion
|
179 |
|
180 | If you have more than one thing going on that you want to track completion
|
181 | of, you may find the related [are-we-there-yet] helpful. It's `change`
|
182 | event can be wired up to the `show` method to get a more traditional
|
183 | progress bar interface.
|
184 |
|
185 | [are-we-there-yet]: https://www.npmjs.com/package/are-we-there-yet
|
186 |
|
187 | ### THEMES
|
188 |
|
189 | ```
|
190 | var themes = require('gauge/themes')
|
191 |
|
192 | // fetch the default color unicode theme for this platform
|
193 | var ourTheme = themes({hasUnicode: true, hasColor: true})
|
194 |
|
195 | // fetch the default non-color unicode theme for osx
|
196 | var 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
|
200 | var ourTheme = themes.newTheme(themes({hasUnicode: false, hasColor: true}), {
|
201 | preProgressbar: '→',
|
202 | postProgressbar: '←'
|
203 | })
|
204 | ```
|
205 |
|
206 | The object returned by `gauge/themes` is an instance of the `ThemeSet` class.
|
207 |
|
208 | ```
|
209 | var ThemeSet = require('gauge/theme-set')
|
210 | var themes = new ThemeSet()
|
211 | // or
|
212 | var themes = require('gauge/themes')
|
213 | var mythemes = themes.newThemeSet() // creates a new themeset based on the default themes
|
214 | ```
|
215 |
|
216 | #### themes(opts)
|
217 | #### themes.getDefault(opts)
|
218 |
|
219 | Theme objects are a function that fetches the default theme based on
|
220 | platform, unicode and color support.
|
221 |
|
222 | Options 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 |
|
231 | If 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 |
|
237 | Adds a named theme to the themeset. You can pass in either a theme object,
|
238 | as returned by `themes.newTheme` or the arguments you'd pass to
|
239 | `themes.newTheme`.
|
240 |
|
241 | #### themes.getThemeNames()
|
242 |
|
243 | Return a list of all of the names of the themes in this themeset. Suitable
|
244 | for use in `themes.getTheme(…)`.
|
245 |
|
246 | #### themes.getTheme(name)
|
247 |
|
248 | Returns the theme object from this theme set named `name`.
|
249 |
|
250 | If `name` does not exist in this themeset an error will be thrown with
|
251 | a `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
|
266 | this set of `opts`.
|
267 |
|
268 | #### themes.newTheme([parentTheme,] newTheme)
|
269 |
|
270 | Create a new theme object based on `parentTheme`. If no `parentTheme` is
|
271 | provided then a minimal parentTheme that defines functions for rendering the
|
272 | activity indicator (spinner) and progress bar will be defined. (This
|
273 | fallback parent is defined in `gauge/base-theme`.)
|
274 |
|
275 | newTheme should be a bare object– we'll start by discussing the properties
|
276 | defined 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 |
|
292 | More generally, themes can have any value that would be a valid value when rendering
|
293 | templates. The properties in the theme are used when their name matches a type in
|
294 | the 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 |
|
302 | There 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 |
|
307 | And one special suffix:
|
308 |
|
309 | * **Theme** - Its value is passed to a function-type item as the theme.
|
310 |
|
311 | #### themes.addToAllThemes(theme)
|
312 |
|
313 | This *mixes-in* `theme` into all themes currently defined. It also adds it
|
314 | to the default parent theme for this themeset, so future themes added to
|
315 | this themeset will get the values from `theme` by default.
|
316 |
|
317 | #### themes.newThemeSet()
|
318 |
|
319 | Copy the current themeset into a new one. This allows you to easily inherit
|
320 | one themeset from another.
|
321 |
|
322 | ### TEMPLATES
|
323 |
|
324 | A template is an array of objects and strings that, after being evaluated,
|
325 | will 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 |
|
336 | The various template elements can either be **plain strings**, in which case they will
|
337 | be 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 |
|
363 | This is the super simple, assume nothing, do no magic internals used by gauge to
|
364 | implement its ordinary interface.
|
365 |
|
366 | ```
|
367 | var Plumbing = require('gauge/plumbing')
|
368 | var 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 |
|
377 | Change the active theme.
|
378 |
|
379 | #### `gauge.setTemplate(template)`
|
380 |
|
381 | Change the active template.
|
382 |
|
383 | #### `gauge.setWidth(width)`
|
384 |
|
385 | Change the width to render at.
|
386 |
|
387 | #### `gauge.hide()`
|
388 |
|
389 | Return the string necessary to hide the progress bar
|
390 |
|
391 | #### `gauge.hideCursor()`
|
392 |
|
393 | Return a string to hide the cursor.
|
394 |
|
395 | #### `gauge.showCursor()`
|
396 |
|
397 | Return a string to show the cursor.
|
398 |
|
399 | #### `gauge.show(status)`
|
400 |
|
401 | Using `status` for values, render the provided template with the theme and return
|
402 | a string that is suitable for printing to update the gauge.
|