File tree Expand file tree Collapse file tree 2 files changed +51
-2
lines changed
src/TerminalObject/Dynamic Expand file tree Collapse file tree 2 files changed +51
-2
lines changed Original file line number Diff line number Diff line change @@ -27,6 +27,13 @@ class Progress extends DynamicTerminalObject
2727 */
2828 protected $ current_percentage = '' ;
2929
30+ /**
31+ * The number of decimal points to display
32+ *
33+ * @var integer $precision
34+ */
35+ protected $ precision = 0 ;
36+
3037 /**
3138 * The string length of the bar when at 100%
3239 *
@@ -88,6 +95,20 @@ public function total($total)
8895 return $ this ;
8996 }
9097
98+ /**
99+ * Set the completed percentage precision
100+ *
101+ * @param integer $precision The number of decimal places to display
102+ *
103+ * @return Progress
104+ */
105+ public function precision ($ precision )
106+ {
107+ $ this ->precision = $ precision ;
108+
109+ return $ this ;
110+ }
111+
91112 /**
92113 * Determines the current percentage we are at and re-writes the progress bar
93114 *
@@ -281,12 +302,14 @@ protected function getBarStrLen()
281302 /**
282303 * Format the percentage so it looks pretty
283304 *
284- * @param integer $percentage
305+ * @param integer $percentage The percentage (0-1) to format
306+ *
285307 * @return float
286308 */
287309 protected function percentageFormatted ($ percentage )
288310 {
289- return round ($ percentage * 100 ) . '% ' ;
311+ $ factor = pow (10 , $ this ->precision );
312+ return round ($ percentage * 100 * $ factor ) / $ factor . '% ' ;
290313 }
291314
292315 /**
Original file line number Diff line number Diff line change @@ -84,6 +84,32 @@ public function it_can_output_a_progress_bar_via_constructor()
8484 }
8585 }
8686
87+ /**
88+ * @test
89+ * @doesNotPerformAssertions
90+ */
91+ public function it_can_output_a_progress_bar_with_precision ()
92+ {
93+ $ this ->shouldWrite ('' );
94+ $ this ->shouldWrite ("\e[m \e[1A \r\e[K {$ this ->repeat (0 )} 0.008% \e[0m " );
95+ $ progress = $ this ->cli ->progress (100000 );
96+ $ progress ->precision (3 );
97+ $ progress ->current (8 );
98+ }
99+
100+ /**
101+ * @test
102+ * @doesNotPerformAssertions
103+ */
104+ public function it_can_output_a_progress_bar_with_precision_rounded ()
105+ {
106+ $ this ->shouldWrite ('' );
107+ $ this ->shouldWrite ("\e[m \e[1A \r\e[K {$ this ->repeat (0 )} 0.01% \e[0m " );
108+ $ progress = $ this ->cli ->progress (100000 );
109+ $ progress ->precision (2 );
110+ $ progress ->current (8 );
111+ }
112+
87113 /**
88114 * @test
89115 * @doesNotPerformAssertions
You can’t perform that action at this time.
0 commit comments