curruwilla /
console-pretty-log
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace ConsolePrettyLog; |
||||
| 4 | |||||
| 5 | /** |
||||
| 6 | * Class Line |
||||
| 7 | * @package ConsolePrettyLog |
||||
| 8 | * @author William Alvares <30/07/2022 20:09> |
||||
| 9 | */ |
||||
| 10 | class Line |
||||
| 11 | { |
||||
| 12 | /** |
||||
| 13 | * @var int[] |
||||
| 14 | */ |
||||
| 15 | private array $codes; |
||||
| 16 | /** |
||||
| 17 | * @var array|null |
||||
| 18 | */ |
||||
| 19 | private ?array $texts; |
||||
| 20 | /** |
||||
| 21 | * @var array|null |
||||
| 22 | */ |
||||
| 23 | private ?array $textsInitial; |
||||
| 24 | /** |
||||
| 25 | * @var array|null |
||||
| 26 | */ |
||||
| 27 | private ?array $styles; |
||||
| 28 | /** |
||||
| 29 | * @var array|null |
||||
| 30 | */ |
||||
| 31 | private ?array $stylesInitial; |
||||
| 32 | /** |
||||
| 33 | * @var string |
||||
| 34 | */ |
||||
| 35 | private string $mask; |
||||
| 36 | /** |
||||
| 37 | * @var array|null |
||||
| 38 | */ |
||||
| 39 | private ?array $columnsSize; |
||||
| 40 | /** |
||||
| 41 | * @var string |
||||
| 42 | */ |
||||
| 43 | private string $separator; |
||||
| 44 | /** |
||||
| 45 | * @var string |
||||
| 46 | */ |
||||
| 47 | private string $paddingCharacter; |
||||
| 48 | /** |
||||
| 49 | * @var bool |
||||
| 50 | */ |
||||
| 51 | private bool $enableDate; |
||||
| 52 | /** |
||||
| 53 | * @var string |
||||
| 54 | */ |
||||
| 55 | private string $dateFormat; |
||||
| 56 | |||||
| 57 | /** |
||||
| 58 | */ |
||||
| 59 | public function __construct() |
||||
| 60 | { |
||||
| 61 | $this->codes = [ |
||||
| 62 | 'bold' => 1, |
||||
| 63 | 'italic' => 3, |
||||
| 64 | 'underline' => 4, |
||||
| 65 | 'strikethrough' => 9, |
||||
| 66 | 'red' => 31, |
||||
| 67 | 'green' => 32, |
||||
| 68 | 'yellow' => 33, |
||||
| 69 | 'blue' => 34, |
||||
| 70 | 'magenta' => 35, |
||||
| 71 | 'cyan' => 36, |
||||
| 72 | 'white' => 37, |
||||
| 73 | 'redbg' => 41, |
||||
| 74 | 'greenbg' => 42, |
||||
| 75 | 'yellowbg' => 43, |
||||
| 76 | 'bluebg' => 44, |
||||
| 77 | 'magentabg' => 45, |
||||
| 78 | 'cyanbg' => 46, |
||||
| 79 | 'lightgreybg' => 47 |
||||
| 80 | ]; |
||||
| 81 | |||||
| 82 | $this->mask = ""; |
||||
| 83 | $this->textsInitial = null; |
||||
| 84 | $this->texts = null; |
||||
| 85 | $this->stylesInitial = null; |
||||
| 86 | $this->styles = null; |
||||
| 87 | $this->columnsSize = null; |
||||
| 88 | $this->separator = "|"; |
||||
| 89 | $this->paddingCharacter = "."; |
||||
| 90 | $this->enableDate = true; |
||||
| 91 | $this->dateFormat = "Y-m-d H:i:s"; |
||||
| 92 | } |
||||
| 93 | |||||
| 94 | /** |
||||
| 95 | * @param string $separator |
||||
| 96 | * @return Line |
||||
| 97 | */ |
||||
| 98 | public function separator(string $separator): Line |
||||
| 99 | { |
||||
| 100 | $this->separator = $separator; |
||||
| 101 | return $this; |
||||
| 102 | } |
||||
| 103 | |||||
| 104 | /** |
||||
| 105 | * @param string $paddingCharacter |
||||
| 106 | * @return Line |
||||
| 107 | */ |
||||
| 108 | public function paddingCharacter(string $paddingCharacter): Line |
||||
| 109 | { |
||||
| 110 | $this->paddingCharacter = $paddingCharacter; |
||||
| 111 | return $this; |
||||
| 112 | } |
||||
| 113 | |||||
| 114 | /** |
||||
| 115 | * @param bool $enableDate |
||||
| 116 | * @return Line |
||||
| 117 | */ |
||||
| 118 | public function enableDate(bool $enableDate = true): Line |
||||
| 119 | { |
||||
| 120 | $this->enableDate = $enableDate; |
||||
| 121 | return $this; |
||||
| 122 | } |
||||
| 123 | |||||
| 124 | /** |
||||
| 125 | * @param string $dateFormat |
||||
| 126 | * @return Line |
||||
| 127 | */ |
||||
| 128 | public function dateFormat(string $dateFormat): Line |
||||
| 129 | { |
||||
| 130 | $this->dateFormat = $dateFormat; |
||||
| 131 | return $this; |
||||
| 132 | } |
||||
| 133 | |||||
| 134 | /** |
||||
| 135 | * @param string|null $text |
||||
| 136 | * @param array|null $styles |
||||
| 137 | * @return Line |
||||
| 138 | */ |
||||
| 139 | public function text(?string $text, ?array $styles = []): Line |
||||
| 140 | { |
||||
| 141 | $this->texts[] = $this->textEnconding($text); |
||||
| 142 | $this->styles[] = $this->getStyleCodes($styles); |
||||
| 143 | |||||
| 144 | return $this; |
||||
| 145 | } |
||||
| 146 | |||||
| 147 | /** |
||||
| 148 | * @param string|null $text |
||||
| 149 | * @param array|null $styles |
||||
| 150 | * @return Line |
||||
| 151 | */ |
||||
| 152 | public function textInitial(?string $text, ?array $styles = []): Line |
||||
| 153 | { |
||||
| 154 | $this->textsInitial[] = $this->textEnconding($text); |
||||
| 155 | $this->stylesInitial[] = $this->getStyleCodes($styles); |
||||
| 156 | |||||
| 157 | return $this; |
||||
| 158 | } |
||||
| 159 | |||||
| 160 | /** |
||||
| 161 | * @param string|null $text |
||||
| 162 | * @return false|string |
||||
| 163 | */ |
||||
| 164 | private function textEnconding(?string $text) |
||||
| 165 | { |
||||
| 166 | return iconv('UTF-8', 'ascii//TRANSLIT', $text); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 167 | } |
||||
| 168 | |||||
| 169 | /** |
||||
| 170 | * @param array|null $styles |
||||
| 171 | * @return string |
||||
| 172 | */ |
||||
| 173 | private function getStyleCodes(?array $styles = []): string |
||||
| 174 | { |
||||
| 175 | $formatMap = array_map(function ($v) { |
||||
| 176 | return $this->codes[$v]; |
||||
| 177 | }, $styles); |
||||
| 178 | |||||
| 179 | return implode(';', $formatMap); |
||||
| 180 | } |
||||
| 181 | |||||
| 182 | /** |
||||
| 183 | * @param array|null $columnsSize |
||||
| 184 | * @return Line |
||||
| 185 | */ |
||||
| 186 | public function columnsSize(?array $columnsSize): Line |
||||
| 187 | { |
||||
| 188 | $this->columnsSize = $columnsSize; |
||||
| 189 | return $this; |
||||
| 190 | } |
||||
| 191 | |||||
| 192 | /** |
||||
| 193 | * @return void |
||||
| 194 | */ |
||||
| 195 | public function print(): void |
||||
| 196 | { |
||||
| 197 | if (php_sapi_name() !== 'cli') { |
||||
| 198 | return; |
||||
| 199 | } |
||||
| 200 | |||||
| 201 | $this->textsInitial(); |
||||
| 202 | |||||
| 203 | if ($this->columnsSize === null) { |
||||
| 204 | $this->texts(); |
||||
| 205 | } else { |
||||
| 206 | $this->textsWithPaddings(); |
||||
| 207 | } |
||||
| 208 | |||||
| 209 | if ($this->enableDate === true) { |
||||
| 210 | echo sprintf("\033[34m[%s] \033[0m", date($this->dateFormat)); |
||||
| 211 | } |
||||
| 212 | |||||
| 213 | echo vsprintf($this->mask . "\n", $this->texts); |
||||
|
0 ignored issues
–
show
It seems like
$this->texts can also be of type null; however, parameter $values of vsprintf() does only seem to accept array, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 214 | |||||
| 215 | $this->newLine(); |
||||
| 216 | } |
||||
| 217 | |||||
| 218 | /** |
||||
| 219 | * @return void |
||||
| 220 | */ |
||||
| 221 | private function texts(): void |
||||
| 222 | { |
||||
| 223 | $count = count($this->texts); |
||||
|
0 ignored issues
–
show
It seems like
$this->texts can also be of type null; however, parameter $value of count() does only seem to accept Countable|array, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 224 | |||||
| 225 | $i = 0; |
||||
| 226 | foreach ($this->texts as $key => $text) { |
||||
| 227 | $i++; |
||||
| 228 | |||||
| 229 | $codes = $this->styles[$key] ?? null; |
||||
| 230 | if ($codes === null) { |
||||
| 231 | continue; |
||||
| 232 | } |
||||
| 233 | |||||
| 234 | $this->mask .= "\033[{$codes}m"; |
||||
| 235 | $this->mask .= "%s"; |
||||
| 236 | $this->mask .= "\033[0m"; |
||||
| 237 | $this->mask .= $count === $i ? "" : " {$this->separator} "; |
||||
| 238 | } |
||||
| 239 | } |
||||
| 240 | |||||
| 241 | /** |
||||
| 242 | * @return void |
||||
| 243 | */ |
||||
| 244 | private function textsWithPaddings(): void |
||||
| 245 | { |
||||
| 246 | $count = count($this->columnsSize); |
||||
|
0 ignored issues
–
show
It seems like
$this->columnsSize can also be of type null; however, parameter $value of count() does only seem to accept Countable|array, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 247 | |||||
| 248 | $i = 0; |
||||
| 249 | foreach ($this->columnsSize as $key => $size) { |
||||
| 250 | $i++; |
||||
| 251 | |||||
| 252 | $codes = $this->styles[$key] ?? null; |
||||
| 253 | if ($codes === null) { |
||||
| 254 | continue; |
||||
| 255 | } |
||||
| 256 | |||||
| 257 | $this->mask .= "\033[{$codes}m"; |
||||
| 258 | $this->mask .= "%-'{$this->paddingCharacter}{$size}s"; |
||||
| 259 | $this->mask .= "\033[0m"; |
||||
| 260 | $this->mask .= $count === $i ? "" : " {$this->separator} "; |
||||
| 261 | } |
||||
| 262 | } |
||||
| 263 | |||||
| 264 | /** |
||||
| 265 | * @return void |
||||
| 266 | */ |
||||
| 267 | private function textsInitial(): void |
||||
| 268 | { |
||||
| 269 | if ($this->textsInitial === null) { |
||||
| 270 | return; |
||||
| 271 | } |
||||
| 272 | |||||
| 273 | $this->texts = array_merge($this->textsInitial, $this->texts); |
||||
|
0 ignored issues
–
show
It seems like
$this->texts can also be of type null; however, parameter $arrays of array_merge() does only seem to accept array, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 274 | $this->styles = array_merge($this->stylesInitial, $this->styles); |
||||
| 275 | } |
||||
| 276 | |||||
| 277 | /** |
||||
| 278 | * @return void |
||||
| 279 | */ |
||||
| 280 | private function newLine(): void |
||||
| 281 | { |
||||
| 282 | $this->texts = []; |
||||
| 283 | $this->styles = []; |
||||
| 284 | $this->mask = ""; |
||||
| 285 | } |
||||
| 286 | } |