-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtabs.php
More file actions
70 lines (61 loc) · 2.05 KB
/
tabs.php
File metadata and controls
70 lines (61 loc) · 2.05 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
65
66
67
68
69
70
<?php
declare(strict_types=1);
/**
* Tab-bar pattern — keep multiple panes addressable by a single
* cursor index, render only the active one.
*
* php examples/tabs.php
*
* Tab to advance, Shift+Tab to step back. '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 Tabs implements Model
{
/** @param list<string> $names @param list<string> $bodies */
public function __construct(
public readonly array $names,
public readonly array $bodies,
public readonly int $cursor = 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 (true) {
$msg->type === KeyType::Tab && $msg->alt
=> [new self($this->names, $this->bodies, ($this->cursor - 1 + count($this->names)) % count($this->names)), null],
$msg->type === KeyType::Tab
=> [new self($this->names, $this->bodies, ($this->cursor + 1) % count($this->names)), null],
default => [$this, null],
};
}
public function view(): string
{
$bar = '';
foreach ($this->names as $i => $name) {
$bar .= $i === $this->cursor
? "\x1b[7m $name \x1b[0m "
: " $name ";
}
return rtrim($bar) . "\n\n" . $this->bodies[$this->cursor] . "\n\n(Tab / Shift+Tab to switch, q to quit)\n";
}
}
(new Program(new Tabs(
['Inbox', 'Drafts', 'Sent'],
[
"Inbox: 12 unread.\n • Mom: dinner Sunday?\n • Bills due Tue\n • PSA: candy is good",
"Drafts: 3.\n • Letter to grandma\n • Cookie recipe\n • Resignation",
"Sent: 47 messages.",
],
)))->run();