forked from php-school/cli-menu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.php
69 lines (59 loc) · 1.79 KB
/
draw.php
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
<?php
declare(strict_types=1);
use PhpSchool\CliMenu\Action\ExitAction;
use PhpSchool\CliMenu\Builder\SplitItemBuilder;
use PhpSchool\CliMenu\CliMenu;
use PhpSchool\CliMenu\Builder\CliMenuBuilder;
use PhpSchool\CliMenu\MenuItem\SplitItem;
use PhpSchool\CliMenu\Style\SelectableStyle;
require_once(__DIR__ . '/../vendor/autoload.php');
$cols = 60;
$rows = 20;
$paint = function (CliMenu $menu) {
$item = $menu->getSelectedItem();
if ($item->getText() === ' ') {
$item->setText('█');
} else {
$item->setText(' ');
}
};
$clear = function (CliMenu $menu) {
foreach ($menu->getItems() as $item) {
if ($item instanceof SplitItem && $item->canSelect()) {
foreach ($item->getItems() as $cell) {
if ($cell->canSelect() && $cell->getText() !== ' ') {
$cell->setText(' ');
}
}
}
}
$menu->redraw(false);
};
$builder = (new CliMenuBuilder)
->disableDefaultItems()
->setWidth($cols + 10)
->setBorder(0)
->setMargin(2)
->setPadding(2, 5)
->modifySelectableStyle(function (SelectableStyle $style) {
$style->setSelectedMarker('')
->setUnselectedMarker('');
})
->addAsciiArt('Draw your own art !')
->addLineBreak();
for ($i = 0; $i < $rows; $i++) {
$builder->addSplitItem(function(SplitItemBuilder $b) use ($cols, $paint) {
$b->setGutter(0);
for ($j = 0; $j < $cols; $j++) {
$b->addItem(' ', $paint);
}
});
}
$builder->addSplitItem(function(SplitItemBuilder $b) {
$b->addStaticItem('Enter: Toggle draw')
->addStaticItem('C: Clear screen')
->addStaticItem('X: Exit');
});
$menu = $builder->build();
$menu->addCustomControlMappings(['C' => $clear, 'X' => new ExitAction]);
$menu->open();