ctongfei / progressbar

Terminal-based progress bar for Java / JVM
http://tongfei.me/progressbar/
MIT License
1.08k stars 103 forks source link

Using ProgressBar constructor and building it using ProgressBarBuilder yields different results #163

Open nathanduchesne opened 4 months ago

nathanduchesne commented 4 months ago

I am experiencing different program behaviour when running this code ProgressBar pb = new ProgressBar(name, 100);

or this code:

ProgressBar.builder().setTaskName(progressBarName).setInitialMax(progressBarMaxCapacity).setStyle(ProgressBarStyle.ASCII).build();

What I was expecting: Everything works the same, except case 2 has a different display, since we no longer use Unicode.

What happens: When using the builder, the default PrintStream is not System.err, but instead is

static ConsoleProgressBarConsumer createConsoleConsumer(int predefinedWidth) {
        PrintStream real = new PrintStream(new FileOutputStream(FileDescriptor.err));
        return createConsoleConsumer(real, predefinedWidth);  // System.err might be overridden by System.setErr
    }

(see Utils.java, line 20-23)

Why this may be a problem: My code redirects certain streams (typically System.err) to other streams, and hence uses System.setErr() to redirect it. Because of this inconsistency, I can no longer do so. Additionally, I am uncertain as to what benefits you gain from instantiating the PrintStream the way you do so in the above code, as opposed to using System.err directly.

What can easily be done :)

Would you be open to changing PrintStream real = new PrintStream(new FileOutputStream(FileDescriptor.err)); with PrintStream real = System.err;

This would make the whole code consistent. Let me know what you think and if you want me to make a pull request for this change!

Best, Nathan

ctongfei commented 3 months ago

Thanks Nathan! I think this deliberate choice of instantiating from FileDescriptor.err is to force it to display it on the screen, if stderr is used for something else (which is likely when used within a build system). Maybe we could add a switch somewhere so that the stream can be customized?