progressbar icon indicating copy to clipboard operation
progressbar copied to clipboard

Duplicate 100% line when using `DelegatingProgressBarConsumer`

Open scordio opened this issue 7 months ago • 1 comments

The following builder configuration seems to produce duplicate 100% lines on version 0.10.1 and 0.9.5:

  ProgressBar progressBar = ProgressBar.builder()
    .setConsumer(new DelegatingProgressBarConsumer(LOGGER::info))
    .setInitialMax(1_000_000_000)
    .build();

Here's a full reproducer with Commons Logging, although the logging framework shouldn't matter:

package com.example;

import me.tongfei.progressbar.DelegatingProgressBarConsumer;
import me.tongfei.progressbar.ProgressBar;
import me.tongfei.progressbar.ProgressBarBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.jupiter.api.Test;

import java.util.Iterator;
import java.util.stream.IntStream;

class ReproducerTest {

  private static final Log LOGGER = LogFactory.getLog(ReproducerTest.class);

  @Test
  void imperative() {
    Iterator<Integer> iterator = IntStream.range(0, 1_000_000_000).iterator();

    try (ProgressBar progressBar = ProgressBar.builder()
      .setConsumer(new DelegatingProgressBarConsumer(LOGGER::info))
      .setInitialMax(1_000_000_000)
      .build()) {

      iterator.forEachRemaining(__ -> progressBar.step());
    }
  }

  @Test
  void declarative() {
    Iterator<Integer> iterator = IntStream.range(0, 1_000_000_000).iterator();

    ProgressBarBuilder builder = ProgressBar.builder()
      .setConsumer(new DelegatingProgressBarConsumer(LOGGER::info))
      .setInitialMax(1_000_000_000);

    ProgressBar.wrap(iterator, builder).forEachRemaining(__ -> {
    });
  }

}

Both tests yield an output like the following:

16:23:20.866 [ProgressBar] INFO com.example.ReproducerTest --    0% │                             │      37658/1000000000 (0:00:00 / 0:00:17) 
16:23:21.853 [ProgressBar] INFO com.example.ReproducerTest --   10% │███                          │  107334738/1000000000 (0:00:01 / 0:00:08) 
16:23:22.856 [ProgressBar] INFO com.example.ReproducerTest --   21% │██████▎                      │  215577615/1000000000 (0:00:02 / 0:00:07) 
16:23:23.855 [ProgressBar] INFO com.example.ReproducerTest --   32% │█████████▍                   │  323598354/1000000000 (0:00:03 / 0:00:06) 
16:23:24.853 [ProgressBar] INFO com.example.ReproducerTest --   42% │████████████▎                │  422840806/1000000000 (0:00:04 / 0:00:05) 
16:23:25.853 [ProgressBar] INFO com.example.ReproducerTest --   53% │███████████████▍             │  532162733/1000000000 (0:00:05 / 0:00:04) 
16:23:26.855 [ProgressBar] INFO com.example.ReproducerTest --   64% │██████████████████▋          │  643504332/1000000000 (0:00:06 / 0:00:03) 
16:23:27.852 [ProgressBar] INFO com.example.ReproducerTest --   75% │█████████████████████▉       │  754526143/1000000000 (0:00:07 / 0:00:02) 
16:23:28.852 [ProgressBar] INFO com.example.ReproducerTest --   84% │████████████████████████▍    │  842560365/1000000000 (0:00:08 / 0:00:01) 
16:23:29.852 [ProgressBar] INFO com.example.ReproducerTest --   95% │███████████████████████████▋ │  953860625/1000000000 (0:00:09 / 0:00:00) 
16:23:30.257 [ProgressBar] INFO com.example.ReproducerTest --  100% │█████████████████████████████│ 1000000000/1000000000 (0:00:09 / 0:00:00) 
16:23:30.258 [ProgressBar] INFO com.example.ReproducerTest --  100% │█████████████████████████████│ 1000000000/1000000000 (0:00:09 / 0:00:00) 

There is no duplicate line on version 0.8.1, so it appears to be a regression.

scordio avatar May 18 '25 14:05 scordio

Hi @holle, I have an idea about the fix (to be reviewed & reconfirmed by @ctongfei).

I'll raise a PR in the upcoming days and let you know, so that you can validate the fix within your use case.

scordio avatar Jun 05 '25 13:06 scordio