Skip to content

Commit 649c9cc

Browse files
committed
update readme
1 parent dd4e1c1 commit 649c9cc

File tree

3 files changed

+222
-3
lines changed

3 files changed

+222
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ a php console application library.
2323

2424
- by composer
2525

26-
编辑 `composer.json` `require` 添加
26+
edit `composer.json`at `require` add
2727

2828
```
2929
"inhere/console": "dev-master",
3030
// "inhere/console": "dev-php5", // for php5
3131
```
3232

33-
然后执行: `composer update`
33+
run: `composer update`
3434

3535
- Direct fetch
3636

README_zh.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
- 颜色风格输出支持
99
- 特殊格式信息显示
1010
- 用户信息交互支持
11-
- 其他功能
11+
- 命令方法注释自动提取为参数 `arguments` 和 选项 `options`
12+
- 类似 symfony/console 的预定义参数定义支持
1213

1314
## 项目地址
1415

@@ -625,3 +626,21 @@ $answer = Interact::limitedAsk('please input you age?', null, function($age)
625626
## License
626627

627628
MIT
629+
630+
## 我的其他项目
631+
632+
### `inhere/redis` [github](https://github.com/inhere/php-redis) [git@osc](https://git.oschina.net/inhere/php-redis)
633+
634+
简单的redis操作客户端包装库
635+
636+
### `inhere/sroute` [github](https://github.com/inhere/php-srouter) [git@osc](https://git.oschina.net/inhere/php-srouter)
637+
638+
轻量且功能丰富快速的路由库
639+
640+
### `inhere/php-validate` [github](https://github.com/inhere/php-validate) [git@osc](https://git.oschina.net/inhere/php-validate)
641+
642+
一个简洁小巧且功能完善的php验证库。仅有几个文件,无依赖。
643+
644+
### `inhere/http` [github](https://github.com/inhere/php-http) [git@osc](https://git.oschina.net/inhere/php-http)
645+
646+
http 工具库(`request` 请求 `response` 响应 `curl` curl请求库,有简洁、完整和并发请求三个版本的类)

examples/baks/sf2_color.php

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
<?php
2+
/*
3+
* This file is part of the Symfony package.
4+
*
5+
* (c) Fabien Potencier <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
12+
/**
13+
* Formatter style class for defining styles.
14+
*
15+
* @author Konstantin Kudryashov <[email protected]>
16+
*/
17+
class OutputFormatterStyle
18+
{
19+
private static $availableForegroundColors = array(
20+
'black' => array('set' => 30, 'unset' => 39),
21+
'red' => array('set' => 31, 'unset' => 39),
22+
'green' => array('set' => 32, 'unset' => 39),
23+
'yellow' => array('set' => 33, 'unset' => 39),
24+
'blue' => array('set' => 34, 'unset' => 39),
25+
'magenta' => array('set' => 35, 'unset' => 39),
26+
'cyan' => array('set' => 36, 'unset' => 39),
27+
'white' => array('set' => 37, 'unset' => 39),
28+
'default' => array('set' => 39, 'unset' => 39),
29+
);
30+
private static $availableBackgroundColors = array(
31+
'black' => array('set' => 40, 'unset' => 49),
32+
'red' => array('set' => 41, 'unset' => 49),
33+
'green' => array('set' => 42, 'unset' => 49),
34+
'yellow' => array('set' => 43, 'unset' => 49),
35+
'blue' => array('set' => 44, 'unset' => 49),
36+
'magenta' => array('set' => 45, 'unset' => 49),
37+
'cyan' => array('set' => 46, 'unset' => 49),
38+
'white' => array('set' => 47, 'unset' => 49),
39+
'default' => array('set' => 49, 'unset' => 49),
40+
);
41+
private static $availableOptions = array(
42+
'bold' => array('set' => 1, 'unset' => 22),
43+
'underscore' => array('set' => 4, 'unset' => 24),
44+
'blink' => array('set' => 5, 'unset' => 25),
45+
'reverse' => array('set' => 7, 'unset' => 27),
46+
'conceal' => array('set' => 8, 'unset' => 28),
47+
);
48+
private $foreground;
49+
private $background;
50+
private $options = array();
51+
/**
52+
* Initializes output formatter style.
53+
*
54+
* @param string|null $foreground The style foreground color name
55+
* @param string|null $background The style background color name
56+
* @param array $options The style options
57+
*/
58+
public function __construct($foreground = null, $background = null, array $options = array())
59+
{
60+
if (null !== $foreground) {
61+
$this->setForeground($foreground);
62+
}
63+
if (null !== $background) {
64+
$this->setBackground($background);
65+
}
66+
if (count($options)) {
67+
$this->setOptions($options);
68+
}
69+
}
70+
/**
71+
* Sets style foreground color.
72+
*
73+
* @param string|null $color The color name
74+
*
75+
* @throws \InvalidArgumentException When the color name isn't defined
76+
*/
77+
public function setForeground($color = null)
78+
{
79+
if (null === $color) {
80+
$this->foreground = null;
81+
return;
82+
}
83+
if (!isset(static::$availableForegroundColors[$color])) {
84+
throw new \InvalidArgumentException(sprintf(
85+
'Invalid foreground color specified: "%s". Expected one of (%s)',
86+
$color,
87+
implode(', ', array_keys(static::$availableForegroundColors))
88+
));
89+
}
90+
$this->foreground = static::$availableForegroundColors[$color];
91+
}
92+
/**
93+
* Sets style background color.
94+
*
95+
* @param string|null $color The color name
96+
*
97+
* @throws \InvalidArgumentException When the color name isn't defined
98+
*/
99+
public function setBackground($color = null)
100+
{
101+
if (null === $color) {
102+
$this->background = null;
103+
return;
104+
}
105+
if (!isset(static::$availableBackgroundColors[$color])) {
106+
throw new \InvalidArgumentException(sprintf(
107+
'Invalid background color specified: "%s". Expected one of (%s)',
108+
$color,
109+
implode(', ', array_keys(static::$availableBackgroundColors))
110+
));
111+
}
112+
$this->background = static::$availableBackgroundColors[$color];
113+
}
114+
/**
115+
* Sets some specific style option.
116+
*
117+
* @param string $option The option name
118+
*
119+
* @throws \InvalidArgumentException When the option name isn't defined
120+
*/
121+
public function setOption($option)
122+
{
123+
if (!isset(static::$availableOptions[$option])) {
124+
throw new \InvalidArgumentException(sprintf(
125+
'Invalid option specified: "%s". Expected one of (%s)',
126+
$option,
127+
implode(', ', array_keys(static::$availableOptions))
128+
));
129+
}
130+
if (!in_array(static::$availableOptions[$option], $this->options)) {
131+
$this->options[] = static::$availableOptions[$option];
132+
}
133+
}
134+
/**
135+
* Unsets some specific style option.
136+
*
137+
* @param string $option The option name
138+
*
139+
* @throws \InvalidArgumentException When the option name isn't defined
140+
*/
141+
public function unsetOption($option)
142+
{
143+
if (!isset(static::$availableOptions[$option])) {
144+
throw new \InvalidArgumentException(sprintf(
145+
'Invalid option specified: "%s". Expected one of (%s)',
146+
$option,
147+
implode(', ', array_keys(static::$availableOptions))
148+
));
149+
}
150+
$pos = array_search(static::$availableOptions[$option], $this->options);
151+
if (false !== $pos) {
152+
unset($this->options[$pos]);
153+
}
154+
}
155+
/**
156+
* Sets multiple style options at once.
157+
*
158+
* @param array $options
159+
*/
160+
public function setOptions(array $options)
161+
{
162+
$this->options = array();
163+
foreach ($options as $option) {
164+
$this->setOption($option);
165+
}
166+
}
167+
/**
168+
* Applies the style to a given text.
169+
*
170+
* @param string $text The text to style
171+
*
172+
* @return string
173+
*/
174+
public function apply($text)
175+
{
176+
$setCodes = array();
177+
$unsetCodes = array();
178+
if (null !== $this->foreground) {
179+
$setCodes[] = $this->foreground['set'];
180+
$unsetCodes[] = $this->foreground['unset'];
181+
}
182+
if (null !== $this->background) {
183+
$setCodes[] = $this->background['set'];
184+
$unsetCodes[] = $this->background['unset'];
185+
}
186+
if (count($this->options)) {
187+
foreach ($this->options as $option) {
188+
$setCodes[] = $option['set'];
189+
$unsetCodes[] = $option['unset'];
190+
}
191+
}
192+
if (0 === count($setCodes)) {
193+
return $text;
194+
}
195+
return sprintf("\033[%sm%s\033[%sm", implode(';', $setCodes), $text, implode(';', $unsetCodes));
196+
}
197+
}
198+
199+
$clr = new OutputFormatterStyle('red');
200+
echo $clr->apply('message');

0 commit comments

Comments
 (0)