Skip to content

Commit 2426a48

Browse files
committed
Propagate auto shortcuts to submenus
1 parent f41cb83 commit 2426a48

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

examples/shortcuts.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@
2323
})
2424
->addLineBreak('-');
2525
})
26-
->addSplitItem(function (SplitItemBuilder $b) {
26+
->addSplitItem(function (SplitItemBuilder $b) use ($itemCallable) {
2727
$b->addItem('Split Item [1]', function() { echo 'Split Item 1!'; })
2828
->addItem('Split Item [2]', function() { echo 'Split Item 2!'; })
29-
->addItem('Split Item [3]', function() { echo 'Split Item 3!'; });
29+
->addItem('Split Item [3]', function() { echo 'Split Item 3!'; })
30+
->addSubMenu('Split Item [4]', function (CliMenuBuilder $builder) use ($itemCallable) {
31+
$builder->addItem('Third [I]tem', $itemCallable);
32+
33+
});
3034
})
3135
->addLineBreak('-')
3236
->build();

src/Builder/CliMenuBuilder.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ public function addSubMenu(string $text, \Closure $callback) : self
155155
{
156156
$builder = self::newSubMenu($this->terminal);
157157

158+
if ($this->autoShortcuts) {
159+
$builder->enableAutoShortcuts($this->autoShortcutsRegex);
160+
}
161+
158162
$callback = $callback->bindTo($builder);
159163
$callback($builder);
160164

@@ -270,6 +274,10 @@ public function addSplitItem(\Closure $callback) : self
270274
{
271275
$builder = new SplitItemBuilder($this->menu);
272276

277+
if ($this->autoShortcuts) {
278+
$builder->enableAutoShortcuts($this->autoShortcutsRegex);
279+
}
280+
273281
$callback = $callback->bindTo($builder);
274282
$callback($builder);
275283

src/Builder/SplitItemBuilder.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,22 @@ class SplitItemBuilder
2424
*/
2525
private $splitItem;
2626

27+
/**
28+
* Whether or not to auto create keyboard shortcuts for items
29+
* when they contain square brackets. Eg: [M]y item
30+
*
31+
* @var bool
32+
*/
33+
private $autoShortcuts = false;
34+
35+
/**
36+
* Regex to auto match for shortcuts defaults to looking
37+
* for a single character encased in square brackets
38+
*
39+
* @var string
40+
*/
41+
private $autoShortcutsRegex = '/\[(.)\]/';
42+
2743
public function __construct(CliMenu $menu)
2844
{
2945
$this->menu = $menu;
@@ -59,6 +75,10 @@ public function addSubMenu(string $text, \Closure $callback) : self
5975
{
6076
$builder = CliMenuBuilder::newSubMenu($this->menu->getTerminal());
6177

78+
if ($this->autoShortcuts) {
79+
$builder->enableAutoShortcuts($this->autoShortcutsRegex);
80+
}
81+
6282
$callback = $callback->bindTo($builder);
6383
$callback($builder);
6484

@@ -80,6 +100,17 @@ public function setGutter(int $gutter) : self
80100

81101
return $this;
82102
}
103+
104+
public function enableAutoShortcuts(string $regex = null) : self
105+
{
106+
$this->autoShortcuts = true;
107+
108+
if (null !== $regex) {
109+
$this->autoShortcutsRegex = $regex;
110+
}
111+
112+
return $this;
113+
}
83114

84115
public function build() : SplitItem
85116
{

0 commit comments

Comments
 (0)