-
Notifications
You must be signed in to change notification settings - Fork 106
Initial checkable item support #186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
AydinHassan
merged 6 commits into
php-school:master
from
jtreminio:feature/toggleable-item
Dec 16, 2019
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
12607bf
Initial toggleable item support
jtreminio 5bfa6ca
PR change requests
jtreminio 8b56765
Spacing
jtreminio bc0bdf7
check -> toggle
jtreminio 7088fe9
Unit tests for CheckableItems
jtreminio 86416ab
check() -> toggle()
jtreminio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
use PhpSchool\CliMenu\CliMenu; | ||
use PhpSchool\CliMenu\Builder\CliMenuBuilder; | ||
use PhpSchool\CliMenu\MenuItem\CheckableItem; | ||
|
||
require_once(__DIR__ . '/../vendor/autoload.php'); | ||
|
||
$itemCallable = function (CliMenu $menu) { | ||
/** @var CheckableItem $item */ | ||
$item = $menu->getSelectedItem(); | ||
|
||
$item->check(); | ||
|
||
$menu->redraw(); | ||
}; | ||
|
||
$menu = (new CliMenuBuilder) | ||
->setTitle('Select a Language') | ||
->addSubMenu('Compiled', function (CliMenuBuilder $b) use ($itemCallable) { | ||
$b->setTitle('Compiled Languages') | ||
->addCheckableItem('Rust', $itemCallable) | ||
->addCheckableItem('C++', $itemCallable) | ||
->addCheckableItem('Go', $itemCallable) | ||
->addCheckableItem('Java', $itemCallable) | ||
->addCheckableItem('C', $itemCallable) | ||
; | ||
}) | ||
->addSubMenu('Interpreted', function (CliMenuBuilder $b) use ($itemCallable) { | ||
$b->setTitle('Interpreted Languages') | ||
->setUncheckedMarker('[○] ') | ||
->setCheckedMarker('[●] ') | ||
->addCheckableItem('PHP', $itemCallable) | ||
->addCheckableItem('Javascript', $itemCallable) | ||
->addCheckableItem('Ruby', $itemCallable) | ||
->addCheckableItem('Python', $itemCallable) | ||
; | ||
}) | ||
->build(); | ||
|
||
$menu->open(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
<?php | ||
|
||
namespace PhpSchool\CliMenu\MenuItem; | ||
|
||
use PhpSchool\CliMenu\MenuItem; | ||
use PhpSchool\CliMenu\MenuStyle; | ||
use PhpSchool\CliMenu\Util\StringUtil; | ||
|
||
class CheckableItem implements MenuItem\MenuItemInterface | ||
{ | ||
/** | ||
* @var callable | ||
*/ | ||
private $selectAction; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $text = ''; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $showItemExtra = false; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $disabled = false; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $checked = false; | ||
|
||
public function __construct( | ||
string $text, | ||
callable $selectAction, | ||
bool $showItemExtra = false, | ||
bool $disabled = false | ||
) { | ||
$this->text = $text; | ||
$this->selectAction = $selectAction; | ||
$this->showItemExtra = $showItemExtra; | ||
$this->disabled = $disabled; | ||
} | ||
|
||
/** | ||
* Execute the items callable if required | ||
*/ | ||
public function getSelectAction() : ?callable | ||
{ | ||
return $this->selectAction; | ||
} | ||
|
||
/** | ||
* Return the raw string of text | ||
*/ | ||
public function getText() : string | ||
{ | ||
return $this->text; | ||
} | ||
|
||
/** | ||
* Set the raw string of text | ||
*/ | ||
public function setText(string $text) : void | ||
{ | ||
$this->text = $text; | ||
} | ||
|
||
/** | ||
* The output text for the item | ||
* | ||
* @param MenuStyle $style | ||
* @param bool $selected Currently unused in this class | ||
* @return array | ||
*/ | ||
public function getRows(MenuStyle $style, bool $selected = false) : array | ||
{ | ||
$marker = sprintf("%s", $this->checked ? $style->getCheckedMarker() : $style->getUncheckedMarker()); | ||
|
||
$length = $style->getDisplaysExtra() | ||
? $style->getContentWidth() - (mb_strlen($style->getItemExtra()) + 2) | ||
: $style->getContentWidth(); | ||
|
||
$rows = explode( | ||
"\n", | ||
StringUtil::wordwrap( | ||
sprintf('%s%s', $marker, $this->text), | ||
$length, | ||
sprintf("\n%s", str_repeat(' ', mb_strlen($marker))) | ||
) | ||
); | ||
|
||
return array_map(function ($row, $key) use ($style, $length) { | ||
$text = $this->disabled ? $style->getDisabledItemText($row) : $row; | ||
|
||
if ($key === 0) { | ||
return $this->showItemExtra | ||
? sprintf('%s%s %s', $text, str_repeat(' ', $length - mb_strlen($row)), $style->getItemExtra()) | ||
: $text; | ||
} | ||
|
||
return $text; | ||
}, $rows, array_keys($rows)); | ||
} | ||
|
||
/** | ||
* Can the item be selected | ||
*/ | ||
public function canSelect() : bool | ||
{ | ||
return !$this->disabled; | ||
} | ||
|
||
public function showsItemExtra() : bool | ||
{ | ||
return $this->showItemExtra; | ||
} | ||
|
||
/** | ||
* Enable showing item extra | ||
*/ | ||
public function showItemExtra() : void | ||
{ | ||
$this->showItemExtra = true; | ||
} | ||
|
||
/** | ||
* Disable showing item extra | ||
*/ | ||
public function hideItemExtra() : void | ||
{ | ||
$this->showItemExtra = false; | ||
} | ||
|
||
/** | ||
* Toggles checked state | ||
*/ | ||
public function check() | ||
{ | ||
$this->checked = !$this->checked; | ||
} | ||
|
||
/** | ||
* Sets checked state to true | ||
*/ | ||
public function setChecked() | ||
{ | ||
$this->checked = true; | ||
} | ||
|
||
/** | ||
* Sets checked state to false | ||
*/ | ||
public function setUnchecked() | ||
{ | ||
$this->checked = false; | ||
} | ||
|
||
public function getChecked(): bool | ||
{ | ||
return $this->checked; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.