Repository navigation
Expand file tree
/
Copy pathprint-key.php
More file actions
64 lines (55 loc) · 1.59 KB
/
Copy pathprint-key.php
File metadata and controls
64 lines (55 loc) · 1.59 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
62
63
64
<?php
declare(strict_types=1);
/**
* Print every key the runtime parses. Useful as a sanity check for
* Kitty / xterm modifier handling.
*
* php examples/print-key.php
*
* Press anything; press Ctrl-C to quit.
*/
require __DIR__ . '/../vendor/autoload.php';
use SugarCraft\Core\Cmd;
use SugarCraft\Core\Model;
use SugarCraft\Core\Msg;
use SugarCraft\Core\Msg\KeyMsg;
use SugarCraft\Core\Program;
final class PrintKey implements Model
{
/** @param list<string> $log */
public function __construct(public readonly array $log = [])
{
}
public function init(): ?\Closure
{
return null;
}
public function update(Msg $msg): array
{
if ($msg instanceof KeyMsg) {
if ($msg->ctrl && $msg->rune === 'c') {
return [$this, Cmd::quit()];
}
$entry = sprintf(
'type=%-12s rune=%-6s ctrl=%s alt=%s shift=%s string=%s',
$msg->type->name,
$msg->rune === '' ? '∅' : json_encode($msg->rune),
$msg->ctrl ? '✓' : '·',
$msg->alt ? '✓' : '·',
$msg->shift ? '✓' : '·',
$msg->string(),
);
$log = array_slice([...$this->log, $entry], -12);
return [new self($log), null];
}
return [$this, null];
}
public function view(): string
{
$body = $this->log === []
? '(press anything)'
: implode("\n", $this->log);
return "Last 12 keys:\n\n$body\n\n(Ctrl-C to quit)\n";
}
}
(new Program(new PrintKey()))->run();