|
| 1 | +<?php |
| 2 | +namespace Ubiquity\devtools\cmd; |
| 3 | + |
| 4 | +class 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'; |
| 8 | + |
| 9 | + /** |
| 10 | + * Returns a colored string |
| 11 | + * @param string $string |
| 12 | + * @param string $color |
| 13 | + * @param string $bgColor |
| 14 | + * @return string |
| 15 | + */ |
| 16 | + public static function colorize($string, $color = null, $bgColor = null) { |
| 17 | + if(!self::isSupported()){ |
| 18 | + return $string; |
| 19 | + } |
| 20 | + $coloredString = ""; |
| 21 | + if (isset($color)) { |
| 22 | + $coloredString .= self::escape($color); |
| 23 | + } |
| 24 | + if (isset($bgColor)) { |
| 25 | + $coloredString .= self::escape($bgColor); |
| 26 | + } |
| 27 | + $coloredString .= $string .self::escape(self::CLEAR); |
| 28 | + return $coloredString; |
| 29 | + } |
| 30 | + |
| 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;}); |
| 34 | + return implode("\n", $lines); |
| 35 | + } |
| 36 | + |
| 37 | + private static function escape($value){ |
| 38 | + return "\033[{$value}m"; |
| 39 | + } |
| 40 | + |
| 41 | + public static function showInfo($content,$dColor=self::CYAN){ |
| 42 | + return self::colorize(self::formatContent($content),$dColor); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @return boolean |
| 47 | + */ |
| 48 | + public static function isSupported() |
| 49 | + { |
| 50 | + if (DIRECTORY_SEPARATOR === '\\') { |
| 51 | + if (\function_exists('sapi_windows_vt100_support') && @sapi_windows_vt100_support(STDOUT)) { |
| 52 | + 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')) { |
| 57 | + return true; |
| 58 | + } |
| 59 | + return false; |
| 60 | + } else { |
| 61 | + return function_exists('posix_isatty') && @posix_isatty(STDOUT); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 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"; |
| 70 | + } |
| 71 | + |
| 72 | + public static function showMessage($content, $type='info',$title=null) { |
| 73 | + $header=" ■ ".$type; |
| 74 | + if(isset($title)){ |
| 75 | + $header.=' : '.$title; |
| 76 | + } |
| 77 | + $result=self::formatContent($content); |
| 78 | + switch ($type){ |
| 79 | + case 'error': |
| 80 | + $header=self::colorize($header,self::LIGHT_RED); |
| 81 | + break; |
| 82 | + case 'success': |
| 83 | + $header=self::colorize($header,self::GREEN); |
| 84 | + break; |
| 85 | + case 'info': |
| 86 | + $header=self::colorize($header,self::CYAN); |
| 87 | + break; |
| 88 | + case 'warning': |
| 89 | + $header=self::colorize($header,self::LIGHT_GRAY); |
| 90 | + break; |
| 91 | + } |
| 92 | + $result=rtrim($result,"\n"); |
| 93 | + return ConsoleTable::borderType([[$header.$result]], $type); |
| 94 | + } |
| 95 | + |
| 96 | + public static function formatHtml($str){ |
| 97 | + $reg='@<(b)>(.+?)</\1>@i'; |
| 98 | + if(!self::isSupported()){ |
| 99 | + return preg_replace($reg, '$2', $str); |
| 100 | + } |
| 101 | + return preg_replace($reg, self::escape(self::BOLD).'$2'.self::escape(self::END_BOLD), $str); |
| 102 | + } |
| 103 | +} |
| 104 | + |
0 commit comments