Skip to content

ProgressBar is not rendered  #170

Closed
Closed

Description

@fxlemire

progress: ^2.0.0
node: 8.9.1
OS: Windows 10
terminal: git bash 2.15.1

This is an algorithm for today's advent of code. I thought it would be fun to add a progress bar since it's quite long to run (it has to iterate 40 mil times).

But the progress bar just won't work. It works on a very small set or any of your examples, but not with the real data. Below code is in typescript:

import * as ProgressBar from 'progress';

const getNextValue = (n: number, generator: 'A' | 'B'): number => {
  const factor = generator === 'A' ? 16807 : 48271;

  return n * factor % 2147483647;
};

const getTotalMatches = (rounds = 40000000, generatorA = 512, generatorB = 191): number => {
  const bar = new ProgressBar('[:bar] :percent :etas', {
    complete: '=',
    incomplete: ' ',
    total: rounds,
    width: 10,
  });
  let r = rounds;
  let nextA = generatorA;
  let nextB = generatorB;
  let totalMatches = 0;

  while (r-- > 0) {
    nextA = getNextValue(nextA, 'A');
    nextB = getNextValue(nextB, 'B');
    const nextABin = nextA.toString(2).split('').reverse().join('');
    const nextBBin = nextB.toString(2).split('').reverse().join('');

    if (nextABin.startsWith(nextBBin.substr(0, 16))) {
      ++totalMatches;
    }

    bar.tick();
  }

  return totalMatches;
};

const downloadExample = () => {
  const contentLength = 128 * 1024;

  const bar = new ProgressBar('[:bar] :percent :etas', {
    complete: '=' ,
    incomplete: ' ',
    total: contentLength,
    width: 20,
  });

  (function next() {
    if (contentLength) {
      const chunk = Math.random() * 10 * 1024;
      bar.tick(chunk);

      if (!bar.complete) {
        setTimeout(next, Math.random() * 1000);
      }
    }
  })();
};

// console.log(`Test should be 1: ${getTotalMatches(5, 65, 8921)}`); // ProgressBar displayed
// console.log(`Test should be 588: ${getTotalMatches(40000000, 65, 8921)}`); // ProgressBar not displayed
// console.log(`Result: ${getTotalMatches()}`); // ProgressBar not displayed
// console.log(downloadExample()); // ProgressBar displayed

I tried creating the ProgressBar with some ratio by basically doing total: rounds > 10 ? 10 : rounds, and surrounding the bar.tick() in if (rounds < 10 || r % (rounds / 10) === 0), but it doesn't work either.

Any cue? I might be blind or something...

Activity

  1. westwood846 commented on Jan 8, 2018

    @westwood846
    Contributor

    I also had problems with the progress bar not rendering so I downgraded to 1.1.8 (npm install --save [email protected]) and it's working. Didn't even have to change my code...

  2. westwood846 commented on Mar 22, 2018

    @westwood846
    Contributor

    I managed to nail the problem down to synchronous iteration:

    var ProgressBar = require('../'); // Assuming this file is in the examples dir
    
    var len = Infinity;
    var bar = new ProgressBar('[:bar]', len);
    
    for (var i = 0; i <= len; i++) {
      bar.tick();
      if (i % 100000000 === 0) console.log(i);
    }

    In the example above, the bar is never drawn. When using some high value for len instead of Infinity, the bar eventually shows up, after the iteration is done.

    @thebigredgeek if I submit a pull request, will you merge it?

  3. westwood846 commented on Mar 22, 2018

    @westwood846
    Contributor

    In any case, there you go. Would be great if you could merge this. Let me know if the PR needs any additional changes.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions