Skip to content

Commit 9afe381

Browse files
committed
Docs and example for custom mappings
1 parent 8e6695e commit 9afe381

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,41 @@ $menu = (new CliMenuBuilder)
453453
$menu->open();
454454
```
455455

456+
#### Custom Control Mapping
457+
458+
This functionality allows to map custom key presses to a callable. For example we can set the key press "x" to close the menu:
459+
460+
```php
461+
$exit = function(CliMenu $menu) {
462+
$menu->close();
463+
}
464+
465+
$menu = (new CliMenuBuilder)
466+
->addItem('Item 1')
467+
->build();
468+
469+
$menu->addCustomMapping("x", $exit);
470+
471+
$menu->open();
472+
```
473+
474+
Another example is mapping shortcuts to a list of items:
475+
476+
```php
477+
$myCallback = function(CliMenu $menu) {
478+
// Do something
479+
}
480+
481+
$menu = (new CliMenuBuilder)
482+
->addItem('List of [C]lients', $myCallback)
483+
->build();
484+
485+
// Now, pressing Uppercase C (it's case sensitive) will call $myCallback
486+
$menu->addCustomMapping('C', $myCallback);
487+
488+
$menu->open();
489+
```
490+
456491
#### Dialogues
457492

458493
##### Flash

examples/custom-mapping.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
use PhpSchool\CliMenu\CliMenu;
4+
use PhpSchool\CliMenu\CliMenuBuilder;
5+
6+
require_once(__DIR__ . '/../vendor/autoload.php');
7+
8+
$exit = function (CliMenu $menu) {
9+
$menu->close();
10+
};
11+
12+
$menu = (new CliMenuBuilder)
13+
->setTitle('Basic CLI Menu')
14+
->addItem('First Item', $exit)
15+
->addItem('Second Item', $exit)
16+
->addLineBreak('-')
17+
->build();
18+
19+
$menu->addCustomControlMapping('x', $exit);
20+
$menu->open();

0 commit comments

Comments
 (0)