2
2
namespace Ubiquity \devtools \cmd ;
3
3
4
4
class ConsoleTable {
5
- const TOP_LEFT ='┌ ' , TOP_RIGHT ='┐ ' ;
5
+ const TOP_LEFT ='╭ ' , TOP_RIGHT ='╮ ' ;
6
6
7
7
const H_LINE ='─ ' ,V_LINE ='│ ' ;
8
8
9
9
const M_COL_TOP ='┬ ' ,M_COL_ROW ='┼ ' ,M_COL_BOTTOM ='┴ ' ;
10
10
11
11
const M_ROW_LEFT ='├ ' ,M_ROW_RIGHT ='┤ ' ;
12
12
13
- const BOTTOM_LEFT ='└ ' ,BOTTOM_RIGHT ='┘ ' ;
13
+ const BOTTOM_LEFT ='╰ ' ,BOTTOM_RIGHT ='╯ ' ;
14
14
15
15
const BINARY_VALUES =[
16
16
'0011 ' =>self ::TOP_LEFT ,
@@ -23,8 +23,7 @@ class ConsoleTable {
23
23
'1110 ' =>self ::M_COL_BOTTOM ,
24
24
'1100 ' =>self ::BOTTOM_RIGHT ,
25
25
'1010 ' =>self ::H_LINE ,
26
- '0101 ' =>self ::V_LINE ,
27
- '0000 ' =>' '
26
+ '0101 ' =>self ::V_LINE
28
27
];
29
28
30
29
@@ -50,10 +49,14 @@ class ConsoleTable {
50
49
51
50
private $ rowCount ;
52
51
53
- private $ padding =1 ;
52
+ private $ padding =5 ;
54
53
55
54
private $ indent =0 ;
56
55
56
+ private $ borderColor =ConsoleFormatter::LIGHT_GRAY ;
57
+
58
+ private $ preserveSpaceBefore =false ;
59
+
57
60
/**
58
61
* Get the printable cell content
59
62
* @param integer $index The column index
@@ -68,25 +71,32 @@ private function getCellOutput($index, $row = null){
68
71
if ($ index === 0 ) {
69
72
$ output .= str_repeat (' ' , $ this ->indent );
70
73
}
71
- $ output .=($ this ->v_lines [$ index ]==1 )? self :: V_LINE :' ' ;
74
+ $ output .=($ this ->v_lines [$ index ]==1 )? $ this -> getVLine () :' ' ;
72
75
73
76
$ output .= $ padding ; # left padding
74
77
if (is_string ($ cell )){
75
- $ cell = trim (preg_replace ('/\s+/ ' , ' ' , $ cell )); # remove line breaks
78
+ $ cell = rtrim (preg_replace ('/\s+/ ' , ' ' , $ cell )); # remove line breaks
79
+ if (!$ this ->preserveSpaceBefore ){
80
+ $ cell =ltrim ($ cell );
81
+ }
76
82
}else {
77
83
$ cell ='{} ' ;
78
84
}
79
85
$ content = preg_replace ('#\x1b[[][^A-Za-z]*[A-Za-z]# ' , '' , $ cell );
80
- $ delta = mb_strlen ($ cell ,'UTF-8 ' ) - mb_strlen ($ content ,'UTF-8 ' )+$ this ->padding ;
86
+
87
+ $ delta = -mb_strlen ($ cell ,'UTF-8 ' ) + mb_strlen ($ content ,'UTF-8 ' )+$ this ->padding ;
81
88
$ output .= $ this ->mb_str_pad ($ cell , $ width -$ delta , $ row ? ' ' : '- ' ); # cell content
82
89
83
- //$output .= $padding; # right padding
84
90
if ($ row && $ index == count ($ row )-1 ) {
85
- $ output .= ($ this ->v_lines [count ($ row )]==1 )?self :: V_LINE :' ' ;
91
+ $ output .= ($ this ->v_lines [count ($ row )]==1 )?$ this -> getVLine () :' ' ;
86
92
}
87
93
return $ output ;
88
94
}
89
95
96
+ private function getVLine (){
97
+ return ConsoleFormatter::colorize (self ::V_LINE ,$ this ->borderColor );
98
+ }
99
+
90
100
private function initializeBorders (){
91
101
$ this ->h_lines =array_fill (0 ,sizeof ($ this ->datas )+1 , 1 );
92
102
$ this ->v_lines =array_fill (0 , $ this ->colCount +1 , 1 );
@@ -103,17 +113,33 @@ private function initializeBorders(){
103
113
* @return string
104
114
*/
105
115
private function mb_str_pad ($ str , $ pad_len , $ pad_str = ' ' , $ dir = STR_PAD_RIGHT , $ encoding = NULL ){
106
- $ encoding = $ encoding === NULL ? mb_internal_encoding () : $ encoding ;
107
- $ padBefore = $ dir === STR_PAD_BOTH || $ dir === STR_PAD_LEFT ;
108
- $ padAfter = $ dir === STR_PAD_BOTH || $ dir === STR_PAD_RIGHT ;
109
- $ pad_len -= mb_strlen ($ str , $ encoding );
110
- $ targetLen = $ padBefore && $ padAfter ? $ pad_len / 2 : $ pad_len ;
111
- $ strToRepeatLen = mb_strlen ($ pad_str , $ encoding );
112
- $ repeatTimes = ceil ($ targetLen / $ strToRepeatLen );
113
- $ repeatedString = str_repeat ($ pad_str , max (0 , $ repeatTimes )); // safe if used with valid utf-8 strings
114
- $ before = $ padBefore ? mb_substr ($ repeatedString , 0 , floor ($ targetLen ), $ encoding ) : '' ;
115
- $ after = $ padAfter ? mb_substr ($ repeatedString , 0 , ceil ($ targetLen ), $ encoding ) : '' ;
116
- return $ before . $ str . $ after ;
116
+ $ content = preg_replace ('#\x1b[[][^A-Za-z]*[A-Za-z]# ' , '' , $ str );
117
+ $ str_len = mb_strlen ($ content );
118
+ $ pad_str_len = mb_strlen ($ pad_str );
119
+ if (!$ str_len && ($ dir == STR_PAD_RIGHT || $ dir == STR_PAD_LEFT )) {
120
+ $ str_len = 1 ; // @debug
121
+ }
122
+ if (!$ pad_len || !$ pad_str_len || $ pad_len <= $ str_len ) {
123
+ return $ str ;
124
+ }
125
+
126
+ $ result = null ;
127
+ $ repeat = ceil ($ str_len - $ pad_str_len + $ pad_len );
128
+ if ($ dir == STR_PAD_RIGHT ) {
129
+ $ result = $ str . str_repeat ($ pad_str , $ repeat );
130
+ $ result = mb_substr ($ result , 0 , $ pad_len );
131
+ } else if ($ dir == STR_PAD_LEFT ) {
132
+ $ result = str_repeat ($ pad_str , $ repeat ) . $ str ;
133
+ $ result = mb_substr ($ result , -$ pad_len );
134
+ } else if ($ dir == STR_PAD_BOTH ) {
135
+ $ length = ($ pad_len - $ str_len ) / 2 ;
136
+ $ repeat = ceil ($ length / $ pad_str_len );
137
+ $ result = mb_substr (str_repeat ($ pad_str , $ repeat ), 0 , floor ($ length ))
138
+ . $ str
139
+ . mb_substr (str_repeat ($ pad_str , $ repeat ), 0 , ceil ($ length ));
140
+ }
141
+
142
+ return $ result ;
117
143
}
118
144
119
145
public function setDatas ($ datas ){
@@ -198,7 +224,7 @@ private function border($row){
198
224
$ res .=str_repeat ($ line , $ this ->colWidths [$ i ]);
199
225
}
200
226
$ res .=$ this ->getBorderValue ($ row , $ this ->colCount );
201
- return $ res ;
227
+ return ConsoleFormatter:: colorize ( $ res, $ this -> borderColor ) ;
202
228
}
203
229
204
230
private function getBorderValue ($ row ,$ col ){
@@ -296,5 +322,46 @@ public function setIndent($indent) {
296
322
$ this ->indent = $ indent ;
297
323
}
298
324
325
+ /**
326
+ * @param string $borderColor
327
+ */
328
+ public function setBorderColor ($ borderColor ) {
329
+ $ this ->borderColor = $ borderColor ;
330
+ }
331
+
332
+ public static function borderColor ($ text ,$ color =ConsoleFormatter::LIGHT_GRAY ){
333
+ $ border =new ConsoleTable ();
334
+ $ border ->setIndent (5 );
335
+ $ border ->setBorderColor ($ color );
336
+ $ border ->setDatas ($ text );
337
+ $ border ->setPreserveSpaceBefore (true );
338
+ return $ border ->getTable ();
339
+ }
340
+
341
+ public static function borderType ($ text ,$ type ){
342
+ switch ($ type ){
343
+ case 'error ' :
344
+ $ color =ConsoleFormatter::LIGHT_RED ;
345
+ break ;
346
+ case 'success ' :
347
+ $ color =ConsoleFormatter::LIGHT_GREEN ;
348
+ break ;
349
+ case 'info ' :
350
+ $ color =ConsoleFormatter::LIGHT_CYAN ;
351
+ break ;
352
+ case 'warning ' :
353
+ $ color =ConsoleFormatter::LIGHT_GRAY ;
354
+ break ;
355
+
356
+ }
357
+ return self ::borderColor ($ text ,$ color );
358
+ }
359
+ /**
360
+ * @param boolean $preserveSpaceBefore
361
+ */
362
+ public function setPreserveSpaceBefore ($ preserveSpaceBefore ) {
363
+ $ this ->preserveSpaceBefore = $ preserveSpaceBefore ;
364
+ }
365
+
299
366
}
300
367
0 commit comments