Skip to content

Commit 466f114

Browse files
committed
init cli-utils lib. add some classes and examples
1 parent 0f193df commit 466f114

12 files changed

+688
-61
lines changed

.gitignore

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.idea/
2+
.phpintel/
3+
!README.md
4+
!.gitkeep
5+
composer.lock
6+
*.swp
7+
*.log
8+
*.pid
9+
*.patch
10+
.DS_Store

LICENSE

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 inhere
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
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, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

composer.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "toolkit/cli-utils",
3+
"type": "library",
4+
"description": "some cli tool library of the php",
5+
"keywords": ["library","tool","php"],
6+
"homepage": "https://github.com/php-toolkit/cli-utils",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "inhere",
11+
"email": "in.798@qq.com",
12+
"homepage": "http://www.yzone.net/"
13+
}
14+
],
15+
"require": {
16+
"php": ">7.0.0"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"Toolkit\\Cli\\" : "src/"
21+
}
22+
},
23+
"suggest": {
24+
"inhere/simple-print-tool": "Very lightweight data printing tools",
25+
"inhere/php-validate": "Very lightweight data validate tool",
26+
"inhere/console": "a lightweight php console application library."
27+
}
28+
}

example/all-color-style.jpg

154 KB
Loading

example/color.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2018/5/4
6+
* Time: 下午3:03
7+
*/
8+
9+
use Toolkit\Cli\Color;
10+
11+
require dirname(__DIR__) . '/test/boot.php';
12+
13+
foreach (Color::getStyles() as $style) {
14+
printf(" %s: %s\n", $style, Color::apply($style, 'A message'));
15+
}

example/high.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2018/5/4
6+
* Time: 下午3:13
7+
*/
8+
9+
use Toolkit\Cli\Highlighter;
10+
11+
require dirname(__DIR__) . '/test/boot.php';
12+
13+
echo "Highlight current file content:\n";
14+
15+
// this is an comment
16+
$rendered = Highlighter::create()->highlight(file_get_contents(__FILE__));
17+
18+
\Toolkit\Cli\Cli::write($rendered);

phpunit.xml.dist

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit backupGlobals="false"
4+
backupStaticAttributes="false"
5+
bootstrap="test/boot.php"
6+
colors="false"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
>
13+
<testsuites>
14+
<testsuite name="Php Library Test Suite">
15+
<directory>test/</directory>
16+
</testsuite>
17+
</testsuites>
18+
19+
<filter>
20+
<whitelist>
21+
<directory suffix=".php">src</directory>
22+
</whitelist>
23+
</filter>
24+
</phpunit>

src/Cli.php

+55-34
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,14 @@
66
* Time: 下午5:33
77
*/
88

9-
namespace Toolkit\Sys;
9+
namespace Toolkit\Cli;
1010

1111
/**
1212
* Class Cli
13-
* @package Toolkit\Sys
13+
* @package Toolkit\Cli
1414
*/
1515
class Cli
1616
{
17-
/*******************************************************************************
18-
* color render
19-
******************************************************************************/
20-
21-
/**
22-
* @param $text
23-
* @param string|int|array $style
24-
* @return string
25-
*/
26-
public static function color(string $text, $style = null): string
27-
{
28-
return Color::render($text, $style);
29-
}
30-
31-
/**
32-
* @param string $text
33-
* @return string
34-
*/
35-
public static function clearColor($text): string
36-
{
37-
// return preg_replace('/\033\[(?:\d;?)+m/', '' , "\033[0;36mtext\033[0m");
38-
return preg_replace('/\033\[(?:\d;?)+m/', '', strip_tags($text));
39-
}
40-
4117
/*******************************************************************************
4218
* read/write message
4319
******************************************************************************/
@@ -58,17 +34,17 @@ public static function read($message = null, $nl = false): string
5834

5935
/**
6036
* write message to console
61-
* @param $message
37+
* @param $messages
6238
* @param bool $nl
6339
* @param bool $quit
6440
*/
65-
public static function write($message, $nl = true, $quit = false)
41+
public static function write($messages, $nl = true, $quit = false)
6642
{
67-
if (\is_array($message)) {
68-
$message = implode($nl ? PHP_EOL : '', $message);
43+
if (\is_array($messages)) {
44+
$messages = implode($nl ? PHP_EOL : '', $messages);
6945
}
7046

71-
self::stdout(Color::parseTag($message), $nl, $quit);
47+
self::stdout(Color::parseTag($messages), $nl, $quit);
7248
}
7349

7450
/**
@@ -77,7 +53,7 @@ public static function write($message, $nl = true, $quit = false)
7753
* @param bool $nl
7854
* @param bool|int $quit
7955
*/
80-
public static function stdout($message, $nl = true, $quit = false)
56+
public static function stdout(string $message, $nl = true, $quit = false)
8157
{
8258
fwrite(\STDOUT, $message . ($nl ? PHP_EOL : ''));
8359
fflush(\STDOUT);
@@ -94,7 +70,7 @@ public static function stdout($message, $nl = true, $quit = false)
9470
* @param bool $nl
9571
* @param bool|int $quit
9672
*/
97-
public static function stderr($message, $nl = true, $quit = -200)
73+
public static function stderr(string $message, $nl = true, $quit = -1)
9874
{
9975
fwrite(\STDERR, self::color('[ERROR] ', 'red') . $message . ($nl ? PHP_EOL : ''));
10076
fflush(\STDOUT);
@@ -105,6 +81,24 @@ public static function stderr($message, $nl = true, $quit = -200)
10581
}
10682
}
10783

84+
/*******************************************************************************
85+
* color render
86+
******************************************************************************/
87+
88+
/**
89+
* @param $text
90+
* @param string|int|array $style
91+
* @return string
92+
*/
93+
public static function color(string $text, $style = null): string
94+
{
95+
return Color::render($text, $style);
96+
}
97+
98+
/*******************************************************************************
99+
* some helpers
100+
******************************************************************************/
101+
108102
/**
109103
* Returns true if STDOUT supports colorization.
110104
* This code has been copied and adapted from
@@ -113,9 +107,36 @@ public static function stderr($message, $nl = true, $quit = -200)
113107
*/
114108
public static function isSupportColor(): bool
115109
{
116-
return SysEnv::isSupportColor();
110+
if (DIRECTORY_SEPARATOR === '\\') {
111+
return
112+
'10.0.10586' === PHP_WINDOWS_VERSION_MAJOR . '.' . PHP_WINDOWS_VERSION_MINOR . '.' . PHP_WINDOWS_VERSION_BUILD
113+
|| false !== getenv('ANSICON')
114+
|| 'ON' === getenv('ConEmuANSI')
115+
|| 'xterm' === getenv('TERM')// || 'cygwin' === getenv('TERM')
116+
;
117+
}
118+
119+
if (!\defined('STDOUT')) {
120+
return false;
121+
}
122+
123+
return self::isInteractive(STDOUT);
124+
}
125+
126+
/**
127+
* Returns if the file descriptor is an interactive terminal or not.
128+
* @param int|resource $fileDescriptor
129+
* @return boolean
130+
*/
131+
public static function isInteractive($fileDescriptor): bool
132+
{
133+
return \function_exists('posix_isatty') && @posix_isatty($fileDescriptor);
117134
}
118135

136+
/*******************************************************************************
137+
* parse $argv
138+
******************************************************************************/
139+
119140
/**
120141
* Parses $GLOBALS['argv'] for parameters and assigns them to an array.
121142
* Supports:

0 commit comments

Comments
 (0)