-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.php
More file actions
56 lines (47 loc) Β· 1.48 KB
/
views.php
File metadata and controls
56 lines (47 loc) Β· 1.48 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
<?php
declare(strict_types=1);
/**
* Multiple-view pattern β keep several view bodies behind one
* Model and switch between them via state.
*
* php examples/views.php
*
* Press 1, 2, 3 to switch views. Press '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;
final class Views implements Model
{
public function __construct(public readonly int $current = 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->rune) {
'1' => [new self(0), null],
'2' => [new self(1), null],
'3' => [new self(2), null],
default => [$this, null],
};
}
public function view(): string
{
$views = [
"π¬ Welcome screen\n\nThree-step wizard, view 1 of 3.\nPress 2 to advance.",
"π Configure step\n\nCheck things, type things.\nPress 3 to advance.",
"π Done!\n\nYou made it. Press 1 to start over.",
];
return $views[$this->current] . "\n\n(1/2/3 to switch, q to quit)\n";
}
}
(new Program(new Views()))->run();