Skip to content

Commit 45c1848

Browse files
committed
Allow to disable auto shortcuts
1 parent 629a9ce commit 45c1848

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

examples/shortcuts.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
};
1212

1313
$menu = (new CliMenuBuilder)
14+
->disableAutoShortcuts()
1415
->setTitle('Basic CLI Menu')
1516
->addItem('[F]irst Item', $itemCallable)
1617
->addItem('Se[c]ond Item', $itemCallable)

src/Builder/CliMenuBuilder.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ class CliMenuBuilder
5757
*/
5858
private $disabled = false;
5959

60+
/**
61+
* Whether or not to auto create keyboard shortcuts for items
62+
* when they contain square brackets. Eg: [M]y item
63+
*
64+
* @var bool
65+
*/
66+
private $autoShortcuts = true;
67+
6068
/**
6169
* @var bool
6270
*/
@@ -183,6 +191,13 @@ public function addSubMenuFromBuilder(string $text, CliMenuBuilder $builder) : s
183191
return $this;
184192
}
185193

194+
public function disableAutoShortcuts() : self
195+
{
196+
$this->autoShortcuts = false;
197+
198+
return $this;
199+
}
200+
186201
private function extractShortcut(string $title) : ?string
187202
{
188203
preg_match('/\[(.)\]/', $title, $match);
@@ -217,6 +232,10 @@ private function processSplitItemShortcuts(SplitItem $splitItem) : void
217232

218233
private function processIndividualShortcut(MenuItemInterface $item, callable $callback) : void
219234
{
235+
if (!$this->autoShortcuts) {
236+
return;
237+
}
238+
220239
if ($shortcut = $this->extractShortcut($item->getText())) {
221240
$this->menu->addCustomControlMapping(
222241
$shortcut,

test/Builder/CliMenuBuilderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ public function testAddSplitItemWithClosureBinding() : void
779779

780780
$this->checkItems($menu->getItems()[0]->getItems(), $expected);
781781
}
782-
782+
783783
private function checkMenuItems(CliMenu $menu, array $expected) : void
784784
{
785785
$this->checkItems($this->readAttribute($menu, 'items'), $expected);

0 commit comments

Comments
 (0)