Skip to content

Commit 0ebda79

Browse files
committed
some update
1 parent 98b0717 commit 0ebda79

File tree

3 files changed

+174
-12
lines changed

3 files changed

+174
-12
lines changed

src/color/Lite.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017/5/10
6+
* Time: 下午7:45
7+
*/
8+
9+
namespace inhere\console\color;
10+
11+
/**
12+
* Class Lite
13+
* @package inhere\console\color
14+
*/
15+
class Lite
16+
{
17+
const NORMAL = 0;
18+
19+
// Foreground color
20+
const FG_BLACK = 30;
21+
const FG_RED = 31;
22+
const FG_GREEN = 32;
23+
const FG_BROWN = 33;
24+
const FG_BLUE = 34;
25+
const FG_CYAN = 36;
26+
const FG_WHITE = 37;
27+
const FG_DEFAULT = 39;
28+
29+
// Background color
30+
const BG_BLACK = 40;
31+
const BG_RED = 41;
32+
const BG_GREEN = 42;
33+
const BG_BROWN = 43;
34+
const BG_BLUE = 44;
35+
const BG_CYAN = 46;
36+
const BG_WHITE = 47;
37+
const BG_DEFAULT = 49;
38+
39+
// color option
40+
const BOLD = 1; // 加粗
41+
const FUZZY = 2; // 模糊(不是所有的终端仿真器都支持)
42+
const ITALIC = 3; // 斜体(不是所有的终端仿真器都支持)
43+
const UNDERSCORE = 4; // 下划线
44+
const BLINK = 5; // 闪烁
45+
const REVERSE = 7; // 颠倒的 交换背景色与前景色
46+
47+
/**
48+
* some styles
49+
* @var array
50+
*/
51+
public static $styles = [
52+
'light_red' => '1;31',
53+
'light_green' => '1;32',
54+
'yellow' => '1;33',
55+
'light_blue' => '1;34',
56+
'magenta' => '1;35',
57+
'light_cyan' => '1;36',
58+
'white' => '1;37',
59+
'normal' => '0',
60+
'black' => '0;30',
61+
'red' => '0;31',
62+
'green' => '0;32',
63+
'brown' => '0;33',
64+
'blue' => '0;34',
65+
'cyan' => '0;36',
66+
'bold' => '1',
67+
'underscore' => '4',
68+
'reverse' => '7',
69+
];
70+
71+
/**
72+
* @param $text
73+
* @param string|int|array $style
74+
* @param bool $return
75+
* @return string
76+
*/
77+
public static function render($text, $style = self::NORMAL, $return = true)
78+
{
79+
if(is_string($style)) {
80+
$out = isset(self::$styles[$style]) ? self::$styles[$style] : self::NORMAL;
81+
} elseif (is_int($style)) {
82+
$out = $style;
83+
84+
// array: [Lite::FG_GREEN, Lite::BG_WHITE, Lite::UNDERSCORE]
85+
} elseif (is_array($style)) {
86+
$out = implode(';', $style);
87+
} else {
88+
$out = self::NORMAL;
89+
}
90+
91+
// $result = chr(27). "$out{$text}" . chr(27) . chr(27) . "[0m". chr(27);
92+
$result = "\033[{$out}m{$text}\033[0m";
93+
94+
if($return ){
95+
return $result;
96+
}
97+
98+
echo $result;
99+
100+
return true;
101+
}
102+
103+
}

src/color/Style.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@
1313
*/
1414
final class Style
1515
{
16+
/**
17+
* Foreground base value
18+
* @var int
19+
*/
20+
const FG_BASE = 30;
21+
22+
/**
23+
* Background base value
24+
* @var int
25+
*/
26+
const BG_BASE = 40;
1627

1728
//////////////////////////////////////////// Color Style ////////////////////////////////////////////
1829

@@ -44,16 +55,6 @@ final class Style
4455
'reverse' => 7, // 颠倒的 交换背景色与前景色
4556
];
4657

47-
/**
48-
* Foreground base value
49-
*/
50-
private static $fgBase = 30;
51-
52-
/**
53-
* Background base value
54-
*/
55-
private static $bgBase = 40;
56-
5758
/**
5859
* Foreground color
5960
*/
@@ -131,7 +132,7 @@ public function __construct($fg = '', $bg = '', array $options = [])
131132
);
132133
}
133134

134-
$this->fgColor = static::$fgBase + static::$knownColors[$fg];
135+
$this->fgColor = self::FG_BASE + static::$knownColors[$fg];
135136
}
136137

137138
if ($bg) {
@@ -144,7 +145,7 @@ public function __construct($fg = '', $bg = '', array $options = [])
144145
);
145146
}
146147

147-
$this->bgColor = static::$bgBase + static::$knownColors[$bg];
148+
$this->bgColor = self::BG_BASE + static::$knownColors[$bg];
148149
}
149150

150151
foreach ($options as $option) {

src/color/cli-color.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# cli color
2+
3+
```
4+
5+
\033[0m 关闭所有属性
6+
\033[1m 设置高亮度
7+
\033[4m 下划线
8+
\033[5m 闪烁
9+
\033[7m 反显
10+
\033[8m 消隐
11+
\033[30m 至 \033[37m 设置前景色
12+
\033[40m 至 \033[47m 设置背景色
13+
\033[nA 光标上移n行
14+
\033[nB 光标下移n行
15+
\033[nC 光标右移n行
16+
\033[nD 光标左移n行
17+
\033[y;xH设置光标位置
18+
\033[2J 清屏
19+
\033[K 清除从光标到行尾的内容
20+
\033[s 保存光标位置
21+
\033[u 恢复光标位置
22+
\033[?25l 隐藏光标
23+
\033[?25h 显示光标
24+
```
25+
26+
### 各数字所代表的颜色如下:
27+
28+
- 背景颜色范围:40----49
29+
30+
```
31+
40:黑
32+
41:深红
33+
42:绿
34+
43:黄色
35+
44:蓝色
36+
45:紫色
37+
46:深绿
38+
47:白色
39+
```
40+
41+
- 字颜色:30-----------39
42+
43+
```
44+
30:黑
45+
31:红
46+
32:绿
47+
33:黄
48+
34:蓝色
49+
35:紫色
50+
36:深绿
51+
37:白色
52+
```
53+
54+
另外,同类的多种设置项可以组合在一起,中间用分号(`;`)隔开。如下:
55+
56+
```
57+
echo -e "\033[20;1H\033[1;4;32mHello,world\033[0m"
58+
```

0 commit comments

Comments
 (0)