Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
ConsoleFormatter.php
Go to the documentation of this file.
1<?php
3
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';
19
28 public static function colorize($string, $color = null, $bgColor = null) {
29 if (! self::isSupported()) {
30 return $string;
31 }
32 $coloredString = "";
33 if (isset($color)) {
34 $coloredString .= self::escape($color);
35 }
36 if (isset($bgColor)) {
37 $coloredString .= self::escape($bgColor);
38 }
39 $coloredString .= $string . self::escape(self::CLEAR);
40 return $coloredString;
41 }
42
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 });
49 return implode("\n", $lines);
50 }
51
52 private static function escape($value) {
53 return "\033[{$value}m";
54 }
55
56 public static function showInfo($content, $dColor = self::CYAN) {
57 return self::colorize(self::formatContent($content), $dColor);
58 }
59
64 public static function isSupported() {
65 if (DIRECTORY_SEPARATOR === '\\') {
66 if (\function_exists('sapi_windows_vt100_support') && @sapi_windows_vt100_support(STDOUT)) {
67 return true;
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')) {
69 return true;
70 }
71 return false;
72 } else {
73 return function_exists('posix_isatty') && @posix_isatty(STDOUT);
74 }
75 }
76
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";
89 }
90
99 public static function showMessage($content, $type = 'info', $title = null) {
100 $header = " ■ " . $type;
101 if (isset($title)) {
102 $header .= ' : ' . $title;
103 }
104 $width=Screen::getWidth();
105 $content=wordwrap($content,$width-10);
106 $result = self::formatContent($content);
107 switch ($type) {
108 case 'error':
109 $header = self::colorize($header, self::LIGHT_RED);
110 break;
111 case 'success':
112 $header = self::colorize($header, self::GREEN);
113 break;
114 case 'info':
115 $header = self::colorize($header, self::CYAN);
116 break;
117 case 'warning':
118 $header = self::colorize($header, self::LIGHT_GRAY);
119 break;
120 }
121 $result = \rtrim($result, "\n");
123 [
124 $header . $result
125 ]
126 ], $type);
127 }
128
135 public static function formatHtml($str) {
136 $reg = '@<(b)>(.+?)</\1>@i';
137 if (! self::isSupported()) {
138 return \preg_replace($reg, '$2', $str);
139 }
140 return \preg_replace($reg, self::escape(self::BOLD) . '$2' . self::escape(self::END_BOLD), $str);
141 }
142}
143
Ubiquity\devtools\cmd$ConsoleFormatter This class is part of Ubiquity.
static formatContent($content, $prefix=" · ")
Format a multilines content.
static colorize($string, $color=null, $bgColor=null)
Returns a colored string.
static showMessage($content, $type='info', $title=null)
Return a formated message.
static formatHtml($str)
Format an html message (only for bold values).
static showInfo($content, $dColor=self::CYAN)
static borderType($text, $type)
Surround the text with a border of the specified type.
static getWidth()
Gets the terminal width.
Definition Screen.php:23