Skip to content

Commit

Permalink
perf(io): update some use io class logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Dec 17, 2021
1 parent 07a1e51 commit 5b887a4
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 72 deletions.
2 changes: 1 addition & 1 deletion resource/deprecated/AbstractInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*
* @package Inhere\Console\IO
*/
abstract class AbstractInput implements InputInterface
abstract class AbstractInput // implements InputInterface
{
use InputArgumentsTrait, InputOptionsTrait;

Expand Down
16 changes: 7 additions & 9 deletions src/AbstractApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
use Inhere\Console\Concern\StyledOutputAwareTrait;
use Inhere\Console\Contract\ApplicationInterface;
use Inhere\Console\Contract\ErrorHandlerInterface;
use Inhere\Console\Contract\InputInterface;
use Inhere\Console\Contract\OutputInterface;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Inhere\Console\Util\Helper;
Expand Down Expand Up @@ -370,10 +368,10 @@ public function runWithArgs(array $args): mixed
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @param Input $input
* @param Output $output
*/
public function runWithIO(InputInterface $input, OutputInterface $output): void
public function runWithIO(Input $input, Output $output): void
{
$app = $this->copy();
$app->setInput($input);
Expand All @@ -384,13 +382,13 @@ public function runWithIO(InputInterface $input, OutputInterface $output): void
}

/**
* @param string $command
* @param InputInterface $input
* @param OutputInterface $output
* @param string $command
* @param Input $input
* @param Output $output
*
* @return mixed
*/
public function subRun(string $command, InputInterface $input, OutputInterface $output): mixed
public function subRun(string $command, Input $input, Output $output): mixed
{
$app = $this->copy();
$app->setInput($input);
Expand Down
9 changes: 0 additions & 9 deletions src/Concern/FormatOutputAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,6 @@ public function write($messages, $nl = true, $quit = false, array $opts = []): i
], $opts));
}

// public function print(...$args): void
// {
// if (count($args) > 1) {
// echo ;
// }
//
//
// }

/**
* @param ...$args
*/
Expand Down
33 changes: 15 additions & 18 deletions src/Concern/InputOutputAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@
namespace Inhere\Console\Concern;

use Inhere\Console\IO\Input;
use Inhere\Console\Contract\InputInterface;
use Inhere\Console\IO\Output;
use Inhere\Console\Contract\OutputInterface;
use Toolkit\PFlag\FlagsParser;
use Toolkit\PFlag\SFlags;

/**
* Class InputOutputAwareTrait
Expand All @@ -29,14 +26,14 @@ trait InputOutputAwareTrait
protected ?FlagsParser $flags;

/**
* @var InputInterface|null
* @var Input|null
*/
protected ?InputInterface $input;
protected ?Input $input;

/**
* @var OutputInterface|null
* @var Output|null
*/
protected ?OutputInterface $output;
protected ?Output $output;

/**
* @return string
Expand Down Expand Up @@ -64,7 +61,7 @@ public function getScriptName(): string

/**
* @param string $question
* @param bool $nl
* @param bool $nl
*
* @return string
*/
Expand Down Expand Up @@ -94,47 +91,47 @@ public function writeln(mixed $message): int
}

/**
* @return Input|InputInterface
* @return Input
*/
public function getInput(): InputInterface
public function getInput(): Input
{
return $this->input;
}

/**
* @param InputInterface $input
* @param Input $input
*/
public function setInput(InputInterface $input): void
public function setInput(Input $input): void
{
$this->input = $input;
}

/**
* @return Output|OutputInterface
* @return Output
*/
public function getOutput(): OutputInterface
public function getOutput(): Output
{
return $this->output;
}

/**
* @param Output|OutputInterface $output
* @param Output $output
*/
public function setOutput(OutputInterface $output): void
public function setOutput(Output $output): void
{
$this->output = $output;
}

/**
* @return FlagsParser|SFlags
* @return FlagsParser
*/
public function getFlags(): FlagsParser
{
return $this->flags;
}

/**
* @param FlagsParser|SFlags $flags
* @param FlagsParser $flags
*/
public function setFlags(FlagsParser $flags): void
{
Expand Down
1 change: 0 additions & 1 deletion src/Contract/OutputInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* Class OutputInterface
*
* @package Inhere\Console\Contract
* @method error(string $string)
*/
interface OutputInterface
{
Expand Down
18 changes: 2 additions & 16 deletions src/IO/Output.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,12 @@ class Output extends StreamOutput
*/
protected $errorStream;

/**
* 控制台窗口(字体/背景)颜色添加处理
* window colors
*
* @var Style|null
*/
protected ?Style $style = null;

/**
* Output constructor.
*/
public function __construct()
{
parent::__construct(Cli::getOutputStream());

$this->getStyle();
}

/***************************************************************************
Expand Down Expand Up @@ -136,7 +126,7 @@ public function readln(string $question = '', bool $nl = false): string
*/
public function stderr(string $text = '', bool $nl = true): int
{
return Console::write($text, $nl, [
return Console::write($text, $nl, false, [
'steam' => $this->errorStream,
]);
}
Expand All @@ -150,11 +140,7 @@ public function stderr(string $text = '', bool $nl = true): int
*/
public function getStyle(): Style
{
if (!$this->style) {
$this->style = Style::instance();
}

return $this->style;
return Style::global();
}

/**
Expand Down
35 changes: 17 additions & 18 deletions src/Util/ProgressBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use Closure;
use Inhere\Console\IO\Output;
use Inhere\Console\Contract\OutputInterface;
use LogicException;
use RuntimeException;
use Toolkit\Stdlib\Helper\Format;
Expand All @@ -24,7 +23,7 @@
* @package Inhere\Console\Util
* @form \Symfony\Component\Console\Helper\ProgressBar
*
* ```
* ```bash
* 1 [->--------------------------]
* 3 [■■■>------------------------]
* 25/50 [==============>-------------] 50%
Expand Down Expand Up @@ -121,9 +120,9 @@ class ProgressBar
private bool $firstRun = true;

/**
* @var OutputInterface
* @var Output
*/
private Output|OutputInterface $output;
private Output $output;

/**
* messages
Expand All @@ -143,21 +142,21 @@ class ProgressBar
public const DEFAULT_FORMAT = '[{@bar}] {@percent:3s}%({@current}/{@max}) {@elapsed:6s}/{@estimated:-6s} {@memory:6s}';

/**
* @param OutputInterface|null $output
* @param int $maxSteps
* @param Output|null $output
* @param int $maxSteps
*
* @return ProgressBar
*/
public static function create(OutputInterface $output = null, int $maxSteps = 0): ProgressBar
public static function create(Output $output = null, int $maxSteps = 0): ProgressBar
{
return new self($output, $maxSteps);
}

/**
* @param OutputInterface|null $output
* @param Output|null $output
* @param int $maxSteps
*/
public function __construct(OutputInterface $output = null, int $maxSteps = 0)
public function __construct(Output $output = null, int $maxSteps = 0)
{
$this->output = $output ?: new Output;

Expand All @@ -168,11 +167,11 @@ public function __construct(OutputInterface $output = null, int $maxSteps = 0)
/**
* 开始
*
* @param null $maxSteps
* @param int|null $maxSteps
*
* @throws LogicException
*/
public function start($maxSteps = null): void
public function start(int $maxSteps = null): void
{
if ($this->started) {
throw new LogicException('Progress bar already started.');
Expand Down Expand Up @@ -260,7 +259,7 @@ public function finish(): void
*/
public function display(): void
{
if (null === $this->format) {
if (!$this->format) {
$this->format = self::DEFAULT_FORMAT;
}

Expand Down Expand Up @@ -306,10 +305,10 @@ public function render(string $text): void
}

/**
* @return mixed
* @return string
* @throws RuntimeException
*/
protected function buildLine(): mixed
protected function buildLine(): string
{
// $regex = "{%([a-z\-_]+)(?:\:([^%]+))?%}i";
return preg_replace_callback('/{@([\w]+)(?:\:([\w-]+))?}/i', function ($matches) {
Expand Down Expand Up @@ -543,17 +542,17 @@ public function getPercent(): float
}

/**
* @return mixed
* @return int
*/
public function getStartTime(): mixed
public function getStartTime(): int
{
return $this->startTime;
}

/**
* @return mixed
* @return int
*/
public function getFinishTime(): mixed
public function getFinishTime(): int
{
return $this->finishTime;
}
Expand Down

0 comments on commit 5b887a4

Please sign in to comment.