Repository navigation
Expand file tree
/
Copy pathsuspend.php
More file actions
61 lines (52 loc) · 1.51 KB
/
Copy pathsuspend.php
File metadata and controls
61 lines (52 loc) · 1.51 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
61
<?php
declare(strict_types=1);
/**
* `Cmd::suspend` — drop into a Ctrl+Z background, then resume on
* SIGCONT. The runtime sends ResumeMsg when the program comes
* back to the foreground.
*
* php examples/suspend.php
*
* Press space to suspend the program. Run `fg` in your shell to
* bring it back. 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\Msg\ResumeMsg;
use SugarCraft\Core\Msg\SuspendMsg;
use SugarCraft\Core\Program;
final class SuspendDemo implements Model
{
public function __construct(public readonly int $resumeCount = 0)
{
}
public function init(): ?\Closure
{
return null;
}
public function update(Msg $msg): array
{
if ($msg instanceof KeyMsg) {
if ($msg->type === KeyType::Char && $msg->rune === 'q') {
return [$this, Cmd::quit()];
}
if ($msg->type === KeyType::Space) {
return [$this, Cmd::suspend()];
}
}
if ($msg instanceof ResumeMsg) {
return [new self($this->resumeCount + 1), null];
}
return [$this, null];
}
public function view(): string
{
return "Suspend demo — resumed {$this->resumeCount} time(s).\n\n"
. "(space to suspend, type `fg` to resume, q to quit)\n";
}
}
(new Program(new SuspendDemo()))->run();