<?php declare(strict_types=1);
namespace Inhere\Console\Concern;
use Closure;
use Generator;
use Inhere\Console\Component\Formatter\HelpPanel;
use Inhere\Console\Component\Formatter\MultiList;
use Inhere\Console\Component\Formatter\Panel;
use Inhere\Console\Component\Formatter\Section;
use Inhere\Console\Component\Formatter\SingleList;
use Inhere\Console\Component\Formatter\Table;
use Inhere\Console\Component\Formatter\Title;
use Toolkit\Cli\Style;
use Inhere\Console\Console;
use Inhere\Console\Util\Interact;
use Inhere\Console\Util\Show;
use LogicException;
use Toolkit\Stdlib\Php;
use function array_merge;
use function json_encode;
use function method_exists;
use function sprintf;
use function strpos;
use function substr;
trait FormatOutputAwareTrait
{
public function write($messages, $nl = true, $quit = false, array $opts = []): int
{
return Console::write($messages, $nl, $quit, array_merge([
'flush' => true,
'stream' => $this->outputStream,
], $opts));
}
public function writef(string $format, ...$args): void
{
Console::printf($format, ...$args);
}
public function printf(string $format, ...$args): void
{
Console::printf($format, ...$args);
}
public function writeln($text, $quit = false, array $opts = []): int
{
return Console::writeln($text, $quit, $opts);
}
public function println($text, $quit = false, array $opts = []): int
{
return Console::writeln($text, $quit, $opts);
}
public function writeRaw($text, bool $nl = true, $quit = false, array $opts = []): int
{
return Console::writeRaw($text, $nl, $quit, $opts);
}
public function colored(string $text, string $tag = 'info'): int
{
return $this->writeln(sprintf('<%s>%s</%s>', $tag, $text, $tag));
}
public function block($messages, string $type = 'MESSAGE', string $style = Style::NORMAL, $quit = false): int
{
return Show::block($messages, $type, $style, $quit);
}
public function liteBlock($messages, string $type = 'MESSAGE', string $style = Style::NORMAL, $quit = false): int
{
return Show::liteBlock($messages, $type, $style, $quit);
}
public function title(string $title, array $opts = []): void
{
Title::show($title, $opts);
}
public function section(string $title, $body, array $opts = []): void
{
Section::show($title, $body, $opts);
}
public function aList($data, string $title = 'Information', array $opts = []): void
{
SingleList::show($data, $title, $opts);
}
public function multiList(array $data, array $opts = []): void
{
MultiList::show($data, $opts);
}
public function mList(array $data, array $opts = []): void
{
MultiList::show($data, $opts);
}
public function helpPanel(array $config): void
{
HelpPanel::show($config);
}
public function panel(array $data, string $title = 'Information panel', array $opts = []): void
{
Panel::show($data, $title, $opts);
}
public function table(array $data, string $title = 'Data Table', array $opts = []): void
{
Table::show($data, $title, $opts);
}
public function progressTxt(int $total, string $msg, string $doneMsg = ''): Generator
{
return Show::progressTxt($total, $msg, $doneMsg);
}
public function progressBar($total, array $opts = []): Generator
{
return Show::progressBar($total, $opts);
}
public function __call($method, array $args = [])
{
$map = Show::getBlockMethods(false);
if (isset($map[$method])) {
$msg = $args[0];
$quit = $args[1] ?? false;
$style = $map[$method];
if (0 === strpos($method, 'lite')) {
$type = substr($method, 4);
return Show::liteBlock($msg, $type === 'Primary' ? 'IMPORTANT' : $type, $style, $quit);
}
return Show::block($msg, $style === 'primary' ? 'IMPORTANT' : $style, $style, $quit);
}
if (method_exists(Show::class, $method)) {
return Show::$method(...$args);
}
if (method_exists(Interact::class, $method)) {
return Interact::$method(...$args);
}
throw new LogicException("Call a not exists method: $method of the " . static::class);
}
public function json(
$data,
bool $echo = true,
int $flags = JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
) {
$string = json_encode($data, $flags);
if ($echo) {
return Console::write($string);
}
return $string;
}
public function dump(...$vars): void
{
Console::write(Php::dumpVars(...$vars));
}
public function prints(...$vars): void
{
Console::write(Php::printVars(...$vars));
}
}