@@ -26,6 +26,8 @@ public class ProgressBar implements AutoCloseable
26
26
27
27
private long lastUpdate ;
28
28
29
+ private double frameDelay ;
30
+
29
31
/**
30
32
* Create and initialize a progress bar
31
33
*
@@ -41,6 +43,9 @@ public ProgressBar(ProgressBarTheme theme)
41
43
42
44
// Last update time
43
45
this .lastUpdate = System .nanoTime ();
46
+
47
+ // Default frame delay is 0.01666 (60 fps)
48
+ this .frameDelay = 1 / 60d ;
44
49
}
45
50
46
51
/**
@@ -61,11 +66,11 @@ public ProgressRow appendBar(ProgressRow bar)
61
66
62
67
protected void update ()
63
68
{
64
- // Check time, update only every 0.0166s (60 fps )
69
+ // Check time to limit for framerate (default 60fps )
65
70
// Performance of the update heavily depends on the terminal's escape code handling
66
71
// implementation, so frequent updates will degrade performance on a bad terminal
67
72
var curTime = System .nanoTime ();
68
- if ((curTime - lastUpdate ) / 1e9d < 0.0166 ) return ;
73
+ if ((curTime - lastUpdate ) / 1e9d < frameDelay ) return ;
69
74
lastUpdate = curTime ;
70
75
71
76
forceUpdate ();
@@ -93,14 +98,30 @@ public void finishBar(ProgressRow bar)
93
98
94
99
/**
95
100
* Finalize and close the progress bar (print the final line)
96
- *
97
- * @throws Exception e
98
101
*/
99
102
@ Override
100
103
public void close ()
101
104
{
102
105
}
103
106
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
+
104
125
public List <ProgressRow > getActiveBars ()
105
126
{
106
127
return activeBars ;
0 commit comments