Skip to content

Commit 0d3b735

Browse files
committed
[+] PBar: Support non-tty output
1 parent 94c3f10 commit 0d3b735

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/main/java/org/hydev/mcpm/client/interaction/ProgressBar.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import java.util.*;
88

99
import static java.lang.String.format;
10+
import static org.fusesource.jansi.internal.CLibrary.STDOUT_FILENO;
11+
import static org.fusesource.jansi.internal.CLibrary.isatty;
1012
import static org.hydev.mcpm.utils.GeneralUtils.safeSleep;
1113

1214
/**
@@ -20,14 +22,16 @@ public class ProgressBar implements AutoCloseable
2022
private final ConsoleUtils cu;
2123
private final ProgressBarTheme theme;
2224
private final PrintStream out;
23-
private final int cols;
25+
private int cols;
2426

2527
private final List<ProgressRow> activeBars;
2628

2729
private long lastUpdate;
2830

2931
private double frameDelay;
3032

33+
private final boolean istty;
34+
3135
/**
3236
* Create and initialize a progress bar
3337
*
@@ -41,11 +45,18 @@ public ProgressBar(ProgressBarTheme theme)
4145
this.activeBars = new ArrayList<>();
4246
this.cols = AnsiConsole.getTerminalWidth();
4347

48+
// Default to 70-char width if the width can't be detected (like in a non-tty output)
49+
if (this.cols == 0) this.cols = 70;
50+
4451
// Last update time
4552
this.lastUpdate = System.nanoTime();
4653

4754
// Default frame delay is 0.01666 (60 fps)
4855
this.frameDelay = 1 / 60d;
56+
57+
// Check if output is a TTY. If not, change frame rate to 0.5 fps to avoid spamming a log.
58+
this.istty = isatty(STDOUT_FILENO) == 0;
59+
if (istty) this.frameDelay = 1 / 0.5;
4960
}
5061

5162
/**
@@ -79,7 +90,7 @@ protected void update()
7990
private void forceUpdate()
8091
{
8192
// Roll back to the first line
82-
cu.curUp(activeBars.size());
93+
if (istty) cu.curUp(activeBars.size());
8394
activeBars.forEach(bar -> out.println(bar.toString(theme, cols)));
8495
}
8596

@@ -142,7 +153,7 @@ public static void main(String[] args)
142153
for (int i = 0; i < 1300; i++)
143154
{
144155
if (i < 1000 && i % 100 == 0)
145-
all.add(b.appendBar(new ProgressRow(300).unit("MB").desc(format("File %s.tar.gz", all.size()))).descLen(40));
156+
all.add(b.appendBar(new ProgressRow(300).unit("MB").desc(format("File %s.tar.gz", all.size()))).descLen(30));
146157
all.forEach(a -> a.increase(1));
147158
safeSleep(3);
148159
}

src/main/java/org/hydev/mcpm/client/interaction/ProgressRow.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ public String toString(ProgressBarTheme theme, int cols)
6767
// Add progress bar length
6868
var len = cols - t.length() + "{progbar}".length();
6969

70+
// Safety check: If there are no space to display the progress bar, then don't display it
71+
if (len < 0) return t.replace("{progbar}", "");
72+
7073
// Calculate progress length
7174
int pLen = (int) (1d * completed / total * len);
7275
var bar = theme.done().repeat(pLen / theme.doneLen()) + theme.ipr().repeat((len - pLen) / theme.iprLen());

0 commit comments

Comments
 (0)