Skip to content

Commit 82dfc43

Browse files
committed
initial v1
1 parent 39b50b9 commit 82dfc43

7 files changed

Lines changed: 365 additions & 1 deletion

File tree

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
desktop.ini
2+
todo.txt
3+
test.php
4+
composer.phar
5+
composer.lock
6+
vendor/

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Changelog
2+
3+
# v1.0.1
4+
5+
* detect file method errors
6+
* change file method type (void -> bool)
7+
8+
# v1.0
9+
10+
release fast project.
11+
12+
* initial release

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Mehdi HosseinZade
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,40 @@
11
# ColorConsole
2-
php colorconsole
2+
3+
ColorConsole customized https://github.com/kevinlebrun/colors.php/ for Olive: `color.php` license: (The MIT License) Copyright (c) 2018 Kevin Le Brun lebrun.k@gmail.com
4+
5+
## Installation
6+
7+
Install the latest version with
8+
9+
```
10+
$ composer require olive-cms/colorconsole
11+
```
12+
13+
## Basic Usage
14+
15+
``` php
16+
require_once 'vendor/autoload.php';
17+
use Olive\Tools\ColorConsole;
18+
19+
ColorConsole::render(
20+
'Your Message',
21+
[
22+
'color' => 'magenta',
23+
'background' => 'white',
24+
'style' => ['bold', 'reverse'],
25+
'align' => 'center'
26+
]
27+
);
28+
```
29+
30+
## Documentation
31+
32+
- [Usage Instructions](doc/01-usage.md)
33+
34+
## Requirements
35+
36+
- PHP 5.5+.
37+
38+
## License
39+
40+
olive-cms/colorconsole is licensed under the [MIT license](http://opensource.org/licenses/MIT).

composer.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "olive-cms/colorconsole",
3+
"description": "php colorconsole",
4+
"type": "library",
5+
"keywords": ["tools"],
6+
"require": {
7+
"php": ">=5.5"
8+
},
9+
"autoload": {
10+
"psr-4": {
11+
"Olive\\Tools\\": "src/"
12+
}
13+
},
14+
"license": "MIT",
15+
"authors": [{
16+
"name": "Mehdi HosseinZade",
17+
"email": "arshen@outlook.com",
18+
"homepage": "https://arshen.ir",
19+
"role": "Developer"
20+
}]
21+
}

doc/01-usage.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Using ColorConsole
2+
3+
- [Installation](#installation)
4+
1. [Basic Usage](#basic-usage)
5+
- [Items](#items)
6+
1. [Style](#style)
7+
2. [Color](#color)
8+
3. [Background](#background)
9+
3. [Align](#align)
10+
11+
## Installation
12+
13+
olive-cms/colorconsole is available on Packagist ([olive-cms/colorconsole](http://packagist.org/packages/olive-cms/colorconsole)) and as such installable via [Composer](http://getcomposer.org/).
14+
15+
```bash
16+
composer require olive-cms/colorconsole
17+
```
18+
19+
If you do not use Composer, you can grab the code from GitHub, and use any PSR-0 compatible autoloader (e.g. the [Symfony2 ClassLoader component](https://github.com/symfony/ClassLoader)) to load Monolog classes.
20+
21+
22+
### Basic Usage
23+
24+
``` php
25+
require_once 'vendor/autoload.php';
26+
use Olive\Tools\ColorConsole;
27+
28+
ColorConsole::render(
29+
'Your Message',
30+
[
31+
'color' => 'magenta',
32+
'background' => 'white',
33+
'style' => ['bold', 'reverse'],
34+
'align' => 'center'
35+
]
36+
);
37+
```
38+
39+
## Items
40+
41+
### Style
42+
43+
Notice: italic and blink may not work depending of your terminal
44+
45+
available styles:
46+
47+
* reset
48+
* bold
49+
* dark
50+
* italic
51+
* underline
52+
* blink
53+
* reverse
54+
* concealed
55+
56+
### Color
57+
58+
available text Colors:
59+
60+
* default
61+
* black
62+
* red
63+
* green
64+
* yellow
65+
* blue
66+
* magenta
67+
* cyan
68+
* light_gray
69+
* dark_gray
70+
* light_red
71+
* light_green
72+
* light_yellow
73+
* light_blue
74+
* light_magenta
75+
* light_cyan
76+
* white
77+
78+
### Background
79+
80+
available text backgrounds:
81+
82+
* default
83+
* black
84+
* red
85+
* green
86+
* yellow
87+
* blue
88+
* magenta
89+
* cyan
90+
* light_gray
91+
* dark_gray
92+
* light_red
93+
* light_green
94+
* light_yellow
95+
* light_blue
96+
* light_magenta
97+
* light_cyan
98+
* white
99+
100+
### Align
101+
102+
available Align:
103+
104+
* center

src/ColorConsole.php

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
<?php
2+
namespace Olive\Tools;
3+
4+
/**
5+
* customize https://github.com/kevinlebrun/colors.php/ for Olive
6+
* color.php license: (The MIT License) Copyright (c) 2018 Kevin Le Brun lebrun.k@gmail.com
7+
*/
8+
class ColorConsole
9+
{
10+
// italic and blink may not work depending of your terminal
11+
private const styles = [
12+
'reset' => '0',
13+
'bold' => '1',
14+
'dark' => '2',
15+
'italic' => '3',
16+
'underline' => '4',
17+
'blink' => '5',
18+
'reverse' => '7',
19+
'concealed' => '8',
20+
'default' => '39',
21+
'black' => '30',
22+
'red' => '31',
23+
'green' => '32',
24+
'yellow' => '33',
25+
'blue' => '34',
26+
'magenta' => '35',
27+
'cyan' => '36',
28+
'light_gray' => '37',
29+
'dark_gray' => '90',
30+
'light_red' => '91',
31+
'light_green' => '92',
32+
'light_yellow' => '93',
33+
'light_blue' => '94',
34+
'light_magenta' => '95',
35+
'light_cyan' => '96',
36+
'white' => '97',
37+
'bg_default' => '49',
38+
'bg_black' => '40',
39+
'bg_red' => '41',
40+
'bg_green' => '42',
41+
'bg_yellow' => '43',
42+
'bg_blue' => '44',
43+
'bg_magenta' => '45',
44+
'bg_cyan' => '46',
45+
'bg_light_gray' => '47',
46+
'bg_dark_gray' => '100',
47+
'bg_light_red' => '101',
48+
'bg_light_green' => '102',
49+
'bg_light_yellow' => '103',
50+
'bg_light_blue' => '104',
51+
'bg_light_magenta' => '105',
52+
'bg_light_cyan' => '106',
53+
'bg_white' => '107',
54+
];
55+
56+
public static function render($string = '', $option = [])
57+
{
58+
if (isset($GLOBALS['OliveConsoleColor']) and $GLOBALS['OliveConsoleColor'] == false) {
59+
return $string;
60+
}
61+
$color = 'default';
62+
$background = 'default';
63+
$styles = [];
64+
if (isset($option['color'])) {
65+
$color = $option['color'];
66+
}
67+
if (isset($option['background'])) {
68+
$background = $option['background'];
69+
}
70+
if (isset($option['style'])) {
71+
$st = $option['style'];
72+
if (is_array($st)) {
73+
$styles = $st;
74+
} else {
75+
$styles = [$st];
76+
}
77+
}
78+
if (isset($option['align']) and $option['align'] == 'center') {
79+
$string = self::center($string);
80+
}
81+
$message = self::stylize($color, $string);
82+
$message = self::stylize('bg_' . $background, $message);
83+
foreach ($styles as $style) {
84+
$message = self::stylize($style, $message);
85+
}
86+
87+
return $message;
88+
}
89+
90+
/**
91+
* Returns true if the stream supports colorization.
92+
*
93+
* Colorization is disabled if not supported by the stream:
94+
*
95+
* - Windows without Ansicon, ConEmu or Babun
96+
* - non tty consoles
97+
*
98+
* @return bool true if the stream supports colorization, false otherwise
99+
*
100+
* @link https://github.com/symfony/Console/blob/master/Output/StreamOutput.php#L94
101+
* @codeCoverageIgnore
102+
*/
103+
private static function shouldStylize()
104+
{
105+
if (DIRECTORY_SEPARATOR === '\\') {
106+
return (function_exists('sapi_windows_vt100_support')
107+
&& @sapi_windows_vt100_support(STDOUT))
108+
|| false !== getenv('ANSICON')
109+
|| 'ON' === getenv('ConEmuANSI')
110+
|| 'xterm' === getenv('TERM');
111+
}
112+
113+
if (function_exists('stream_isatty')) {
114+
return @stream_isatty(STDOUT);
115+
}
116+
117+
if (function_exists('posix_isatty')) {
118+
return @posix_isatty(STDOUT);
119+
}
120+
121+
$stat = @fstat(self::stream);
122+
// Check if formatted mode is S_IFCHR
123+
return $stat ? 0020000 === ($stat['mode'] & 0170000) : false;
124+
}
125+
126+
private static function stylize($style, $text)
127+
{
128+
if (! self::shouldStylize()) {
129+
return $text;
130+
}
131+
132+
$style = strtolower($style);
133+
134+
if (array_key_exists($style, self::styles)) {
135+
return self::buildEscSeq(self::styles[$style]) . $text . self::buildEscSeq(self::styles['reset']);
136+
}
137+
138+
if (preg_match('/^((?:bg_)?)color\[([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\]$/', $style, $matches)) {
139+
$option = $matches[1] == 'bg_' ? 48 : 38;
140+
141+
return self::buildEscSeq("{$option};5;{$matches[2]}") . $text . self::buildEscSeq(self::styles['reset']);
142+
}
143+
144+
throw new \Exception("Invalid style $style");
145+
}
146+
147+
private static function buildEscSeq($style)
148+
{
149+
return sprintf("\033[%sm", $style);
150+
}
151+
152+
private static function center($text, $width = 80)
153+
{
154+
foreach (explode(PHP_EOL, $text) as $line) {
155+
$line = trim($line);
156+
$lineWidth = strlen($line) - mb_strlen($line, 'UTF-8') + $width;
157+
$centered .= str_pad($line, $lineWidth, ' ', STR_PAD_BOTH) . PHP_EOL;
158+
}
159+
160+
return trim($centered, PHP_EOL);
161+
}
162+
}

0 commit comments

Comments
 (0)