gauge-v2 1.0.0

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