<?php declare(strict_types=1);
namespace Inhere\Console\IO;
use Inhere\Console\Contract\OutputInterface;
use Toolkit\Cli\Style;
use Inhere\Console\Console;
use Inhere\Console\Concern\FormatOutputAwareTrait;
use Toolkit\Cli\Cli;
class Output implements OutputInterface
{
use FormatOutputAwareTrait;
protected $outputStream;
protected $errorStream;
protected $style;
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();
}
public function startBuffer(): void
{
Console::startBuffer();
}
public function clearBuffer(): void
{
Console::clearBuffer();
}
public function stopBuffer(bool $flush = true, $nl = false, $quit = false, array $opts = []): void
{
Console::stopBuffer($flush, $nl, $quit, $opts);
}
public function flush(bool $nl = false, $quit = false, array $opts = []): void
{
Console::flushBuffer($nl, $quit, $opts);
}
public function read(string $question = '', bool $nl = false): string
{
return Console::read($question, $nl);
}
public function stderr(string $text = '', $nl = true): int
{
return Console::write($text, $nl, [
'steam' => $this->errorStream,
]);
}
public function getStyle(): Style
{
if (!$this->style) {
$this->style = Style::instance();
}
return $this->style;
}
public function supportColor(): bool
{
return Cli::isSupportColor();
}
public function getOutputStream()
{
return $this->outputStream;
}
public function setOutputStream($outStream): self
{
$this->outputStream = $outStream;
return $this;
}
public function getErrorStream()
{
return $this->errorStream;
}
public function setErrorStream($errorStream): self
{
$this->errorStream = $errorStream;
return $this;
}
}