Skip to content

The type of value and total in template function should be number #16

Description

@zcbenz

When passing a template function to BarItem, users usually do some really complicated formatting, it would be handy if the value and total could be passed as numbers.

    bar.add(new BarItem(progress, {
      options: Object.assign(presets.shades, {
        width: 15,
      }),
      template: ({bar, value, total, etaHumanReadable}) => {
        const valueNumber = parseInt(value);
        const totalNumber = parseInt(total);
        const name = (progress.getPayload() as any).name as string;
        const isFinished = valueNumber >= totalNumber;
        const eta = isFinished ? '' : ` | ETA: ${etaHumanReadable}`;
        const bytesOptions = {
          // Avoid jumping cursors around numbers like 1.9MB and 2MB.
          minimumFractionDigits: valueNumber > 1024 * 1024 ? 1 : 0,
          // Keep numbers compact.
          maximumFractionDigits: 1,
          space: false,
        };
        value = prettyBytes(valueNumber, bytesOptions);
        total = prettyBytes(totalNumber, bytesOptions);
        const percent = isFinished ? total : `${value}/${total}`;
        return `${bar} | ${name} | ${percent}${eta}`;
      },
    }));

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions