Skip to content

Commit 334a9c6

Browse files
committed
update some
1 parent 6ce4276 commit 334a9c6

File tree

4 files changed

+122
-98
lines changed

4 files changed

+122
-98
lines changed

src/Component/Style/Style.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace Inhere\Console\Component\Style;
1212

1313
use Toolkit\Cli\Cli;
14+
use Toolkit\Cli\ColorTag;
1415

1516
/**
1617
* Class Style
@@ -49,11 +50,6 @@ class Style
4950
*/
5051
public const COLOR_TAG = '/<([a-zA-Z=;]+)>(.*?)<\/\\1>/s';
5152

52-
/**
53-
* Regex used for removing color codes
54-
*/
55-
public const STRIP_TAG = '/<[\/]?[a-zA-Z=;]+>/';
56-
5753
/**
5854
* @var self
5955
*/
@@ -147,20 +143,30 @@ protected function loadDefaultStyles(): void
147143
/**
148144
* Process a string use style
149145
* @param string $style
150-
* @param $text
146+
* @param string $text
151147
* @return string
152148
*/
153-
public function apply(string $style, $text): string
149+
public function apply(string $style, string $text): string
154150
{
155151
return $this->format(self::wrap($text, $style));
156152
}
157153

158154
/**
159155
* Process a string.
160-
* @param $text
156+
* @param string $text
157+
* @return mixed
158+
*/
159+
public function t(string $text)
160+
{
161+
return $this->format($text);
162+
}
163+
164+
/**
165+
* Process a string.
166+
* @param string $text
161167
* @return mixed
162168
*/
163-
public function render($text)
169+
public function render(string $text)
164170
{
165171
return $this->format($text);
166172
}
@@ -177,10 +183,10 @@ public function format(string $text)
177183

178184
// if don't support output color text, clear color tag.
179185
if (!Cli::isSupportColor() || self::isNoColor()) {
180-
return static::stripColor($text);
186+
return self::stripColor($text);
181187
}
182188

183-
if (!\preg_match_all(self::COLOR_TAG, $text, $matches)) {
189+
if (!$matches = ColorTag::matchAll($text)) {
184190
return $text;
185191
}
186192

@@ -222,8 +228,7 @@ protected function replaceColor($text, $tag, $match, $style): string
222228
*/
223229
public static function stripColor(string $string)
224230
{
225-
// $text = strip_tags($text);
226-
return \preg_replace(self::STRIP_TAG, '', $string);
231+
return ColorTag::strip($string);
227232
}
228233

229234
/****************************************************************************

src/IO/Output.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function read($question = null, $nl = false): string
108108
$this->write($question, $nl);
109109
}
110110

111-
return trim(fgets(\STDIN));
111+
return \trim(\fgets(\STDIN));
112112
}
113113

114114
/**
@@ -121,7 +121,7 @@ public function stderr($text = '', $nl = true): self
121121
{
122122
$text = $this->getStyle()->format($text);
123123

124-
fwrite($this->errorStream, $text . ($nl ? "\n" : null));
124+
\fwrite($this->errorStream, $text . ($nl ? "\n" : null));
125125

126126
return $this;
127127
}

src/Traits/FormatOutputAwareTrait.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ public function writeRaw($text, $nl = true, $quit = false, array $opts = []): in
7878
return Show::writeRaw($text, $nl, $quit, $opts);
7979
}
8080

81+
/**
82+
* @param string $text
83+
* @param string $tag
84+
* @return int
85+
*/
86+
public function colored(string $text, string $tag = 'info'): int
87+
{
88+
return $this->writeln(\sprintf('<%s>%s</%s>', $tag, $text, $tag));
89+
}
90+
8191
/**
8292
* @inheritdoc
8393
* @see Show::block()

0 commit comments

Comments
 (0)