-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounter.php
More file actions
60 lines (50 loc) · 1.28 KB
/
Copy pathcounter.php
File metadata and controls
60 lines (50 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
declare(strict_types=1);
/**
* The classic bubbletea counter app.
*
* php examples/counter.php
*
* ↑ / ↓ to count, q to quit.
*/
require __DIR__ . '/../vendor/autoload.php';
use SugarCraft\Core\Cmd;
use SugarCraft\Core\KeyType;
use SugarCraft\Core\Model;
use SugarCraft\Core\Msg;
use SugarCraft\Core\Msg\KeyMsg;
use SugarCraft\Core\Program;
use SugarCraft\Core\Subscriptions;
final class Counter implements Model
{
public function __construct(public readonly int $count = 0)
{
}
public function init(): ?\Closure
{
return null;
}
public function update(Msg $msg): array
{
if (!$msg instanceof KeyMsg) {
return [$this, null];
}
if ($msg->type === KeyType::Char && $msg->rune === 'q') {
return [$this, Cmd::quit()];
}
return match ($msg->type) {
KeyType::Up => [new self($this->count + 1), null],
KeyType::Down => [new self($this->count - 1), null],
default => [$this, null],
};
}
public function view(): string
{
return "Count: {$this->count}\n\n↑ / ↓ to count, q to quit\n";
}
public function subscriptions(): ?Subscriptions
{
return null;
}
}
(new Program(new Counter()))->run();