Skip to content

Commit e58bb70

Browse files
jtreminioAydinHassan
authored andcommitted
Item styling for Checkbox and Radio items
1 parent c72b588 commit e58bb70

19 files changed

+581
-334
lines changed

examples/checkable-item.php renamed to examples/checkbox-item.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use PhpSchool\CliMenu\CliMenu;
44
use PhpSchool\CliMenu\Builder\CliMenuBuilder;
5+
use PhpSchool\CliMenu\Style\CheckboxStyle;
56

67
require_once(__DIR__ . '/../vendor/autoload.php');
78

@@ -22,8 +23,10 @@
2223
})
2324
->addSubMenu('Interpreted', function (CliMenuBuilder $b) use ($itemCallable) {
2425
$b->setTitle('Interpreted Languages')
25-
->setUncheckedMarker('[○] ')
26-
->setCheckedMarker('[●] ')
26+
->checkboxStyle(function (CheckboxStyle $style) {
27+
$style->setMarkerOff('[○] ')
28+
->setMarkerOn('[●] ');
29+
})
2730
->addCheckboxItem('PHP', $itemCallable)
2831
->addCheckboxItem('Javascript', $itemCallable)
2932
->addCheckboxItem('Ruby', $itemCallable)

examples/radio-item.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use PhpSchool\CliMenu\CliMenu;
44
use PhpSchool\CliMenu\Builder\CliMenuBuilder;
5+
use PhpSchool\CliMenu\Style\RadioStyle;
56

67
require_once(__DIR__ . '/../vendor/autoload.php');
78

@@ -22,8 +23,10 @@
2223
})
2324
->addSubMenu('Interpreted', function (CliMenuBuilder $b) use ($itemCallable) {
2425
$b->setTitle('Interpreted Languages')
25-
->setUnradioMarker('[ ] ')
26-
->setRadioMarker('[✔] ')
26+
->radioStyle(function (RadioStyle $style) {
27+
$style->setMarkerOff('[ ] ')
28+
->setMarkerOn('[✔] ');
29+
})
2730
->addRadioItem('PHP', $itemCallable)
2831
->addRadioItem('Javascript', $itemCallable)
2932
->addRadioItem('Ruby', $itemCallable)

examples/split-checkable-item.php renamed to examples/split-checkbox-item.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
require_once(__DIR__ . '/../vendor/autoload.php');
88

9+
ini_set('display_errors', 1);
10+
ini_set('display_startup_errors', 1);
11+
error_reporting(E_ALL);
12+
913
$itemCallable = function (CliMenu $menu) {
1014
echo $menu->getSelectedItem()->getText();
1115
};

src/Builder/CliMenuBuilder.php

Lines changed: 72 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use PhpSchool\CliMenu\MenuItem\SplitItem;
1717
use PhpSchool\CliMenu\MenuItem\StaticItem;
1818
use PhpSchool\CliMenu\MenuStyle;
19+
use PhpSchool\CliMenu\Style\CheckboxStyle;
20+
use PhpSchool\CliMenu\Style\RadioStyle;
1921
use PhpSchool\CliMenu\Terminal\TerminalFactory;
2022
use PhpSchool\Terminal\Terminal;
2123

@@ -138,7 +140,10 @@ public function addCheckboxItem(
138140
bool $showItemExtra = false,
139141
bool $disabled = false
140142
) : self {
141-
$this->addMenuItem(new CheckboxItem($text, $itemCallable, $showItemExtra, $disabled));
143+
$item = (new CheckboxItem($text, $itemCallable, $showItemExtra, $disabled))
144+
->setStyle($this->menu->getCheckboxStyle());
145+
146+
$this->addMenuItem($item);
142147

143148
return $this;
144149
}
@@ -149,7 +154,10 @@ public function addRadioItem(
149154
bool $showItemExtra = false,
150155
bool $disabled = false
151156
) : self {
152-
$this->addMenuItem(new RadioItem($text, $itemCallable, $showItemExtra, $disabled));
157+
$item = (new RadioItem($text, $itemCallable, $showItemExtra, $disabled))
158+
->setStyle($this->menu->getRadioStyle());
159+
160+
$this->addMenuItem($item);
153161

154162
return $this;
155163
}
@@ -194,6 +202,18 @@ public function addSubMenu(string $text, \Closure $callback) : self
194202
$menu->setStyle($this->menu->getStyle());
195203
}
196204

205+
if (!$menu->getCheckboxStyle()->getIsCustom()) {
206+
$menu->checkboxStyle(function (CheckboxStyle $style) {
207+
$style->fromArray($this->menu->getCheckboxStyle()->toArray());
208+
});
209+
}
210+
211+
if (!$menu->getRadioStyle()->getIsCustom()) {
212+
$menu->radioStyle(function (RadioStyle $style) {
213+
$style->fromArray($this->menu->getRadioStyle()->toArray());
214+
});
215+
}
216+
197217
$this->menu->addItem($item = new MenuMenuItem(
198218
$text,
199219
$menu,
@@ -216,6 +236,14 @@ public function addSubMenuFromBuilder(string $text, CliMenuBuilder $builder) : s
216236
$menu->setStyle($this->menu->getStyle());
217237
}
218238

239+
$menu->checkboxStyle(function (CheckboxStyle $style) {
240+
$style->fromArray($this->menu->getCheckboxStyle()->toArray());
241+
});
242+
243+
$menu->radioStyle(function (RadioStyle $style) {
244+
$style->fromArray($this->menu->getRadioStyle()->toArray());
245+
});
246+
219247
$this->menu->addItem($item = new MenuMenuItem(
220248
$text,
221249
$menu,
@@ -302,6 +330,14 @@ public function addSplitItem(\Closure $callback) : self
302330
}
303331

304332
$callback($builder);
333+
334+
$builder->checkboxStyle(function (CheckboxStyle $style) {
335+
$style->fromArray($this->menu->getCheckboxStyle()->toArray());
336+
});
337+
338+
$builder->radioStyle(function (RadioStyle $style) {
339+
$style->fromArray($this->menu->getRadioStyle()->toArray());
340+
});
305341

306342
$this->menu->addItem($splitItem = $builder->build());
307343

@@ -417,34 +453,6 @@ public function setSelectedMarker(string $marker) : self
417453
return $this;
418454
}
419455

420-
public function setUncheckedMarker(string $marker) : self
421-
{
422-
$this->style->setUncheckedMarker($marker);
423-
424-
return $this;
425-
}
426-
427-
public function setCheckedMarker(string $marker) : self
428-
{
429-
$this->style->setCheckedMarker($marker);
430-
431-
return $this;
432-
}
433-
434-
public function setUnradioMarker(string $marker) : self
435-
{
436-
$this->style->setUnradioMarker($marker);
437-
438-
return $this;
439-
}
440-
441-
public function setRadioMarker(string $marker) : self
442-
{
443-
$this->style->setRadioMarker($marker);
444-
445-
return $this;
446-
}
447-
448456
public function setItemExtra(string $extra) : self
449457
{
450458
$this->style->setItemExtra($extra);
@@ -558,4 +566,38 @@ public function build() : CliMenu
558566

559567
return $this->menu;
560568
}
569+
570+
/**
571+
* Use as
572+
*
573+
->checkboxStyle(function (CheckboxStyle $style) {
574+
$style->setMarkerOff('- ');
575+
})
576+
*
577+
* @param callable $itemCallable
578+
* @return $this
579+
*/
580+
public function checkboxStyle(callable $itemCallable) : self
581+
{
582+
$this->menu->checkboxStyle($itemCallable);
583+
584+
return $this;
585+
}
586+
587+
/**
588+
* Use as
589+
*
590+
->radioStyle(function (RadioStyle $style) {
591+
$style->setMarkerOff('- ');
592+
})
593+
*
594+
* @param callable $itemCallable
595+
* @return $this
596+
*/
597+
public function radioStyle(callable $itemCallable) : self
598+
{
599+
$this->menu->radioStyle($itemCallable);
600+
601+
return $this;
602+
}
561603
}

src/Builder/SplitItemBuilder.php

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use PhpSchool\CliMenu\MenuItem\SelectableItem;
1111
use PhpSchool\CliMenu\MenuItem\SplitItem;
1212
use PhpSchool\CliMenu\MenuItem\StaticItem;
13+
use PhpSchool\CliMenu\Style\CheckboxStyle;
14+
use PhpSchool\CliMenu\Style\RadioStyle;
1315

1416
/**
1517
* @author Aydin Hassan <[email protected]>
@@ -42,10 +44,23 @@ class SplitItemBuilder
4244
*/
4345
private $autoShortcutsRegex = '/\[(.)\]/';
4446

47+
/**
48+
* @var CheckboxStyle
49+
*/
50+
private $checkboxStyle;
51+
52+
/**
53+
* @var RadioStyle
54+
*/
55+
private $radioStyle;
56+
4557
public function __construct(CliMenu $menu)
4658
{
4759
$this->menu = $menu;
4860
$this->splitItem = new SplitItem();
61+
62+
$this->checkboxStyle = new CheckboxStyle();
63+
$this->radioStyle = new RadioStyle();
4964
}
5065

5166
public function addItem(
@@ -65,7 +80,10 @@ public function addCheckboxItem(
6580
bool $showItemExtra = false,
6681
bool $disabled = false
6782
) : self {
68-
$this->splitItem->addItem(new CheckboxItem($text, $itemCallable, $showItemExtra, $disabled));
83+
$item = (new CheckboxItem($text, $itemCallable, $showItemExtra, $disabled))
84+
->setStyle($this->menu->getCheckboxStyle());
85+
86+
$this->splitItem->addItem($item);
6987

7088
return $this;
7189
}
@@ -76,7 +94,10 @@ public function addRadioItem(
7694
bool $showItemExtra = false,
7795
bool $disabled = false
7896
) : self {
79-
$this->splitItem->addItem(new RadioItem($text, $itemCallable, $showItemExtra, $disabled));
97+
$item = (new RadioItem($text, $itemCallable, $showItemExtra, $disabled))
98+
->setStyle($this->menu->getRadioStyle());
99+
100+
$this->splitItem->addItem($item);
80101

81102
return $this;
82103
}
@@ -108,6 +129,14 @@ public function addSubMenu(string $text, \Closure $callback) : self
108129
$menu = $builder->build();
109130
$menu->setParent($this->menu);
110131

132+
$menu->checkboxStyle(function (CheckboxStyle $style) {
133+
$style->fromArray($this->menu->getCheckboxStyle()->toArray());
134+
});
135+
136+
$menu->radioStyle(function (RadioStyle $style) {
137+
$style->fromArray($this->menu->getRadioStyle()->toArray());
138+
});
139+
111140
$this->splitItem->addItem(new MenuMenuItem(
112141
$text,
113142
$menu,
@@ -139,4 +168,38 @@ public function build() : SplitItem
139168
{
140169
return $this->splitItem;
141170
}
171+
172+
/**
173+
* Use as
174+
*
175+
->checkboxStyle(function (CheckboxStyle $style) {
176+
$style->setMarkerOff('- ');
177+
})
178+
*
179+
* @param callable $itemCallable
180+
* @return $this
181+
*/
182+
public function checkboxStyle(callable $itemCallable) : self
183+
{
184+
$this->menu->checkboxStyle($itemCallable);
185+
186+
return $this;
187+
}
188+
189+
/**
190+
* Use as
191+
*
192+
->radioStyle(function (RadioStyle $style) {
193+
$style->setMarkerOff('- ');
194+
})
195+
*
196+
* @param callable $itemCallable
197+
* @return $this
198+
*/
199+
public function radioStyle(callable $itemCallable) : self
200+
{
201+
$this->menu->radioStyle($itemCallable);
202+
203+
return $this;
204+
}
142205
}

0 commit comments

Comments
 (0)