-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from php-school/dialouges
Dialogue feature
- Loading branch information
Showing
21 changed files
with
1,094 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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 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,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(); |
This file contains 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,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(); |
This file contains 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 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,85 @@ | ||
<?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%s%s\n", | ||
$this->style->getUnselectedSetCode(), | ||
str_repeat(' ', $this->style->getPadding()), | ||
$this->text, | ||
str_repeat(' ', $this->style->getPadding()), | ||
$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( | ||
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%s%s\n", | ||
$this->style->getUnselectedSetCode(), | ||
str_repeat(' ', $this->style->getPadding()), | ||
str_repeat(' ', mb_strlen($this->text)), | ||
str_repeat(' ', $this->style->getPadding()), | ||
$this->style->getUnselectedUnsetCode() | ||
)); | ||
|
||
$this->terminal->moveCursorToTop(); | ||
$input = $this->terminal->getKeyedInput(); | ||
|
||
while ($input !== 'enter') { | ||
$input = $this->terminal->getKeyedInput(); | ||
} | ||
|
||
$this->parentMenu->redraw(); | ||
} | ||
} |
Oops, something went wrong.