Skip to content

Commit 16186db

Browse files
committed
accepts stream
1 parent 18055da commit 16186db

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"license": "MIT",
1616
"require": {
1717
"php": "^7.2",
18-
"alecrabbit/php-cli-tools": "^0.1"
18+
"alecrabbit/php-cli-tools": "^0.2"
1919
},
2020
"require-dev": {
2121
"alecrabbit/php-helpers": "^0.5 || ^0.6",
@@ -33,5 +33,5 @@
3333
"AlecRabbit\\Tests\\": "tests"
3434
}
3535
},
36-
"minimum-stability": "beta"
36+
"minimum-stability": "alpha"
3737
}

src/ConsoleColour/ConsoleColor.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace AlecRabbit\ConsoleColour;
44

5+
use AlecRabbit\Cli\Tools\Core\TerminalStatic;
56
use AlecRabbit\Cli\Tools\Terminal;
67
use AlecRabbit\ConsoleColour\Contracts\Styles;
78
use AlecRabbit\ConsoleColour\Core\Contracts\ConsoleColorInterface;
@@ -42,6 +43,20 @@ protected function setColorSupport(bool $force, bool $force256Colors): void
4243
$this->are256ColorsSupported = $this->supported && ($force256Colors || $terminal->color() >= COLOR256_TERMINAL);
4344
}
4445

46+
/**
47+
* @param null|bool|resource $stream
48+
* @param null|bool $force
49+
* @param bool $force256Colors
50+
*/
51+
public function setStream($stream = null, ?bool $force = null, bool $force256Colors = false): void
52+
{
53+
if (\is_resource($stream)) {
54+
$colorSupport = TerminalStatic::colorSupport($stream);
55+
$this->supported = $force || ($colorSupport >= COLOR_TERMINAL);
56+
$this->are256ColorsSupported = $this->supported && ($force256Colors || $colorSupport >= COLOR256_TERMINAL);
57+
}
58+
}
59+
4560
/** {@inheritdoc} */
4661
public function apply($styles, $text): string
4762
{
@@ -163,7 +178,7 @@ protected function isValid($style): bool
163178
}
164179

165180
/**
166-
* @param int|string|array $styles
181+
* @param mixed $styles
167182
* @return array
168183
*/
169184
protected function refineStyles($styles): array
@@ -235,7 +250,7 @@ public function addTheme($name, $styles, bool $override = false): void
235250
if (\array_key_exists($name, $this->themes) && false === $override) {
236251
throw new \RuntimeException('Theme [' . $name . '] is already set.');
237252
}
238-
253+
239254
$styles = $this->refineStyles($styles);
240255

241256
foreach ($styles as $style) {

src/ConsoleColour/Core/AbstractThemes.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use AlecRabbit\ConsoleColour\ConsoleColor;
66
use AlecRabbit\ConsoleColour\Exception\InvalidStyleException;
7+
use Symfony\Component\Console\Exception\InvalidArgumentException;
78

89
abstract class AbstractThemes
910
{
@@ -19,11 +20,13 @@ abstract class AbstractThemes
1920
/**
2021
* Themed constructor.
2122
* @param null|bool $colorize
23+
* @param null|bool|resource $stream
2224
* @throws InvalidStyleException
2325
*/
24-
public function __construct(?bool $colorize = null)
26+
public function __construct(?bool $colorize = null, $stream = null)
2527
{
2628
$this->color = new ConsoleColor();
29+
$this->color->setStream($stream);
2730
$this->doColorize = $this->refineColorize($colorize);
2831
$this->setThemes();
2932
}

0 commit comments

Comments
 (0)