-
Notifications
You must be signed in to change notification settings - Fork 106
Dialogue feature #49
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
Dialogue feature #49
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
use PhpSchool\CliMenu\CliMenu; | ||
use PhpSchool\CliMenu\CliMenuBuilder; | ||
|
||
require_once(__DIR__ . '/../vendor/autoload.php'); | ||
|
||
$itemCallable = function (CliMenu $menu) { | ||
$menu->confirm('PHP School FTW!') | ||
->display('OK'); | ||
}; | ||
|
||
$menu = (new CliMenuBuilder) | ||
->setTitle('Basic CLI Menu') | ||
->addItem('First Item', $itemCallable) | ||
->addItem('Second Item', $itemCallable) | ||
->addItem('Third Item', $itemCallable) | ||
->addLineBreak('-') | ||
->build(); | ||
|
||
$menu->open(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
use PhpSchool\CliMenu\CliMenu; | ||
use PhpSchool\CliMenu\CliMenuBuilder; | ||
|
||
require_once(__DIR__ . '/../vendor/autoload.php'); | ||
|
||
$itemCallable = function (CliMenu $menu) { | ||
$flash = $menu->flash("PHP School FTW!!"); | ||
$flash->getStyle()->setBg('green'); | ||
$flash->display(); | ||
}; | ||
|
||
$menu = (new CliMenuBuilder) | ||
->setTitle('Basic CLI Menu') | ||
->addItem('First Item', $itemCallable) | ||
->addItem('Second Item', $itemCallable) | ||
->addItem('Third Item', $itemCallable) | ||
->addLineBreak('-') | ||
->build(); | ||
|
||
$menu->open(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace PhpSchool\CliMenu\Dialogue; | ||
|
||
/** | ||
* @author Aydin Hassan <[email protected]> | ||
*/ | ||
class Confirm extends Dialogue | ||
{ | ||
|
||
/** | ||
* Display confirmation with a button with the given text | ||
* | ||
* @param string $confirmText | ||
*/ | ||
public function display($confirmText = 'OK') | ||
{ | ||
$this->assertMenuOpen(); | ||
|
||
$this->terminal->moveCursorToRow($this->y); | ||
|
||
$promptWidth = mb_strlen($this->text) + 4; | ||
|
||
$this->emptyRow(); | ||
|
||
$this->write(sprintf( | ||
"%s %s %s\n", | ||
$this->style->getUnselectedSetCode(), | ||
$this->text, | ||
$this->style->getUnselectedUnsetCode() | ||
)); | ||
|
||
$this->emptyRow(); | ||
|
||
$confirmText = sprintf(' < %s > ', $confirmText); | ||
$leftFill = ($promptWidth / 2) - (mb_strlen($confirmText) / 2); | ||
|
||
$this->write(sprintf( | ||
'%s%s%s', | ||
$this->style->getUnselectedSetCode(), | ||
str_repeat(' ', $leftFill), | ||
$this->style->getUnselectedSetCode() | ||
)); | ||
|
||
$this->write( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we be handling long messages, e.g. wrap over multiple lines like we do with the menu items? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ATM - we only allow one line message and long message won't wrap - it was a bit hard for the calculating logic as I was rushing. We can add this in the future with a bit more time without breaking BC. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah no worries sounds good to me. it would complicate centering height etc too so a nice little challenge in the future |
||
sprintf( | ||
'%s%s%s', | ||
$this->style->getSelectedSetCode(), | ||
$confirmText, | ||
$this->style->getSelectedUnsetCode() | ||
), | ||
-1 | ||
); | ||
|
||
$this->write( | ||
sprintf( | ||
"%s%s%s\n", | ||
$this->style->getUnselectedSetCode(), | ||
str_repeat(' ', ceil($promptWidth - $leftFill - mb_strlen($confirmText))), | ||
$this->style->getSelectedUnsetCode() | ||
), | ||
-1 | ||
); | ||
|
||
$this->write(sprintf( | ||
"%s %s %s\n", | ||
$this->style->getUnselectedSetCode(), | ||
str_repeat(' ', mb_strlen($this->text)), | ||
$this->style->getUnselectedUnsetCode() | ||
)); | ||
|
||
$this->terminal->moveCursorToTop(); | ||
$input = $this->terminal->getKeyedInput(); | ||
|
||
while ($input !== 'enter') { | ||
$input = $this->terminal->getKeyedInput(); | ||
} | ||
|
||
$this->parentMenu->redraw(); | ||
} | ||
} |
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.
We should probably make this customizable (one for a minor release later though imo, unless it would be a 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.
You can grab the style from the Flash object and change it before you call
display
:). Checkout the example 😄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.
of course ... that didn't click .. nice!