Skip to content

Commit 80c98c5

Browse files
committed
add console classes
1 parent 662d317 commit 80c98c5

File tree

4 files changed

+561
-1
lines changed

4 files changed

+561
-1
lines changed

src/Ubiquity/devtools/cmd/Command.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class Command {
2020

2121
protected static $customCommands;
2222

23+
protected static $customAliases;
24+
2325
public function __construct($name, $value, $description, $aliases = [], $parameters = [], $examples = []) {
2426
$this->name = $name;
2527
$this->value = $value;
@@ -516,12 +518,23 @@ public static function getCustomCommands() {
516518
$classes = UIntrospection::getChildClasses('\\Ubiquity\\devtools\\cmd\\commands\\AbstractCustomCommand');
517519
foreach ($classes as $class) {
518520
$o = new $class();
519-
self::$customCommands[$o->getCommand()->getName()] = $o;
521+
$cmd = $o->getCommand();
522+
self::$customCommands[$cmd->getName()] = $o;
523+
$aliases = $cmd->getAliases();
524+
if (is_array($aliases)) {
525+
foreach ($aliases as $alias) {
526+
self::$customAliases[$alias] = $o;
527+
}
528+
}
520529
}
521530
}
522531
return self::$customCommands;
523532
}
524533

534+
public static function getCustomAliases() {
535+
return self::$customAliases;
536+
}
537+
525538
public static function preloadCustomCommands() {
526539
$files = UFileSystem::glob_recursive("*.cmd.php");
527540
foreach ($files as $file) {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
namespace Ubiquity\devtools\cmd;
3+
4+
class Console {
5+
public static function readline(){
6+
return rtrim(fgets(STDIN));
7+
}
8+
9+
public static function question($prompt,array $propositions=null){
10+
echo ConsoleFormatter::colorize($prompt,ConsoleFormatter::BLACK,ConsoleFormatter::BG_YELLOW);
11+
if(is_array($propositions)){
12+
if(sizeof($propositions)>2){
13+
$props="";
14+
foreach ($propositions as $index=>$prop){
15+
$props.="[".($index+1)."] ".$prop."\n";
16+
}
17+
echo ConsoleFormatter::formatContent($props);
18+
do{
19+
$answer=self::readline();
20+
}while((int)$answer!=$answer || !isset($propositions[(int)$answer-1]));
21+
$answer=$propositions[(int)$answer-1];
22+
}else {
23+
echo " (".implode("/", $propositions).")\n";
24+
do{
25+
$answer=self::readline();
26+
}while(array_search($answer, $propositions)===false);
27+
}
28+
}else{
29+
$answer=self::readline();
30+
}
31+
32+
return $answer;
33+
}
34+
35+
public static function isYes($answer){
36+
return array_search($answer, ["yes","y"])!==false;
37+
}
38+
39+
public static function isNo($answer){
40+
return array_search($answer, ["no","n"])!==false;
41+
}
42+
43+
public static function isCancel($answer){
44+
return array_search($answer, ["cancel","z"])!==false;
45+
}
46+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
namespace Ubiquity\devtools\cmd;
3+
4+
class ConsoleFormatter {
5+
const BLACK='0;30',DARK_GREY='1;30',BLUE='0;34',LIGHT_BLUE='1;34',GREEN='0;32',LIGHT_GREEN='1;32',CYAN='0;36',LIGHT_CYAN='1;36',RED='0;31',LIGHT_RED='1;31',PURPLE='0;35',LIGHT_PURPLE='1;35',BROWN='0;33',YELLOW='1;33',LIGHT_GRAY='0;37',WHITE='1;37';
6+
const BG_BLACK='40',BG_RED='41',BG_GREEN='42',BG_YELLOW='43',BG_BLUE='44',BG_MAGENTA='45',BG_CYAN='46',BG_LIGHT_GRAY='47';
7+
const BOLD='1',END_BOLD='22',CLEAR='0';
8+
9+
/**
10+
* Returns a colored string
11+
* @param string $string
12+
* @param string $color
13+
* @param string $bgColor
14+
* @return string
15+
*/
16+
public static function colorize($string, $color = null, $bgColor = null) {
17+
if(!self::isSupported()){
18+
return $string;
19+
}
20+
$coloredString = "";
21+
if (isset($color)) {
22+
$coloredString .= self::escape($color);
23+
}
24+
if (isset($bgColor)) {
25+
$coloredString .= self::escape($bgColor);
26+
}
27+
$coloredString .= $string .self::escape(self::CLEAR);
28+
return $coloredString;
29+
}
30+
31+
private static function prefixLines($str,$prefix){
32+
$lines=explode("\n", $str);
33+
array_walk($lines, function(&$line) use($prefix){if(trim($line)!=null) $line=$prefix.$line;});
34+
return implode("\n", $lines);
35+
}
36+
37+
private static function escape($value){
38+
return "\033[{$value}m";
39+
}
40+
41+
public static function showInfo($content,$dColor=self::CYAN){
42+
return self::colorize(self::formatContent($content),$dColor);
43+
}
44+
45+
/**
46+
* @return boolean
47+
*/
48+
public static function isSupported()
49+
{
50+
if (DIRECTORY_SEPARATOR === '\\') {
51+
if (\function_exists('sapi_windows_vt100_support') && @sapi_windows_vt100_support(STDOUT)) {
52+
return true;
53+
} elseif ('10.0.10586' === PHP_WINDOWS_VERSION_MAJOR.'.'.PHP_WINDOWS_VERSION_MINOR.'.'.PHP_WINDOWS_VERSION_BUILD
54+
|| false !== \getenv('ANSICON')
55+
|| 'ON' === \getenv('ConEmuANSI')
56+
|| 'xterm' === \getenv('TERM')) {
57+
return true;
58+
}
59+
return false;
60+
} else {
61+
return function_exists('posix_isatty') && @posix_isatty(STDOUT);
62+
}
63+
}
64+
65+
public static function formatContent($content,$prefix=" · "){
66+
$content = str_replace ( "<br>", "\n", $content );
67+
$content=self::formatHtml($content);
68+
$content= strip_tags ( $content );
69+
return "\n".self::prefixLines($content,$prefix)."\n";
70+
}
71+
72+
public static function showMessage($content, $type='info',$title=null) {
73+
$header="".$type;
74+
if(isset($title)){
75+
$header.=' : '.$title;
76+
}
77+
$result=self::formatContent($content);
78+
switch ($type){
79+
case 'error':
80+
$header=self::colorize($header,self::LIGHT_RED);
81+
break;
82+
case 'success':
83+
$header=self::colorize($header,self::GREEN);
84+
break;
85+
case 'info':
86+
$header=self::colorize($header,self::CYAN);
87+
break;
88+
case 'warning':
89+
$header=self::colorize($header,self::LIGHT_GRAY);
90+
break;
91+
}
92+
$result=rtrim($result,"\n");
93+
return ConsoleTable::borderType([[$header.$result]], $type);
94+
}
95+
96+
public static function formatHtml($str){
97+
$reg='@<(b)>(.+?)</\1>@i';
98+
if(!self::isSupported()){
99+
return preg_replace($reg, '$2', $str);
100+
}
101+
return preg_replace($reg, self::escape(self::BOLD).'$2'.self::escape(self::END_BOLD), $str);
102+
}
103+
}
104+

0 commit comments

Comments
 (0)