Skip to content

Commit 94c3f10

Browse files
committed
[O] PBar: Make FPS configurable
1 parent abb24db commit 94c3f10

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public class ProgressBar implements AutoCloseable
2626

2727
private long lastUpdate;
2828

29+
private double frameDelay;
30+
2931
/**
3032
* Create and initialize a progress bar
3133
*
@@ -41,6 +43,9 @@ public ProgressBar(ProgressBarTheme theme)
4143

4244
// Last update time
4345
this.lastUpdate = System.nanoTime();
46+
47+
// Default frame delay is 0.01666 (60 fps)
48+
this.frameDelay = 1 / 60d;
4449
}
4550

4651
/**
@@ -61,11 +66,11 @@ public ProgressRow appendBar(ProgressRow bar)
6166

6267
protected void update()
6368
{
64-
// Check time, update only every 0.0166s (60 fps)
69+
// Check time to limit for framerate (default 60fps)
6570
// Performance of the update heavily depends on the terminal's escape code handling
6671
// implementation, so frequent updates will degrade performance on a bad terminal
6772
var curTime = System.nanoTime();
68-
if ((curTime - lastUpdate) / 1e9d < 0.0166) return;
73+
if ((curTime - lastUpdate) / 1e9d < frameDelay) return;
6974
lastUpdate = curTime;
7075

7176
forceUpdate();
@@ -93,14 +98,30 @@ public void finishBar(ProgressRow bar)
9398

9499
/**
95100
* Finalize and close the progress bar (print the final line)
96-
*
97-
* @throws Exception e
98101
*/
99102
@Override
100103
public void close()
101104
{
102105
}
103106

107+
public ProgressBar setFrameDelay(double frameDelay)
108+
{
109+
this.frameDelay = frameDelay;
110+
return this;
111+
}
112+
113+
/**
114+
* Set frame rate in the unit of frames per second
115+
*
116+
* @param fps FPS
117+
* @return Self for fluent access
118+
*/
119+
public ProgressBar setFps(int fps)
120+
{
121+
this.frameDelay = 1d / fps;
122+
return this;
123+
}
124+
104125
public List<ProgressRow> getActiveBars()
105126
{
106127
return activeBars;

0 commit comments

Comments
 (0)