Skip to content

Structural improvements #9

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Below is an example demonstrating how to use the PHP Prompts library to create i
1. **Initialize**

```php
$prompt = new MaplePHP\Prompts\Prompt();
$prompt = new \MaplePHP\Prompts\Prompt();
```

2. **Set the Title and Description for the Prompts**
Expand Down Expand Up @@ -278,7 +278,7 @@ These prompt types enable a variety of user interactions in a CLI environment, m
You also execute some of the prompt above command directly with the Command class.

```php
$command = new MaplePHP\Prompts\Command();
$command = new \MaplePHP\Prompts\Command();
// Print messages and return input
$input = $command->message("Text field", true);
$input = $command->mask("Masked input (password)");
Expand All @@ -302,7 +302,7 @@ $command->approve("Print red error message");
The `progress` method of the `Command` class allows displaying a progress bar with customizable sleep intervals to indicate ongoing operations.

```php
$command = new MaplePHP\Prompts\Command();
$command = new \MaplePHP\Prompts\Command();
$command->progress(1, 100, function($i, $length) {
return 20;
});
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"autoload": {
"psr-4": {
"MaplePHP\\Prompts\\": ""
"MaplePHP\\Prompts\\": "src"
}
},
"minimum-stability": "dev"
Expand Down
5 changes: 2 additions & 3 deletions examples/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@

require_once("{$dir}/vendor/autoload.php");

use MaplePHP\Prompts\Prompt;
use MaplePHP\Prompts\Command;
use MaplePHP\Prompts\SttyWrapper;
use MaplePHP\Prompts\Prompt;

$command = new Command();
$inp = new Prompt();
Expand Down Expand Up @@ -123,7 +122,7 @@
});
print_r($prompt);

} catch (\MaplePHP\Prompts\PromptException $e) {
} catch (\MaplePHP\Prompts\Exceptions\PromptException $e) {
$command->error($e->getMessage());
}

Expand Down
20 changes: 17 additions & 3 deletions Command.php → src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
namespace MaplePHP\Prompts;

use Exception;
use InvalidArgumentException;
use MaplePHP\Http\Interfaces\StreamInterface;
use MaplePHP\Http\Stream;
use InvalidArgumentException;
use MaplePHP\Prompts\Themes\Ansi;

/**
* Class Command
Expand All @@ -20,11 +21,24 @@ class Command
public function __construct(?StreamInterface $stream = null)
{
// This is the stream we want to use, 9/10 times
$this->stream = is_null($stream) ? new Stream(Stream::STDOUT) : $stream;
$this->stream = $stream === null ? new Stream(Stream::STDOUT) : $stream;
$this->stty = new SttyWrapper();
$this->ansi = new Ansi();
}

/**
* With stream
*
* @param StreamInterface $stream
* @return $this
*/
public function withStream(StreamInterface $stream): self
{
$new = clone $this;
$new->stream = $stream;
return $new;
}

/**
* Get stream
*
Expand Down Expand Up @@ -360,7 +374,7 @@ protected function showMenu(int $selIndex, array $items): void
protected function write(string $message, bool $break = true): void
{
$this->stream->write($message);
if($break) {
if ($break) {
$this->stream->write("\n");
}
}
Expand Down
2 changes: 1 addition & 1 deletion PromptException.php → src/Exceptions/PromptException.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace MaplePHP\Prompts;
namespace MaplePHP\Prompts\Exceptions;

use Exception;

Expand Down
File renamed without changes.
29 changes: 16 additions & 13 deletions Prompt.php → src/Prompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace MaplePHP\Prompts;

//use MaplePHP\Prompts\Command;
use ErrorException;
use Exception;
use MaplePHP\Validate\Inp;
use InvalidArgumentException;
use MaplePHP\Prompts\Exceptions\PromptException;
use MaplePHP\Validate\Validator;

class Prompt
{
Expand All @@ -25,7 +25,7 @@ public function __construct()
}

/**
* Get command instance
* Get a command instance
*
* @return Command
*/
Expand Down Expand Up @@ -99,7 +99,9 @@ public function add(string $name, array $data): self
*
* @param array $row
* @return mixed
* @throws ErrorException
* @throws PromptException
* @throws \MaplePHP\Prompts\PromptException
* @throws Exception
*/
public function promptLine(array $row): mixed
Expand Down Expand Up @@ -153,13 +155,13 @@ public function promptLine(array $row): mixed
}
break;
case "continue":
if(is_callable($items)) {
if (is_callable($items)) {
$items = $items($this->prevVal, $this->index);
if($items === false) {
if ($items === false) {
return true;
}
}
if(!is_array($items)) {
if (!is_array($items)) {
throw new InvalidArgumentException("The items must return a a valid array");
}
$inst = new self();
Expand Down Expand Up @@ -199,20 +201,21 @@ public function promptLine(array $row): mixed
}

/**
* Prompt output and get result as array or false if aborted
* Prompt output and get the result as an array or false if aborted
*
* @return array|false
* @throws PromptException
* @throws ErrorException
* @throws PromptException|\MaplePHP\Prompts\PromptException
*/
public function prompt(): array|false
{
$result = [];
$this->index = 0;
if(!$this->disableHeaderInfo) {
if (!$this->disableHeaderInfo) {
$this->getHeaderInfo();
}
foreach ($this->data as $name => $row) {
if(!is_array($row)) {
if (!is_array($row)) {
throw new PromptException("The data array has to return an array!", 1);
}
$input = $this->promptLine($row);
Expand All @@ -235,7 +238,7 @@ public function prompt(): array|false
*/
protected function getHeaderInfo(): void
{
if(!is_null($this->helperText)) {
if ($this->helperText !== null) {
$this->command->message("\n" . $this->command->getAnsi()->italic($this->helperText . "\n"));
}

Expand All @@ -253,7 +256,7 @@ protected function getHeaderInfo(): void
}

/**
* Check if input is empty
* Check if the input is empty
*
* @param mixed $input
* @return bool
Expand Down Expand Up @@ -310,7 +313,7 @@ protected function validateItems(array|callable $validate, mixed $input, ?string
*/
final protected function validate(string $value, string $method, array $args = []): bool
{
$inp = new Inp($value);
$inp = new Validator($value);
if (!method_exists($inp, $method)) {
throw new InvalidArgumentException("The validation method \"$method\" does not exist", 1);
}
Expand Down
File renamed without changes.
10 changes: 5 additions & 5 deletions Ansi.php → src/Themes/Ansi.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace MaplePHP\Prompts;
namespace MaplePHP\Prompts\Themes;

use Exception;
use InvalidArgumentException;
Expand Down Expand Up @@ -32,7 +32,7 @@ class Ansi
*/
public function style(string|array|null $styles, string $message): string
{
if(!is_null($styles)) {
if ($styles !== null) {
if (is_string($styles)) {
$styles = [$styles];
}
Expand Down Expand Up @@ -318,7 +318,7 @@ public function brightWhite(string $message): string
*/
public function bg(int $int, string $message): string
{
if(!$this->isSupported()) {
if (!$this->isSupported()) {
return "[$message]";
}
return $this->ansiStyle($int, $message);
Expand Down Expand Up @@ -650,10 +650,10 @@ public function checkbox(): string
*/
final public function isSupported(): bool
{
if($this->disableAnsi) {
if ($this->disableAnsi) {
$this->hasAnsi = false;
}
if (is_null($this->hasAnsi)) {
if ($this->hasAnsi === null) {
if (stripos(PHP_OS, 'WIN') === 0) {
$this->hasAnsi = false;
} else {
Expand Down
Loading