Skip to content

Commit abb24db

Browse files
committed
[O] PBar: Limit refresh rate to 60fps
1 parent 3efdbfa commit abb24db

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public class ProgressBar implements AutoCloseable
2424

2525
private final List<ProgressRow> activeBars;
2626

27+
private long lastUpdate;
28+
2729
/**
2830
* Create and initialize a progress bar
2931
*
@@ -36,6 +38,9 @@ public ProgressBar(ProgressBarTheme theme)
3638
this.cu = new ConsoleUtils(this.out);
3739
this.activeBars = new ArrayList<>();
3840
this.cols = AnsiConsole.getTerminalWidth();
41+
42+
// Last update time
43+
this.lastUpdate = System.nanoTime();
3944
}
4045

4146
/**
@@ -55,6 +60,18 @@ public ProgressRow appendBar(ProgressRow bar)
5560
}
5661

5762
protected void update()
63+
{
64+
// Check time, update only every 0.0166s (60 fps)
65+
// Performance of the update heavily depends on the terminal's escape code handling
66+
// implementation, so frequent updates will degrade performance on a bad terminal
67+
var curTime = System.nanoTime();
68+
if ((curTime - lastUpdate) / 1e9d < 0.0166) return;
69+
lastUpdate = curTime;
70+
71+
forceUpdate();
72+
}
73+
74+
private void forceUpdate()
5875
{
5976
// Roll back to the first line
6077
cu.curUp(activeBars.size());
@@ -68,6 +85,9 @@ protected void update()
6885
*/
6986
public void finishBar(ProgressRow bar)
7087
{
88+
if (!activeBars.contains(bar)) return;
89+
90+
forceUpdate();
7191
this.activeBars.remove(bar);
7292
}
7393

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ public String toString(ProgressBarTheme theme, int cols)
5353
// Calculate speed. TODO: Use a moving window to calculate speed
5454
double speed = completed / elapsed();
5555
double eta = total / speed;
56-
long eta_s = (long) (eta % 60), eta_m = (long) (eta / 60);
56+
long etaS = (long) (eta % 60), etaM = (long) (eta / 60);
5757

5858
// Replace variables
5959
var p = format("%3.0f%%", 100d * completed / total);
6060
var t = fmt.replace("{prefix}", theme.prefix())
6161
.replace("{suffix}", theme.suffix())
6262
.replace("{%done}", p)
63-
.replace("{eta}", format("%02d:%02d", eta_m, eta_s))
63+
.replace("{eta}", format("%02d:%02d", etaM, etaS))
6464
.replace("{speed}", format("%.2f%s/s", speed, unit))
6565
.replace("{desc}", descLen != 0 ? format("%-" + descLen + "s", desc) : desc + " ");
6666

@@ -86,6 +86,8 @@ public void setPb(ProgressBar pb)
8686
*/
8787
public void increase(long incr)
8888
{
89+
if (this.completed >= total) return;
90+
8991
this.completed += incr;
9092
pb.update();
9193
if (completed >= total) pb.finishBar(this);
@@ -98,6 +100,8 @@ public void increase(long incr)
98100
*/
99101
public void set(long completed)
100102
{
103+
if (this.completed >= total) return;
104+
101105
this.completed = completed;
102106
pb.update();
103107
if (completed >= total) pb.finishBar(this);

0 commit comments

Comments
 (0)