-
Notifications
You must be signed in to change notification settings - Fork 106
Selectable item styles #214
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 5 commits into
php-school:master
from
jtreminio:feature/selectable-style
Dec 20, 2019
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e1f5a9e
Selectable item style
jtreminio 06e5469
Merge branch 'master' into feature/selectable-style
jtreminio 2dc5e1c
Tests and upstream changes for Selectable style
jtreminio c459f5c
custom no longer needed
jtreminio 09b4294
CS fix
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
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
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
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 |
---|---|---|
|
@@ -2,18 +2,31 @@ | |
|
||
namespace PhpSchool\CliMenu\MenuItem; | ||
|
||
use PhpSchool\CliMenu\MenuStyle; | ||
use PhpSchool\CliMenu\Util\StringUtil; | ||
use PhpSchool\CliMenu\Style\SelectableStyle; | ||
|
||
/** | ||
* @author Michael Woodward <[email protected]> | ||
*/ | ||
class SelectableItem implements MenuItemInterface | ||
{ | ||
use SelectableTrait; | ||
|
||
/** | ||
* @var callable | ||
*/ | ||
private $selectAction; | ||
|
||
private $text = ''; | ||
|
||
private $showItemExtra = false; | ||
|
||
private $disabled = false; | ||
|
||
/** | ||
* @var SelectableStyle; | ||
*/ | ||
private $style; | ||
|
||
public function __construct( | ||
string $text, | ||
callable $selectAction, | ||
|
@@ -24,6 +37,46 @@ public function __construct( | |
$this->selectAction = $selectAction; | ||
$this->showItemExtra = $showItemExtra; | ||
$this->disabled = $disabled; | ||
|
||
$this->style = new SelectableStyle(); | ||
} | ||
|
||
/** | ||
* The output text for the item | ||
*/ | ||
public function getRows(MenuStyle $style, bool $selected = false) : array | ||
{ | ||
$marker = sprintf("%s", $this->style->getMarker($selected)); | ||
|
||
$length = $this->style->getDisplaysExtra() | ||
? $style->getContentWidth() - (mb_strlen($this->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)), | ||
$this->style->getItemExtra() | ||
) | ||
: $text; | ||
} | ||
|
||
return $text; | ||
}, $rows, array_keys($rows)); | ||
} | ||
|
||
/** | ||
|
@@ -34,6 +87,18 @@ public function getSelectAction() : ?callable | |
return $this->selectAction; | ||
} | ||
|
||
public function getStyle() : SelectableStyle | ||
{ | ||
return $this->style; | ||
} | ||
|
||
public function setStyle(SelectableStyle $style) : self | ||
{ | ||
$this->style = $style; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Return the raw string of text | ||
*/ | ||
|
@@ -49,4 +114,33 @@ public function setText(string $text) : void | |
{ | ||
$this->text = $text; | ||
} | ||
|
||
/** | ||
* 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; | ||
} | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's just drop these methods. It's just a little BC break.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still needed for MenuMenuItem. I can completely drop in next PR for that item's style.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok great, sounds good.