Skip to content

Commit

Permalink
fix some strict error for formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jun 18, 2020
1 parent 158755a commit f9f00b3
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 71 deletions.
4 changes: 2 additions & 2 deletions src/Component/Formatter/Padding.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use Inhere\Console\Console;
use Inhere\Console\Util\Helper;
use Toolkit\Cli\ColorTag;
use Toolkit\Stdlib\Str;
use function array_merge;
use function str_pad;
use function trim;
use function ucfirst;

Expand Down Expand Up @@ -50,7 +50,7 @@ public static function show(array $data, string $title = '', array $opts = []):

foreach ($data as $label => $value) {
$value = ColorTag::wrap((string)$value, $opts['valueStyle']);
$string .= $opts['indent'] . str_pad((string)$label, $paddingLen, $opts['char']) . " $value\n";
$string .= $opts['indent'] . Str::pad($label, $paddingLen, $opts['char']) . " $value\n";
}

Console::write(trim($string));
Expand Down
21 changes: 12 additions & 9 deletions src/Component/Formatter/Panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Inhere\Console\Component\MessageFormatter;
use Inhere\Console\Console;
use Inhere\Console\Util\FormatUtil;
use Toolkit\Stdlib\Str;
use Toolkit\Stdlib\Str\StrBuffer;
use function array_filter;
use function array_merge;
Expand All @@ -20,7 +21,6 @@
use function is_numeric;
use function mb_strlen;
use function rtrim;
use function str_pad;
use function strip_tags;
use function trim;
use function ucwords;
Expand Down Expand Up @@ -117,7 +117,8 @@ public static function show($data, string $title = 'Information Panel', array $o
foreach ($data as $label => $value) {
// label exists
if (!is_numeric($label)) {
$width = mb_strlen($label, 'UTF-8');
$width = Str::len2($label, 'UTF-8');

$labelMaxWidth = $width > $labelMaxWidth ? $width : $labelMaxWidth;
}

Expand Down Expand Up @@ -158,16 +159,17 @@ public static function show($data, string $title = 'Information Panel', array $o

// output title
if ($title) {
$title = ucwords($title);
$title = ucwords($title);

$titleLength = mb_strlen($title, 'UTF-8');
$panelWidth = $panelWidth > $titleLength ? $panelWidth : $titleLength;
$indentSpace = str_pad(' ', ceil($panelWidth / 2) - ceil($titleLength / 2) + 2 * 2, ' ');
$indentSpace = Str::pad(' ', ceil($panelWidth / 2) - ceil($titleLength / 2) + 2 * 2, ' ');
Console::write(" {$indentSpace}<bold>{$title}</bold>");
}

// output panel top border
if ($borderChar) {
$border = str_pad($borderChar, $panelWidth + (3 * 3), $borderChar);
$border = Str::pad($borderChar, $panelWidth + (3 * 3), $borderChar);
Console::write(' ' . $border);
}

Expand Down Expand Up @@ -215,7 +217,8 @@ public function format(): string
foreach ($data as $label => $value) {
// label exists
if (!is_numeric($label)) {
$width = mb_strlen($label, 'UTF-8');
$width = Str::len2($label, 'UTF-8');

$labelMaxWidth = $width > $labelMaxWidth ? $width : $labelMaxWidth;
}

Expand Down Expand Up @@ -257,13 +260,13 @@ public function format(): string
$title = ucwords($title);
$titleLength = mb_strlen($title, 'UTF-8');
$panelWidth = $panelWidth > $titleLength ? $panelWidth : $titleLength;
$indentSpace = str_pad(' ', ceil($panelWidth / 2) - ceil($titleLength / 2) + 2 * 2, ' ');
$indentSpace = Str::pad(' ', ceil($panelWidth / 2) - ceil($titleLength / 2) + 2 * 2, ' ');
$buffer->write(" {$indentSpace}<bold>{$title}</bold>\n");
}

// output panel top border
if ($topBorder = $this->titleBorder) {
$border = str_pad($topBorder, $panelWidth + (3 * 3), $topBorder);
$border = Str::pad($topBorder, $panelWidth + (3 * 3), $topBorder);
$buffer->write(' ' . $border . PHP_EOL);
}

Expand All @@ -280,7 +283,7 @@ public function format(): string

// output panel bottom border
if ($footBorder = $this->footerBorder) {
$border = str_pad($footBorder, $panelWidth + (3 * 3), $footBorder);
$border = Str::pad($footBorder, $panelWidth + (3 * 3), $footBorder);
$buffer->write(' ' . $border . PHP_EOL);
}

Expand Down
7 changes: 3 additions & 4 deletions src/Component/Formatter/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use function ceil;
use function implode;
use function is_array;
use function str_pad;
use function trim;
use function ucwords;
use const PHP_EOL;
Expand Down Expand Up @@ -52,9 +51,9 @@ public static function show(string $title, $body, array $opts = []): void
if ($tLength >= $width) {
$titleIndent = Str::pad(self::CHAR_SPACE, $indent, self::CHAR_SPACE);
} elseif ($opts['titlePos'] === self::POS_RIGHT) {
$titleIndent = str_pad(self::CHAR_SPACE, ceil($width - $tLength) + $indent, self::CHAR_SPACE);
$titleIndent = Str::pad(self::CHAR_SPACE, ceil($width - $tLength) + $indent, self::CHAR_SPACE);
} elseif ($opts['titlePos'] === self::POS_MIDDLE) {
$titleIndent = str_pad(self::CHAR_SPACE, ceil(($width - $tLength) / 2) + $indent, self::CHAR_SPACE);
$titleIndent = Str::pad(self::CHAR_SPACE, ceil(($width - $tLength) / 2) + $indent, self::CHAR_SPACE);
} else {
$titleIndent = Str::pad(self::CHAR_SPACE, $indent, self::CHAR_SPACE);
}
Expand All @@ -67,7 +66,7 @@ public static function show(string $title, $body, array $opts = []): void
$showBBorder = (bool)$opts['bottomBorder'];

if ($showTBorder || $showBBorder) {
$border = str_pad($char, $width, $char);
$border = Str::pad($char, $width, $char);

if ($showTBorder) {
$topBorder = "{$indentStr}$border\n";
Expand Down
12 changes: 6 additions & 6 deletions src/Component/Formatter/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Inhere\Console\Component\MessageFormatter;
use Inhere\Console\Console;
use Toolkit\Cli\ColorTag;
use Toolkit\Stdlib\Str;
use Toolkit\Stdlib\Str\StrBuffer;
use function array_keys;
use function array_merge;
Expand All @@ -19,7 +20,6 @@
use function count;
use function is_string;
use function mb_strlen;
use function str_pad;
use function ucwords;

/**
Expand Down Expand Up @@ -164,11 +164,11 @@ public static function show(array $data, string $title = 'Data Table', array $op
$tStyle = $opts['titleStyle'] ?: 'bold';
$title = ucwords(trim($title));
$titleLength = mb_strlen($title, 'UTF-8');
$indentSpace = str_pad(' ', ceil($tableWidth / 2) - ceil($titleLength / 2) + ($columnCount * 2), ' ');
$indentSpace = Str::pad(' ', ceil($tableWidth / 2) - ceil($titleLength / 2) + ($columnCount * 2), ' ');
$buf->write(" {$indentSpace}<$tStyle>{$title}</$tStyle>\n");
}

$border = $leftIndent . str_pad($rowBorderChar, $tableWidth + ($columnCount * 3) + 2, $rowBorderChar);
$border = $leftIndent . Str::pad($rowBorderChar, $tableWidth + ($columnCount * 3) + 2, $rowBorderChar);

// output table top border
if ($showBorder) {
Expand All @@ -184,7 +184,7 @@ public static function show(array $data, string $title = 'Data Table', array $op
foreach ($head as $index => $name) {
$colMaxWidth = $info['columnMaxWidth'][$index];
// format
$name = str_pad($name, $colMaxWidth, ' ');
$name = Str::pad($name, $colMaxWidth, ' ');
$name = ColorTag::wrap($name, $opts['headStyle']);
$headStr .= " {$name} {$colBorderChar}";
}
Expand All @@ -193,7 +193,7 @@ public static function show(array $data, string $title = 'Data Table', array $op

// head border: split head and body
if ($headBorderChar = $opts['headBorderChar']) {
$headBorder = $leftIndent . str_pad(
$headBorder = $leftIndent . Str::pad(
$headBorderChar,
$tableWidth + ($columnCount * 3) + 2,
$headBorderChar
Expand All @@ -212,7 +212,7 @@ public static function show(array $data, string $title = 'Data Table', array $op
foreach ((array)$row as $value) {
$colMaxWidth = $info['columnMaxWidth'][$colIndex];
// format
$value = str_pad($value, $colMaxWidth, ' ');
$value = Str::pad($value, $colMaxWidth, ' ');
$value = ColorTag::wrap($value, $opts['bodyStyle']);
$rowStr .= " {$value} {$colBorderChar}";
$colIndex++;
Expand Down
3 changes: 1 addition & 2 deletions src/Component/Formatter/Title.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Toolkit\Sys\Sys;
use function array_merge;
use function ceil;
use function str_pad;

/**
* Class Title
Expand Down Expand Up @@ -63,7 +62,7 @@ public static function show(string $title, array $opts = []): void
}

$titleLine = "$titleIndent<bold>$title</bold>\n";
$border = $indentStr . str_pad($char, $width, $char);
$border = $indentStr . Str::pad($char, $width, $char);

Console::write($titleLine . $border);
}
Expand Down
15 changes: 11 additions & 4 deletions src/IO/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Inhere\Console\IO;

use Toolkit\Cli\Cli;
use Toolkit\Cli\Flags;
use function array_map;
use function array_shift;
Expand All @@ -17,8 +18,6 @@
use function implode;
use function preg_match;
use function trim;
use const STDIN;
use const STDOUT;

/**
* Class Input - The input information. by parse global var $argv.
Expand All @@ -36,9 +35,11 @@ class Input extends AbstractInput
protected $commandId = '';

/**
* Default is STDIN
*
* @var resource
*/
protected $inputStream = STDIN;
protected $inputStream;

/**
* Input constructor.
Expand All @@ -52,6 +53,7 @@ public function __construct(array $args = null, bool $parsing = true)
$args = $_SERVER['argv'];
}

$this->inputStream = Cli::getInputStream();
$this->collectInfo($args);

if ($parsing) {
Expand Down Expand Up @@ -91,6 +93,11 @@ protected function doParse(array $args): void
$this->findCommand();
}

public function resetInputStream(): void
{
$this->inputStream = Cli::getInputStream();
}

/**
* @return string
*/
Expand Down Expand Up @@ -130,7 +137,7 @@ protected function tokenEscape(string $token): string
public function read(string $question = '', bool $nl = false): string
{
if ($question) {
fwrite(STDOUT, $question . ($nl ? "\n" : ''));
fwrite(Cli::getOutputStream(), $question . ($nl ? "\n" : ''));
}

return trim(fgets($this->inputStream));
Expand Down
4 changes: 1 addition & 3 deletions src/IO/Input/ArrayInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

use Inhere\Console\IO\Input;
use Toolkit\Cli\Flags;
use function array_shift;
use function implode;

/**
* Class ArrayInput
Expand All @@ -31,7 +29,7 @@ public function __construct(array $args = null, bool $parsing = true)
parent::__construct([], false);

if ($parsing && $args) {
$this->doParse($this->flags);
$this->doParse($this->flags);
}
}

Expand Down
15 changes: 11 additions & 4 deletions src/IO/Output.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ class Output implements \Inhere\Console\Contract\OutputInterface
use FormatOutputAwareTrait;

/**
* Normal output stream
* Normal output stream. Default is STDOUT
*
* @var resource
*/
protected $outputStream = STDOUT;
protected $outputStream;

/**
* Error output stream
* Error output stream. Default is STDERR
*
* @var resource
*/
protected $errorStream = STDERR;
protected $errorStream;

/**
* 控制台窗口(字体/背景)颜色添加处理
Expand All @@ -55,11 +55,18 @@ public function __construct($outputStream = null)
{
if ($outputStream) {
$this->outputStream = $outputStream;
} else {
$this->outputStream = Cli::getOutputStream();
}

$this->getStyle();
}

public function resetOutputStream(): void
{
$this->outputStream = Cli::getOutputStream();
}

/***************************************************************************
* Output buffer
***************************************************************************/
Expand Down
23 changes: 7 additions & 16 deletions src/IO/Output/BufferOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Inhere\Console\IO\Output;

use Inhere\Console\IO\Output;
use function fopen;

/**
* Class BufferOutput
Expand All @@ -11,23 +12,13 @@
*/
class BufferOutput extends Output
{
/**
* @var array
*/
private $buffer = [];

/**
* @param $messages
* @param bool $nl
* @param bool $quit
* @param array $opts
*
* @return int
*/
public function write($messages, $nl = true, $quit = false, array $opts = []): int
public function __construct()
{
$this->buffer[] = $messages;
parent::__construct(fopen('php://memory', 'rwb'));
}

return 0;
public function getBuffer(): string
{
return '';
}
}
Loading

0 comments on commit f9f00b3

Please sign in to comment.