4 A nearly stateless terminal based horizontal gauge / progress bar.
7 var Gauge = require("gauge")
9 var gauge = new Gauge()
11 gauge.show("test", 0.20)
21 ### `var gauge = new Gauge([options], [ansiStream])`
23 * **options** – *(optional)* An option object. (See [below] for details.)
24 * **ansiStream** – *(optional)* A stream that's been blessed by the [ansi]
25 module to include various commands for controlling the cursor in a terminal.
27 [ansi]: https://www.npmjs.com/package/ansi
28 [below]: #theme-objects
30 Constructs a new gauge. Gauges are drawn on a single line, and are not drawn
31 if the current terminal isn't a tty.
33 If you resize your terminal in a way that can be detected then the gauge
34 will be drawn at the new size. As a general rule, growing your terminal will
35 be clean, but shrinking your terminal will result in cruft as we don't have
36 enough information to know where what we wrote previously is now located.
38 The **options** object can have the following properties, all of which are
41 * maxUpdateFrequency: defaults to 50 msec, the gauge will not be drawn more
42 than once in this period of time. This applies to `show` and `pulse`
43 calls, but if you `hide` and then `show` the gauge it will draw it
44 regardless of time since last draw.
45 * theme: defaults to Gauge.unicode` if the terminal supports
46 unicode according to [has-unicode], otherwise it defaults to `Gauge.ascii`.
47 Details on the [theme object](#theme-objects) are documented elsewhere.
48 * template: see [documentation elsewhere](#template-objects) for
51 [has-unicode]: https://www.npmjs.com/package/has-unicode
53 If **ansiStream** isn't passed in, then one will be constructed from stderr
54 with `ansi(process.stderr)`.
56 ### `gauge.show([name, [completed]])`
58 * **name** – *(optional)* The name of the current thing contributing to progress. Defaults to the last value used, or "".
59 * **completed** – *(optional)* The portion completed as a value between 0 and 1. Defaults to the last value used, or 0.
61 If `process.stdout.isTTY` is false then this does nothing. If completed is 0
62 and `gauge.pulse` has never been called, then similarly nothing will be printed.
64 If `maxUpdateFrequency` msec haven't passed since the last call to `show` or
65 `pulse` then similarly, nothing will be printed. (Actually, the update is
66 deferred until `maxUpdateFrequency` msec have passed and if nothing else has
67 happened, the gauge update will happen.)
71 Removes the gauge from the terminal.
73 ### `gauge.pulse([name])`
75 * **name** – *(optional)* The specific thing that triggered this pulse
77 Spins the spinner in the gauge to show output. If **name** is included then
78 it will be combined with the last name passed to `gauge.show` using the
79 subsection property of the theme (typically a right facing arrow).
83 Hides the gauge and ignores further calls to `show` or `pulse`.
87 Shows the gauge and resumes updating when `show` or `pulse` is called.
89 ### `gauge.setTheme(theme)`
91 Change the active theme, will be displayed with the next show or pulse
93 ### `gauge.setTemplate(template)`
95 Change the active template, will be displayed with the next show or pulse
99 There are two theme objects available as a part of the module, `Gauge.unicode` and `Gauge.ascii`.
100 Theme objects have the follow properties:
102 | Property | Unicode | ASCII |
103 | ---------- | ------- | ----- |
104 | startgroup | ╢ | \| |
105 | endgroup | ╟ | \| |
107 | incomplete | ░ | - |
108 | spinner | ▀▐▄▌ | -\\\|/ |
109 | subsection | → | -> |
111 *startgroup*, *endgroup* and *subsection* can be as many characters as you want.
113 *complete* and *incomplete* should be a single character width each.
115 *spinner* is a list of characters to use in turn when displaying an activity
116 spinner. The Gauge will spin as many characters as you give here.
120 A template is an array of objects and strings that, after being evaluated,
121 will be turned into the gauge line. The default template is:
125 {type: "name", separated: true, maxLength: 25, minLength: 25, align: "left"},
126 {type: "spinner", separated: true},
127 {type: "startgroup"},
128 {type: "completionbar"},
133 The various template elements can either be **plain strings**, in which case they will
134 be be included verbatum in the output.
136 If the template element is an object, it can have the following keys:
139 * `name` – The most recent name passed to `show`; if this is in response to a
140 `pulse` then the name passed to `pulse` will be appended along with the
141 subsection property from the theme.
142 * `spinner` – If you've ever called `pulse` this will be one of the characters
143 from the spinner property of the theme.
144 * `startgroup` – The `startgroup` property from the theme.
145 * `completionbar` – This progress bar itself
146 * `endgroup` – The `endgroup` property from the theme.
147 * *separated* – If true, the element will be separated with spaces from things on
148 either side (and margins count as space, so it won't be indented), but only
150 * *maxLength* – The maximum length for this element. If its value is longer it
152 * *minLength* – The minimum length for this element. If its value is shorter it
153 will be padded according to the *align* value.
154 * *align* – (Default: left) Possible values "left", "right" and "center". Works
155 as you'd expect from word processors.
156 * *length* – Provides a single value for both *minLength* and *maxLength*. If both
157 *length* and *minLength or *maxLength* are specifed then the latter take precedence.
159 ### Tracking Completion
161 If you have more than one thing going on that you want to track completion
162 of, you may find the related [are-we-there-yet] helpful. It's `change`
163 event can be wired up to the `show` method to get a more traditional
164 progress bar interface.
166 [are-we-there-yet]: https://www.npmjs.com/package/are-we-there-yet