Skip to content

Commit bd4e2f3

Browse files
committed
some update for style.
1 parent ccee999 commit bd4e2f3

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,18 @@ $output->write('hello <info>world<info>');
374374

375375
> output 实例拥有 `Inhere\Console\Utils\Show` 的所有格式化输出方法。不过都是通过对象式访问的。
376376
377+
- **单独使用颜色风格**
378+
379+
```php
380+
$style = Inhere\Console\Style\Style::create();
381+
382+
echo $style->render('no color <info>color text</info>');
383+
384+
// 直接使用内置的风格
385+
echo $style->info('message');
386+
echo $style->error('message');
387+
```
388+
377389
### 标题文本输出
378390

379391
使用 `Show::title()/$output->title()`

src/Style/Style.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
* Class Style
1717
* @package Inhere\Console\Style
1818
* @link https://github.com/ventoviro/windwalker-IO
19+
*
20+
* @method string info(string $message)
21+
* @method string comment(string $message)
22+
* @method string success(string $message)
23+
* @method string warning(string $message)
24+
* @method string danger(string $message)
25+
* @method string error(string $message)
1926
*/
2027
class Style
2128
{
@@ -95,6 +102,21 @@ public function __construct($fg = '', $bg = '', array $options = [])
95102
$this->loadDefaultStyles();
96103
}
97104

105+
/**
106+
* @param string $method
107+
* @param array $args
108+
* @return mixed|string
109+
* @throws \InvalidArgumentException
110+
*/
111+
public function __call($method, array $args)
112+
{
113+
if (isset($args[0]) && $this->hasStyle($method)) {
114+
return $this->format(sprintf('<%s>%s</%s>', $method, $args[0], $method));
115+
}
116+
117+
throw new \InvalidArgumentException("You called method is not exists: $method");
118+
}
119+
98120
/**
99121
* Adds predefined color styles to the Color styles
100122
* default primary success info warning danger
@@ -193,7 +215,9 @@ public static function stripColor($string)
193215
return preg_replace(self::STRIP_TAG, '', $string);
194216
}
195217

196-
///////////////////////////////////////// Attr Color Style /////////////////////////////////////////
218+
/****************************************************************************
219+
* Attr Color Style
220+
****************************************************************************/
197221

198222
/**
199223
* Add a style.

0 commit comments

Comments
 (0)