Skip to content

Commit f41cb83

Browse files
committed
Disable auto shortcuts by default
1 parent 4867b80 commit f41cb83

File tree

4 files changed

+49
-9
lines changed

4 files changed

+49
-9
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -936,15 +936,15 @@ $menu->open();
936936

937937
## Item Keyboard Shortcuts
938938

939-
By default `CliMenuBuilder` will parse the items text and check for shortcuts. Any single character inside square brackets
939+
If you enable auto shortcuts `CliMenuBuilder` will parse the items text and check for shortcuts. Any single character inside square brackets
940940
will be treated as a shortcut. Pressing that character when the menu is open will trigger that items callable.
941941

942942
This functionality works for split items as well as sub menus. The same characters can be used inside sub menus and the
943943
callable which is invoked will depend on which menu is currently open.
944944

945945
Note: all shortcuts are lower cased.
946946

947-
You can disable this automatic keyboard shortcut mapping if you would like:
947+
To enable this automatic keyboard shortcut mapping simply call `->enableAutoShortcuts()`:
948948

949949
```php
950950
<?php
@@ -957,15 +957,18 @@ $myCallback = function(CliMenu $menu) {
957957
};
958958

959959
$menu = (new CliMenuBuilder)
960-
->disableAutoShortcuts()
960+
->enableAutoShortcuts()
961961
->addItem('List of [C]lients', $myCallback)
962962
->build();
963963

964964
$menu->open();
965965

966-
//Pressing c will do nothing.
966+
//Pressing c will execute $myCallback.
967967
```
968968

969+
You can customise the shortcut matching by passing your own regex to `enableAutoShortcuts`. Be careful to only match
970+
one character in the first capture group or an exception will be thrown.
971+
969972
### Dialogues
970973

971974
#### Flash

examples/shortcuts.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
};
1212

1313
$menu = (new CliMenuBuilder)
14-
->disableAutoShortcuts()
14+
->enableAutoShortcuts()
1515
->setTitle('Basic CLI Menu')
1616
->addItem('[F]irst Item', $itemCallable)
1717
->addItem('Se[c]ond Item', $itemCallable)
@@ -32,3 +32,4 @@
3232
->build();
3333

3434
$menu->open();
35+

src/Builder/CliMenuBuilder.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpSchool\CliMenu\Action\ExitAction;
66
use PhpSchool\CliMenu\Action\GoBackAction;
7+
use PhpSchool\CliMenu\Exception\InvalidShortcutException;
78
use PhpSchool\CliMenu\MenuItem\AsciiArtItem;
89
use PhpSchool\CliMenu\MenuItem\LineBreakItem;
910
use PhpSchool\CliMenu\MenuItem\MenuItemInterface;
@@ -63,7 +64,15 @@ class CliMenuBuilder
6364
*
6465
* @var bool
6566
*/
66-
private $autoShortcuts = true;
67+
private $autoShortcuts = false;
68+
69+
/**
70+
* Regex to auto match for shortcuts defaults to looking
71+
* for a single character encased in square brackets
72+
*
73+
* @var string
74+
*/
75+
private $autoShortcutsRegex = '/\[(.)\]/';
6776

6877
/**
6978
* @var bool
@@ -191,16 +200,29 @@ public function addSubMenuFromBuilder(string $text, CliMenuBuilder $builder) : s
191200
return $this;
192201
}
193202

194-
public function disableAutoShortcuts() : self
203+
public function enableAutoShortcuts(string $regex = null) : self
195204
{
196-
$this->autoShortcuts = false;
205+
$this->autoShortcuts = true;
206+
207+
if (null !== $regex) {
208+
$this->autoShortcutsRegex = $regex;
209+
}
197210

198211
return $this;
199212
}
200213

201214
private function extractShortcut(string $title) : ?string
202215
{
203-
preg_match('/\[(.)\]/', $title, $match);
216+
preg_match($this->autoShortcutsRegex, $title, $match);
217+
218+
if (!isset($match[1])) {
219+
return null;
220+
}
221+
222+
if (mb_strlen($match[1]) > 1) {
223+
throw InvalidShortcutException::fromShortcut($match[1]);
224+
}
225+
204226
return isset($match[1]) ? strtolower($match[1]) : null;
205227
}
206228

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace PhpSchool\CliMenu\Exception;
4+
5+
/**
6+
* @author Aydin Hassan <aydin@hotmail.co.uk>
7+
*/
8+
class InvalidShortcutException extends \RuntimeException
9+
{
10+
public static function fromShortcut(string $shortcut) : self
11+
{
12+
return new static(sprintf('Shortcut key must be only one character. Got: "%s"', $shortcut));
13+
}
14+
}

0 commit comments

Comments
 (0)