11<?php
2- namespace Ubiquity \devtools \cmd ;
2+ namespace Ubiquity \devtools \cmd ;
33
4+ /**
5+ * Ubiquity\devtools\cmd$ConsoleFormatter
6+ * This class is part of Ubiquity
7+ *
8+ * @author jc
9+ * @version 1.0.0
10+ *
11+ */
412class 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 ' ;
13+
14+ 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 ' ;
15+
16+ 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 ' ;
17+
18+ const BOLD = '1 ' , END_BOLD = '22 ' , CLEAR = '0 ' ;
819
920 /**
1021 * Returns a colored string
22+ *
1123 * @param string $string
1224 * @param string $color
1325 * @param string $bgColor
1426 * @return string
1527 */
1628 public static function colorize ($ string , $ color = null , $ bgColor = null ) {
17- if (! self ::isSupported ()){
29+ if (! self ::isSupported ()) {
1830 return $ string ;
1931 }
2032 $ coloredString = "" ;
@@ -24,36 +36,36 @@ public static function colorize($string, $color = null, $bgColor = null) {
2436 if (isset ($ bgColor )) {
2537 $ coloredString .= self ::escape ($ bgColor );
2638 }
27- $ coloredString .= $ string .self ::escape (self ::CLEAR );
39+ $ coloredString .= $ string . self ::escape (self ::CLEAR );
2840 return $ coloredString ;
2941 }
3042
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 ;});
43+ private static function prefixLines ($ str , $ prefix ) {
44+ $ lines = explode ("\n" , $ str );
45+ array_walk ($ lines , function (&$ line ) use ($ prefix ) {
46+ if (trim ($ line ) != null )
47+ $ line = $ prefix . $ line ;
48+ });
3449 return implode ("\n" , $ lines );
3550 }
3651
37- private static function escape ($ value ){
52+ private static function escape ($ value ) {
3853 return "\033[ {$ value }m " ;
3954 }
4055
41- public static function showInfo ($ content ,$ dColor= self ::CYAN ){
42- return self ::colorize (self ::formatContent ($ content ),$ dColor );
56+ public static function showInfo ($ content , $ dColor = self ::CYAN ) {
57+ return self ::colorize (self ::formatContent ($ content ), $ dColor );
4358 }
4459
4560 /**
61+ *
4662 * @return boolean
4763 */
48- public static function isSupported ()
49- {
64+ public static function isSupported () {
5065 if (DIRECTORY_SEPARATOR === '\\' ) {
5166 if (\function_exists ('sapi_windows_vt100_support ' ) && @sapi_windows_vt100_support (STDOUT )) {
5267 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 ' )) {
68+ } elseif ('10.0.10586 ' === PHP_WINDOWS_VERSION_MAJOR . '. ' . PHP_WINDOWS_VERSION_MINOR . '. ' . PHP_WINDOWS_VERSION_BUILD || false !== \getenv ('ANSICON ' ) || 'ON ' === \getenv ('ConEmuANSI ' ) || 'xterm ' === \getenv ('TERM ' )) {
5769 return true ;
5870 }
5971 return false ;
@@ -62,43 +74,68 @@ public static function isSupported()
6274 }
6375 }
6476
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" ;
77+ /**
78+ * Format a multilines content.
79+ *
80+ * @param string $content
81+ * @param string $prefix
82+ * @return string
83+ */
84+ public static function formatContent ($ content , $ prefix = " · " ) {
85+ $ content = str_replace ("<br> " , "\n" , $ content );
86+ $ content = self ::formatHtml ($ content );
87+ $ content = strip_tags ($ content );
88+ return "\n" . self ::prefixLines ($ content , $ prefix ) . "\n" ;
7089 }
7190
72- public static function showMessage ($ content , $ type ='info ' ,$ title =null ) {
73- $ header =" ■ " .$ type ;
74- if (isset ($ title )){
75- $ header .=' : ' .$ title ;
91+ /**
92+ * Return a formated message.
93+ *
94+ * @param string $content
95+ * @param string $type
96+ * @param ?string $title
97+ * @return string
98+ */
99+ public static function showMessage ($ content , $ type = 'info ' , $ title = null ) {
100+ $ header = " ■ " . $ type ;
101+ if (isset ($ title )) {
102+ $ header .= ' : ' . $ title ;
76103 }
77- $ result= self ::formatContent ($ content );
78- switch ($ type ){
104+ $ result = self ::formatContent ($ content );
105+ switch ($ type ) {
79106 case 'error ' :
80- $ header= self ::colorize ($ header ,self ::LIGHT_RED );
107+ $ header = self ::colorize ($ header , self ::LIGHT_RED );
81108 break ;
82109 case 'success ' :
83- $ header= self ::colorize ($ header ,self ::GREEN );
110+ $ header = self ::colorize ($ header , self ::GREEN );
84111 break ;
85112 case 'info ' :
86- $ header= self ::colorize ($ header ,self ::CYAN );
113+ $ header = self ::colorize ($ header , self ::CYAN );
87114 break ;
88115 case 'warning ' :
89- $ header= self ::colorize ($ header ,self ::LIGHT_GRAY );
116+ $ header = self ::colorize ($ header , self ::LIGHT_GRAY );
90117 break ;
91118 }
92- $ result =rtrim ($ result ,"\n" );
93- return ConsoleTable::borderType ([[$ header .$ result ]], $ type );
119+ $ result = rtrim ($ result , "\n" );
120+ return ConsoleTable::borderType ([
121+ [
122+ $ header . $ result
123+ ]
124+ ], $ type );
94125 }
95126
96- public static function formatHtml ($ str ){
97- $ reg ='@<(b)>(.+?)</\1>@i ' ;
98- if (!self ::isSupported ()){
99- return preg_replace ($ reg , '$2 ' , $ str );
127+ /**
128+ * Format an html message (only for <b>bold values</b>).
129+ *
130+ * @param string $str
131+ * @return mixed
132+ */
133+ public static function formatHtml ($ str ) {
134+ $ reg = '@<(b)>(.+?)</\1>@i ' ;
135+ if (! self ::isSupported ()) {
136+ return \preg_replace ($ reg , '$2 ' , $ str );
100137 }
101- return preg_replace ($ reg , self ::escape (self ::BOLD ). '$2 ' . self ::escape (self ::END_BOLD ), $ str );
138+ return \ preg_replace ($ reg , self ::escape (self ::BOLD ) . '$2 ' . self ::escape (self ::END_BOLD ), $ str );
102139 }
103140}
104141
0 commit comments