• Stars
    star
    319
  • Rank 131,491 (Top 3 %)
  • Language
    JavaScript
  • License
    Other
  • Created almost 10 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A terminal based horizontal guage aka, a progress bar

gauge

A nearly stateless terminal based horizontal gauge / progress bar.

var Gauge = require("gauge")

var gauge = new Gauge()

gauge.show("working…", 0)
setTimeout(() => { gauge.pulse(); gauge.show("working…", 0.25) }, 500)
setTimeout(() => { gauge.pulse(); gauge.show("working…", 0.50) }, 1000)
setTimeout(() => { gauge.pulse(); gauge.show("working…", 0.75) }, 1500)
setTimeout(() => { gauge.pulse(); gauge.show("working…", 0.99) }, 2000)
setTimeout(() => gauge.hide(), 2300)

See also the demos:

CHANGES FROM 1.x

Gauge 2.x is breaking release, please see the changelog for details on what's changed if you were previously a user of this module.

THE GAUGE CLASS

This is the typical interface to the module– it provides a pretty fire-and-forget interface to displaying your status information.

var Gauge = require("gauge")

var gauge = new Gauge([stream], [options])
  • stream(optional, default STDERR) A stream that progress bar updates are to be written to. Gauge honors backpressure and will pause most writing if it is indicated.
  • options(optional) An option object.

Constructs a new gauge. Gauges are drawn on a single line, and are not drawn if stream isn't a tty and a tty isn't explicitly provided.

If stream is a terminal or if you pass in tty to options then we will detect terminal resizes and redraw to fit. We do this by watching for resize events on the tty. (To work around a bug in versions of Node prior to 2.5.0, we watch for them on stdout if the tty is stderr.) Resizes to larger window sizes will be clean, but shrinking the window will always result in some cruft.

IMPORTANT: If you previously were passing in a non-tty stream but you still want output (for example, a stream wrapped by the ansi module) then you need to pass in the tty option below, as gauge needs access to the underlying tty in order to do things like terminal resizes and terminal width detection.

The options object can have the following properties, all of which are optional:

  • updateInterval: How often gauge updates should be drawn, in milliseconds.

  • fixedFramerate: Defaults to false on node 0.8, true on everything else. When this is true a timer is created to trigger once every updateInterval ms, when false, updates are printed as soon as they come in but updates more often than updateInterval are ignored. The reason 0.8 doesn't have this set to true is that it can't unref its timer and so it would stop your program from exiting– if you want to use this feature with 0.8 just make sure you call gauge.disable() before you expect your program to exit.

  • themes: A themeset to use when selecting the theme to use. Defaults to gauge/themes, see the themes documentation for details.

  • theme: Select a theme for use, it can be a:

    • Theme object, in which case the themes is not used.
    • The name of a theme, which will be looked up in the current themes object.
    • A configuration object with any of hasUnicode, hasColor or platform keys, which if will be used to override our guesses when making a default theme selection.

    If no theme is selected then a default is picked using a combination of our best guesses at your OS, color support and unicode support.

  • template: Describes what you want your gauge to look like. The default is what npm uses. Detailed documentation is later in this document.

  • hideCursor: Defaults to true. If true, then the cursor will be hidden while the gauge is displayed.

  • tty: The tty that you're ultimately writing to. Defaults to the same as stream. This is used for detecting the width of the terminal and resizes. The width used is tty.columns - 1. If no tty is available then a width of 79 is assumed.

  • enabled: Defaults to true if tty is a TTY, false otherwise. If true the gauge starts enabled. If disabled then all update commands are ignored and no gauge will be printed until you call .enable().

  • Plumbing: The class to use to actually generate the gauge for printing. This defaults to require('gauge/plumbing') and ordinarily you shouldn't need to override this.

  • cleanupOnExit: Defaults to true. Ordinarily we register an exit handler to make sure your cursor is turned back on and the progress bar erased when your process exits, even if you Ctrl-C out or otherwise exit unexpectedly. You can disable this and it won't register the exit handler.

gauge.show(section | status, [completed])

The first argument is either the section, the name of the current thing contributing to progress, or an object with keys like section, subsection & completed (or any others you have types for in a custom template). If you don't want to update or set any of these you can pass null and it will be ignored.

The second argument is the percent completed as a value between 0 and 1. Without it, completion is just not updated. You'll also note that completion can be passed in as part of a status object as the first argument. If both it and the completed argument are passed in, the completed argument wins.

gauge.hide([cb])

Removes the gauge from the terminal. Optionally, callback cb after IO has had an opportunity to happen (currently this just means after setImmediate has called back.)

It turns out this is important when you're pausing the progress bar on one filehandle and printing to another– otherwise (with a big enough print) node can end up printing the "end progress bar" bits to the progress bar filehandle while other stuff is printing to another filehandle. These getting interleaved can cause corruption in some terminals.

gauge.pulse([subsection])

  • subsection(optional) The specific thing that triggered this pulse

Spins the spinner in the gauge to show output. If subsection is included then it will be combined with the last name passed to gauge.show.

gauge.disable()

Hides the gauge and ignores further calls to show or pulse.

gauge.enable()

Shows the gauge and resumes updating when show or pulse is called.

gauge.isEnabled()

Returns true if the gauge is enabled.

gauge.setThemeset(themes)

Change the themeset to select a theme from. The same as the themes option used in the constructor. The theme will be reselected from this themeset.

gauge.setTheme(theme)

Change the active theme, will be displayed with the next show or pulse. This can be:

  • Theme object, in which case the themes is not used.
  • The name of a theme, which will be looked up in the current themes object.
  • A configuration object with any of hasUnicode, hasColor or platform keys, which if will be used to override our guesses when making a default theme selection.

If no theme is selected then a default is picked using a combination of our best guesses at your OS, color support and unicode support.

gauge.setTemplate(template)

Change the active template, will be displayed with the next show or pulse

Tracking Completion

If you have more than one thing going on that you want to track completion of, you may find the related are-we-there-yet helpful. It's change event can be wired up to the show method to get a more traditional progress bar interface.

THEMES

var themes = require('gauge/themes')

// fetch the default color unicode theme for this platform
var ourTheme = themes({hasUnicode: true, hasColor: true})

// fetch the default non-color unicode theme for osx
var ourTheme = themes({hasUnicode: true, hasColor: false, platform: 'darwin'})

// create a new theme based on the color ascii theme for this platform
// that brackets the progress bar with arrows
var ourTheme = themes.newTheme(themes({hasUnicode: false, hasColor: true}), {
  preProgressbar: '→',
  postProgressbar: '←'
})

The object returned by gauge/themes is an instance of the ThemeSet class.

var ThemeSet = require('gauge/theme-set')
var themes = new ThemeSet()
// or
var themes = require('gauge/themes')
var mythemes = themes.newThemeSet() // creates a new themeset based on the default themes

themes(opts)

themes.getDefault(opts)

Theme objects are a function that fetches the default theme based on platform, unicode and color support.

Options is an object with the following properties:

  • hasUnicode - If true, fetch a unicode theme, if no unicode theme is available then a non-unicode theme will be used.
  • hasColor - If true, fetch a color theme, if no color theme is available a non-color theme will be used.
  • platform (optional) - Defaults to process.platform. If no platform match is available then fallback is used instead.

If no compatible theme can be found then an error will be thrown with a code of EMISSINGTHEME.

themes.addTheme(themeName, themeObj)

themes.addTheme(themeName, [parentTheme], newTheme)

Adds a named theme to the themeset. You can pass in either a theme object, as returned by themes.newTheme or the arguments you'd pass to themes.newTheme.

themes.getThemeNames()

Return a list of all of the names of the themes in this themeset. Suitable for use in themes.getTheme(…).

themes.getTheme(name)

Returns the theme object from this theme set named name.

If name does not exist in this themeset an error will be thrown with a code of EMISSINGTHEME.

themes.setDefault([opts], themeName)

opts is an object with the following properties.

  • platform - Defaults to 'fallback'. If your theme is platform specific, specify that here with the platform from process.platform, eg, win32, darwin, etc.
  • hasUnicode - Defaults to false. If your theme uses unicode you should set this to true.
  • hasColor - Defaults to false. If your theme uses color you should set this to true.

themeName is the name of the theme (as given to addTheme) to use for this set of opts.

themes.newTheme([parentTheme,] newTheme)

Create a new theme object based on parentTheme. If no parentTheme is provided then a minimal parentTheme that defines functions for rendering the activity indicator (spinner) and progress bar will be defined. (This fallback parent is defined in gauge/base-theme.)

newTheme should be a bare object– we'll start by discussing the properties defined by the default themes:

  • preProgressbar - displayed prior to the progress bar, if the progress bar is displayed.
  • postProgressbar - displayed after the progress bar, if the progress bar is displayed.
  • progressBarTheme - The subtheme passed through to the progress bar renderer, it's an object with complete and remaining properties that are the strings you want repeated for those sections of the progress bar.
  • activityIndicatorTheme - The theme for the activity indicator (spinner), this can either be a string, in which each character is a different step, or an array of strings.
  • preSubsection - Displayed as a separator between the section and subsection when the latter is printed.

More generally, themes can have any value that would be a valid value when rendering templates. The properties in the theme are used when their name matches a type in the template. Their values can be:

  • strings & numbers - They'll be included as is
  • function (values, theme, width) - Should return what you want in your output. values is an object with values provided via gauge.show, theme is the theme specific to this item (see below) or this theme object, and width is the number of characters wide your result should be.

There are a couple of special prefixes:

  • pre - Is shown prior to the property, if its displayed.
  • post - Is shown after the property, if its displayed.

And one special suffix:

  • Theme - Its value is passed to a function-type item as the theme.

themes.addToAllThemes(theme)

This mixes-in theme into all themes currently defined. It also adds it to the default parent theme for this themeset, so future themes added to this themeset will get the values from theme by default.

themes.newThemeSet()

Copy the current themeset into a new one. This allows you to easily inherit one themeset from another.

TEMPLATES

A template is an array of objects and strings that, after being evaluated, will be turned into the gauge line. The default template is:

[
    {type: 'progressbar', length: 20},
    {type: 'activityIndicator', kerning: 1, length: 1},
    {type: 'section', kerning: 1, default: ''},
    {type: 'subsection', kerning: 1, default: ''}
]

The various template elements can either be plain strings, in which case they will be be included verbatum in the output, or objects with the following properties:

  • type can be any of the following plus any keys you pass into gauge.show plus any keys you have on a custom theme.
    • section – What big thing you're working on now.
    • subsection – What component of that thing is currently working.
    • activityIndicator – Shows a spinner using the activityIndicatorTheme from your active theme.
    • progressbar – A progress bar representing your current completed using the progressbarTheme from your active theme.
  • kerning – Number of spaces that must be between this item and other items, if this item is displayed at all.
  • maxLength – The maximum length for this element. If its value is longer it will be truncated.
  • minLength – The minimum length for this element. If its value is shorter it will be padded according to the align value.
  • align – (Default: left) Possible values "left", "right" and "center". Works as you'd expect from word processors.
  • length – Provides a single value for both minLength and maxLength. If both length and *minLength or maxLength are specified then the latter take precedence.
  • value – A literal value to use for this template item.
  • default – A default value to use for this template item if a value wasn't otherwise passed in.

PLUMBING

This is the super simple, assume nothing, do no magic internals used by gauge to implement its ordinary interface.

var Plumbing = require('gauge/plumbing')
var gauge = new Plumbing(theme, template, width)
  • theme: The theme to use.
  • template: The template to use.
  • width: How wide your gauge should be

gauge.setTheme(theme)

Change the active theme.

gauge.setTemplate(template)

Change the active template.

gauge.setWidth(width)

Change the width to render at.

gauge.hide()

Return the string necessary to hide the progress bar

gauge.hideCursor()

Return a string to hide the cursor.

gauge.showCursor()

Return a string to show the cursor.

gauge.show(status)

Using status for values, render the provided template with the theme and return a string that is suitable for printing to update the gauge.

More Repositories

1

npm

This repository is moving to: https://github.com/npm/cli
17,473
star
2

cli

the package manager for JavaScript
JavaScript
8,303
star
3

node-semver

The semver parser for node (the one npm uses)
JavaScript
4,772
star
4

npm-expansions

Send us a pull request by editing expansions.txt
JavaScript
2,209
star
5

tink

a dependency unwinder for javascript
JavaScript
2,153
star
6

ini

An ini parser/serializer in JavaScript
JavaScript
733
star
7

npx

npm package executor
JavaScript
731
star
8

rfcs

Public change requests/proposals & ideation
JavaScript
726
star
9

npm-registry-couchapp

couchapp bits of registry.npmjs.org
JavaScript
615
star
10

nopt

Node/npm Option Parsing
JavaScript
527
star
11

registry

npm registry documentation
449
star
12

npmlog

The logger that npm uses
JavaScript
423
star
13

marky-markdown

npm's markdown parser
JavaScript
405
star
14

arborist

npm's tree doctor
JavaScript
369
star
15

documentation

Documentation for the npm registry, website, and command-line interface.
MDX
369
star
16

pacote

npm fetcher
JavaScript
349
star
17

download-counts

Background jobs and a minimal service for collecting and delivering download counts
JavaScript
328
star
18

node-which

Like which(1) unix command. Find the first instance of an executable in the PATH.
JavaScript
305
star
19

init-package-json

A node module to get your node module started
JavaScript
284
star
20

validate-npm-package-name

Is the given string an acceptable npm package name?
JavaScript
282
star
21

cacache

npm's content-addressable cache
JavaScript
274
star
22

npm-merge-driver

git merge driver for resolving conflicts in npm-related files
JavaScript
271
star
23

npm-registry-client

JavaScript
264
star
24

lockfile

A very polite lock file utility, which endeavors to not litter, and to wait patiently for others.
JavaScript
259
star
25

registry-issue-archive

An archive of the old npm registry issue tracker
250
star
26

write-file-atomic

Write files in an atomic fashion w/configurable ownership
JavaScript
217
star
27

read-package-json

The thing npm uses to read package.json files with semantics and defaults and validation and stuff
JavaScript
214
star
28

roadmap

Public roadmap for npm
213
star
29

hosted-git-info

Provides metadata and conversions from repository urls for Github, Bitbucket and Gitlab
JavaScript
206
star
30

fstream

Advanced FS Streaming for Node
JavaScript
205
star
31

make-fetch-happen

making fetch happen for npm
JavaScript
190
star
32

read

read(1) for node.
JavaScript
187
star
33

normalize-package-data

normalizes package metadata, typically found in package.json file.
JavaScript
184
star
34

ndm

ndm allows you to deploy OS-specific service-wrappers directly from npm-packages.
JavaScript
181
star
35

are-we-there-yet

Track complex hiearchies of asynchronous task completion statuses.
JavaScript
173
star
36

abbrev-js

Like ruby's Abbrev module
JavaScript
158
star
37

statusboard

Public monitor/status/health board for @npm/cli-team's maintained projects
JavaScript
152
star
38

security-holder

An npm package that holds a spot.
145
star
39

osenv

Look up environment settings specific to different operating systems.
JavaScript
137
star
40

feedback

Public feedback discussions for npm
133
star
41

npm-registry-fetch

like fetch() but for the npm registry
JavaScript
130
star
42

npm-package-arg

Parse the things that can be arguments to `npm install`
JavaScript
116
star
43

libnpm

programmatic npm API
JavaScript
113
star
44

npm-collection-staff-picks

JavaScript
112
star
45

npm-packlist

Walk through a folder and figure out what goes in an npm package
JavaScript
110
star
46

promzard

A prompting json thingie
JavaScript
101
star
47

npm-remote-ls

Examine a package's dependency graph before you install it
JavaScript
89
star
48

npmconf

npm config thing
JavaScript
75
star
49

cmd-shim

The cmd-shim used in npm
JavaScript
75
star
50

npm-tips

A collection of short (5 words or so) tips and tricks that can be sprinkled about the npm site.
JavaScript
73
star
51

www

community space for the npm website
68
star
52

policies

Privacy policy, code of conduct, license, and other npm legal stuff
Shell
67
star
53

package-json

Programmatic API to update package.json
JavaScript
65
star
54

git

a util for spawning git from npm CLI contexts
JavaScript
62
star
55

npm_conf

A conference about npm, maybe. Not to be confused with npmconf.
59
star
56

ignore-walk

Nested/recursive `.gitignore`/`.npmignore` parsing and filtering.
JavaScript
57
star
57

registry-follower-tutorial

write you a registry follower for great good
JavaScript
56
star
58

ssri

subresource integrity for npm
JavaScript
55
star
59

minipass-fetch

An implementation of window.fetch in Node.js using Minipass streams
JavaScript
54
star
60

ci-detect

Detect what kind of CI environment the program is in
JavaScript
53
star
61

run-script

Run a lifecycle script for a package (descendant of npm-lifecycle)
JavaScript
53
star
62

read-installed

Read all the installed packages in a folder, and return a tree structure with all the data.
JavaScript
52
star
63

mute-stream

Bytes go in, but they don't come out (when muted).
JavaScript
49
star
64

fs-write-stream-atomic

Like `fs.createWriteStream(...)`, but atomic.
JavaScript
48
star
65

read-package-json-fast

Like read-package-json, but faster
JavaScript
48
star
66

libnpmpublish

programmatically publish and unpublish npm packages
JavaScript
46
star
67

logical-tree

Calculates a nested logical tree using a package.json and a package lock.
JavaScript
44
star
68

read-package-tree

Read the contents of node_modules
JavaScript
42
star
69

jobs

41
star
70

unique-filename

Generate a unique filename for use in temporary directories or caches.
JavaScript
40
star
71

lock-verify

Report if your package.json is out of sync with your package-lock.json
JavaScript
38
star
72

fstream-ignore

JavaScript
37
star
73

benchmarks

The npm CLI's benchmark suite
JavaScript
36
star
74

npm-lifecycle

npm lifecycle script runner
JavaScript
36
star
75

wombat-cli

The wombat cli tool.
JavaScript
35
star
76

npme-installer

npm Enterprise installer
JavaScript
35
star
77

couch-login

A module for doing logged-in requests against a couchdb server
JavaScript
33
star
78

libnpmexec

npm exec (npx) Programmatic API
JavaScript
33
star
79

npm-audit-report

npm audit security report
JavaScript
33
star
80

ansible-nagios

Ansible role for building Nagios 4.
Perl
32
star
81

config

Configuration management for https://github.com/npm/cli
JavaScript
32
star
82

parse-conflict-json

Parse a JSON string that has git merge conflicts, resolving if possible
JavaScript
32
star
83

npm-profile

Make changes to your npmjs.com profile via cli or library
JavaScript
31
star
84

unique-slug

Generate a unique character string suitible for use in files and URLs.
JavaScript
31
star
85

fstream-npm

fstream class for creating npm packages
JavaScript
30
star
86

redsess

Yet another redis session thing for node.
JavaScript
30
star
87

concurrent-couch-follower

a couch follower wrapper that you can use to be sure you don't miss any documents even if you process them asynchronously.
JavaScript
28
star
88

npm-registry-mock

mock the npm registry
JavaScript
27
star
89

lint

lint the npmcli way
JavaScript
26
star
90

fs

filesystem helper functions, wrappers, and promisification for the npm cli
JavaScript
25
star
91

template-oss

a template package for npm CLI team development
JavaScript
25
star
92

libnpmsearch

programmatic API for the shiny new npm search endpoint
JavaScript
25
star
93

bin-links

.bin/ script linker
JavaScript
24
star
94

libnpmaccess

programmatic api for `npm access`
JavaScript
24
star
95

logos

official logos for npm, Inc
22
star
96

types

Typescript definitions for npm registry content
JavaScript
22
star
97

public-api

21
star
98

deprecate-holder

An npm package that holds a spot.
21
star
99

map-workspaces

Retrieves a name:pathname Map for a given workspaces config
JavaScript
20
star
100

libnpmversion

library to do the things that 'npm version' does
JavaScript
20
star