Détail du package

progress-estimator

bvaughn259.2kMIT0.3.1

Animated progress bars with estimated durations

ascii, busy, cli, console

readme

progress-estimator

Logs a progress bar and estimation for how long a Promise will take to complete. This library tracks previous durations in order to provide more accurate estimates over time.

Demo

🎉 Become a sponsor or ☕ Buy me a coffee

Installation

# use npm
npm install progress-estimator

# use yarn
yarn add progress-estimator

Usage example

const createLogger = require('progress-estimator');
const { join } = require('path');

// All configuration keys are optional, but it's recommended to specify a storage location.
// Learn more about configuration options below.
const logger = createLogger({
  storagePath: join(__dirname, '.progress-estimator'),
});

async function run() {
  await logger(promiseOne, "This is a promise");
  await logger(
    promiseTwo,
    "This is another promise. I think it will take about 1 second",
    {
      estimate: 1000
    }
  );
}

API

createLogger(optionalConfiguration)

This method is the default package export. It creates and configures a logger function (documented below). The following configuration options are supported. (They apply only to the logger instance that's returned.)

name type Description
logFunction Function Custom logging function. Defaults to log-update. Must define .done() and .clear() methods.
spinner object Which spinner from the cli-spinners package to use. Defaults to dots.
storagePath string Where to record durations between runs. Defaults to os.tmpdir().
theme object Custom chalk theme. Look to the default theme for a list of required keys.

logger(promise, labelString, options)

This method logs a progress bar and estimated duration for a promise. It requires at least two parameters– a Promise and a label (e.g. "Running tests"). The label is SHA1 hashed in order to uniquely identify the promise.

An optional third parameter can be provided as well with the following keys:

name type Description
estimate Number Estimated duration of promise. (This value is used initially, until a history of actual durations have been recorded.)
id String Uniquely identifies the promise. This value is needed if the label string is not guaranteed to be unique.

changelog

0.3.1

  • Updated TypeScript definitions. (PR 17)

0.2.2

  • Updated README for npmjs.com. (No actual code changes.)

0.2.1

  • Gracefully handle null or undefined options parameter passed to logger function.

0.2.0

  • Configuration function returns a configured logger, enabling custom configurations for multiple logger use cases.
  • Configuration supports optional custom logging function. Default implementation will continue to use the log-update package.
  • Log function replaces third parameter (previously the estimated duration) with an optional configuration object supporting multiple values (estimated duration and unique id).

0.1.3

  • Moved clearInterval into finally block to ensure proper cleanup if a Promise throws.

0.1.2

  • Moved percentage label (e.g. "35%") inside of progress bar to save horizontal space.
  • Changed format of time from estimated duration (e.g. "3.2 sec estimated") to include elapsed time (e.g. "1.5s, estimated 3.2s"). This new label format was inspired by the Jest test runner.
  • Added estimateExceeded key to theme for when elapsed duration exceeds the estimated duration.
  • Added TypeScript type defs to published lib.

0.1.1

  • Fixed conversion bug for hour units.

0.1.0

  • Initial release.