Skip to content

Commit c68a192

Browse files
committed
messages relooking
1 parent ab4fb05 commit c68a192

File tree

3 files changed

+95
-27
lines changed

3 files changed

+95
-27
lines changed

src/Ubiquity

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use Ubiquity\devtools\cmd\commands\NewThemeCmd;
2828

2929
class Ubiquity {
3030
use CmdTrait;
31-
private static $version="1.2.2";
31+
private static $version="1.2.3";
3232
private static $appName="Ubiquity devtools";
3333
private static $configOptions;
3434
private static $toolsConfig;

src/devtools/cmd/ConsoleFormatter.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static function isSupported()
5959
}
6060
}
6161

62-
public static function formatContent($content,$prefix=' · '){
62+
public static function formatContent($content,$prefix=" · "){
6363
$content = str_replace ( "<br>", "\n", $content );
6464
$content=self::formatHtml($content);
6565
$content= strip_tags ( $content );
@@ -86,14 +86,15 @@ public static function showMessage($content, $type='info',$title=null) {
8686
$header=self::colorize($header,self::LIGHT_GRAY);
8787
break;
8888
}
89-
return $header.$result;
89+
$result=rtrim($result,"\n");
90+
return ConsoleTable::borderType([[$header.$result]], $type);
9091
}
9192

9293
public static function formatHtml($str){
94+
$reg='@<(b)>(.+?)</\1>@i';
9395
if(!self::isSupported()){
94-
return $str;
96+
return preg_replace($reg, '$2', $str);
9597
}
96-
$reg='@<(b)>(.+?)</\1>@i';
9798
return preg_replace($reg, self::escape(self::BOLD).'$2'.self::escape(self::END_BOLD), $str);
9899
}
99100
}

src/devtools/cmd/ConsoleTable.php

Lines changed: 89 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
namespace Ubiquity\devtools\cmd;
33

44
class ConsoleTable {
5-
const TOP_LEFT='', TOP_RIGHT='';
5+
const TOP_LEFT='', TOP_RIGHT='';
66

77
const H_LINE='',V_LINE='';
88

99
const M_COL_TOP='',M_COL_ROW='',M_COL_BOTTOM='';
1010

1111
const M_ROW_LEFT='',M_ROW_RIGHT='';
1212

13-
const BOTTOM_LEFT='',BOTTOM_RIGHT='';
13+
const BOTTOM_LEFT='',BOTTOM_RIGHT='';
1414

1515
const BINARY_VALUES=[
1616
'0011'=>self::TOP_LEFT,
@@ -23,8 +23,7 @@ class ConsoleTable {
2323
'1110'=>self::M_COL_BOTTOM,
2424
'1100'=>self::BOTTOM_RIGHT,
2525
'1010'=>self::H_LINE,
26-
'0101'=>self::V_LINE,
27-
'0000'=>' '
26+
'0101'=>self::V_LINE
2827
];
2928

3029

@@ -50,10 +49,14 @@ class ConsoleTable {
5049

5150
private $rowCount;
5251

53-
private $padding=1;
52+
private $padding=5;
5453

5554
private $indent=0;
5655

56+
private $borderColor=ConsoleFormatter::LIGHT_GRAY;
57+
58+
private $preserveSpaceBefore=false;
59+
5760
/**
5861
* Get the printable cell content
5962
* @param integer $index The column index
@@ -68,25 +71,32 @@ private function getCellOutput($index, $row = null){
6871
if ($index === 0) {
6972
$output .= str_repeat(' ', $this->indent);
7073
}
71-
$output .=($this->v_lines[$index]==1)? self::V_LINE:' ';
74+
$output .=($this->v_lines[$index]==1)? $this->getVLine():' ';
7275

7376
$output .= $padding; # left padding
7477
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+
}
7682
}else{
7783
$cell='{}';
7884
}
7985
$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;
8188
$output .= $this->mb_str_pad($cell, $width-$delta , $row ? ' ' : '-'); # cell content
8289

83-
//$output .= $padding; # right padding
8490
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():' ';
8692
}
8793
return $output;
8894
}
8995

96+
private function getVLine(){
97+
return ConsoleFormatter::colorize(self::V_LINE,$this->borderColor);
98+
}
99+
90100
private function initializeBorders(){
91101
$this->h_lines=array_fill(0,sizeof($this->datas)+1 , 1);
92102
$this->v_lines=array_fill(0, $this->colCount+1, 1);
@@ -103,17 +113,33 @@ private function initializeBorders(){
103113
* @return string
104114
*/
105115
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;
117143
}
118144

119145
public function setDatas($datas){
@@ -198,7 +224,7 @@ private function border($row){
198224
$res.=str_repeat($line, $this->colWidths[$i]);
199225
}
200226
$res.=$this->getBorderValue($row, $this->colCount);
201-
return $res;
227+
return ConsoleFormatter::colorize($res,$this->borderColor);
202228
}
203229

204230
private function getBorderValue($row,$col){
@@ -296,5 +322,46 @@ public function setIndent($indent) {
296322
$this->indent = $indent;
297323
}
298324

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+
299366
}
300367

0 commit comments

Comments
 (0)