Skip to content

Commit 7ca16c7

Browse files
committed
Create a countdown terminal object
1 parent f2d78fb commit 7ca16c7

1 file changed

Lines changed: 70 additions & 0 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace League\CLImate\TerminalObject\Dynamic;
4+
5+
use function sleep;
6+
7+
final class Countdown extends DynamicTerminalObject
8+
{
9+
/** @var string */
10+
private $label = "";
11+
12+
/** @var int */
13+
private $from = 5;
14+
15+
16+
/**
17+
* @param string $label
18+
*/
19+
public function __construct(string $label = "Starting in... ")
20+
{
21+
$this->label = $label;
22+
}
23+
24+
25+
/**
26+
* @param int $from
27+
*
28+
* @return self
29+
*/
30+
public function from(int $from): self
31+
{
32+
$this->from = $from;
33+
return $this;
34+
}
35+
36+
37+
/**
38+
* @return void
39+
*/
40+
public function run(): void
41+
{
42+
$firstLine = true;
43+
$i = $this->from;
44+
while (true) {
45+
46+
$content = "";
47+
48+
if ($firstLine) {
49+
$firstLine = false;
50+
} else {
51+
$content .= $this->util->cursor->up(1);
52+
$content .= $this->util->cursor->startOfCurrentLine();
53+
$content .= $this->util->cursor->deleteCurrentLine();
54+
}
55+
56+
$content .= $this->label;
57+
if ($i > 0) {
58+
$content .= $i;
59+
}
60+
61+
$this->output->write($this->parser->apply($content));
62+
if ($i < 1) {
63+
break;
64+
}
65+
66+
sleep(1);
67+
--$i;
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)