Skip to content

WIP: Inputs #78

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
}
],
"require-dev": {
"phpunit/phpunit": "~5.0",
"squizlabs/php_codesniffer": "~2.0"
"phpunit/phpunit": "^7",
"squizlabs/php_codesniffer": "^3.2"
},
"require": {
"php" : ">=5.6",
"php" : "^7.2",
"beberlei/assert": "^2.4",
"ext-posix": "*"
},
Expand Down
10 changes: 8 additions & 2 deletions examples/basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@
$menu = (new CliMenuBuilder)
->setTitle('Basic CLI Menu')
->addItem('First Item', $itemCallable)
->addItem('Second Item', $itemCallable)
->addItem('Third Item', $itemCallable)
->addItem('Second Item', function (CliMenu $menu) {
$menu->getStyle()->setBg('red');
$menu->redraw();
})
->addItem('Third Item', function (CliMenu $menu) {
$menu->getStyle()->setBg('default');
$menu->redraw();
})
->addLineBreak('-')
->build();

Expand Down
32 changes: 32 additions & 0 deletions examples/input-advanced.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use PhpSchool\CliMenu\CliMenu;
use PhpSchool\CliMenu\CliMenuBuilder;

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

$itemCallable = function (CliMenu $menu) {
$username = $menu->askText()
->setPromptText('Enter username')
->setPlaceholderText('alice')
->ask();

$age = $menu->askNumber()
->setPromptText('Enter age')
->setPlaceholderText('28')
->ask();

$password = $menu->askPassword()
->setPromptText('Enter password')
->ask();

var_dump($username->fetch(), $age->fetch(), $password->fetch());
};

$menu = (new CliMenuBuilder)
->setTitle('User Manager')
->addItem('Create New User', $itemCallable)
->addLineBreak('-')
->build();

$menu->open();
24 changes: 24 additions & 0 deletions examples/input-number.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use PhpSchool\CliMenu\CliMenu;
use PhpSchool\CliMenu\CliMenuBuilder;

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

$itemCallable = function (CliMenu $menu) {
$result = $menu->askNumber()
->setPlaceholderText(10)
->ask();

var_dump($result->fetch());
};

$menu = (new CliMenuBuilder)
->setTitle('Basic CLI Menu')
->addItem('Enter number', $itemCallable)
->addItem('Second Item', $itemCallable)
->addItem('Third Item', $itemCallable)
->addLineBreak('-')
->build();

$menu->open();
24 changes: 24 additions & 0 deletions examples/input-password.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use PhpSchool\CliMenu\CliMenu;
use PhpSchool\CliMenu\CliMenuBuilder;

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

$itemCallable = function (CliMenu $menu) {
$result = $menu->askPassword()
->setPlaceholderText('')
->ask();

var_dump($result->fetch());
};

$menu = (new CliMenuBuilder)
->setTitle('Basic CLI Menu')
->addItem('Enter password', $itemCallable)
->addItem('Second Item', $itemCallable)
->addItem('Third Item', $itemCallable)
->addLineBreak('-')
->build();

$menu->open();
24 changes: 24 additions & 0 deletions examples/input-text.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use PhpSchool\CliMenu\CliMenu;
use PhpSchool\CliMenu\CliMenuBuilder;

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

$itemCallable = function (CliMenu $menu) {
$result = $menu->askText()
->setPlaceholderText('Enter something here')
->ask();

var_dump($result->fetch());
};

$menu = (new CliMenuBuilder)
->setTitle('Basic CLI Menu')
->addItem('Enter text', $itemCallable)
->addItem('Second Item', $itemCallable)
->addItem('Third Item', $itemCallable)
->addLineBreak('-')
->build();

$menu->open();
47 changes: 41 additions & 6 deletions src/CliMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

namespace PhpSchool\CliMenu;

use PhpSchool\CliMenu\Dialogue\NumberInput;
use PhpSchool\CliMenu\Exception\InvalidInstantiationException;
use PhpSchool\CliMenu\Exception\InvalidTerminalException;
use PhpSchool\CliMenu\Exception\MenuNotOpenException;
use PhpSchool\CliMenu\Input\InputIO;
use PhpSchool\CliMenu\Input\Number;
use PhpSchool\CliMenu\Input\Password;
use PhpSchool\CliMenu\Input\Text;
use PhpSchool\CliMenu\MenuItem\LineBreakItem;
use PhpSchool\CliMenu\MenuItem\MenuItemInterface;
use PhpSchool\CliMenu\MenuItem\StaticItem;
Expand Down Expand Up @@ -406,9 +411,7 @@ public function getCurrentFrame()
*/
public function flash($text)
{
if (strpos($text, "\n") !== false) {
throw new \InvalidArgumentException;
}
$this->guardSingleLine($text);

$style = (new MenuStyle($this->terminal))
->setBg('yellow')
Expand All @@ -423,14 +426,46 @@ public function flash($text)
*/
public function confirm($text)
{
if (strpos($text, "\n") !== false) {
throw new \InvalidArgumentException;
}
$this->guardSingleLine($text);

$style = (new MenuStyle($this->terminal))
->setBg('yellow')
->setFg('red');

return new Confirm($this, $style, $this->terminal, $text);
}

public function askNumber() : Number
{
$style = (new MenuStyle($this->terminal))
->setBg('yellow')
->setFg('red');

return new Number(new InputIO($this, $style, $this->terminal));
}

public function askText() : Text
{
$style = (new MenuStyle($this->terminal))
->setBg('yellow')
->setFg('red');

return new Text(new InputIO($this, $style, $this->terminal));
}

public function askPassword() : Password
{
$style = (new MenuStyle($this->terminal))
->setBg('yellow')
->setFg('red');

return new Password(new InputIO($this, $style, $this->terminal));
}

private function guardSingleLine($text)
{
if (strpos($text, "\n") !== false) {
throw new \InvalidArgumentException;
}
}
}
27 changes: 27 additions & 0 deletions src/Input/Input.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace PhpSchool\CliMenu\Input;

/**
* @author Aydin Hassan <[email protected]>
*/
interface Input
{
public function ask() : InputResult;

public function validate(string $input) : bool;

public function setPromptText(string $promptText) : Input;

public function getPromptText() : string;

public function setValidationFailedText(string $validationFailedText) : Input;

public function getValidationFailedText() : string;

public function setPlaceholderText(string $placeholderText) : Input;

public function getPlaceholderText() : string;

public function format(string $value) : string;
}
Loading