Ver Fonte

[Console] added a way to use style without defining a name (<fg=blue;bg=red>...</>)

Fabien Potencier há 15 anos atrás
pai
commit
aaa6aba60b
1 ficheiros alterados com 14 adições e 5 exclusões
  1. 14 5
      src/Symfony/Components/Console/Output/Output.php

+ 14 - 5
src/Symfony/Components/Console/Output/Output.php

@@ -175,9 +175,9 @@ abstract class Output implements OutputInterface
      */
     protected function format($message)
     {
-        $message = preg_replace_callback('#<([a-z][a-z0-9\-_]+)>#i', array($this, 'replaceStartStyle'), $message);
+        $message = preg_replace_callback('#<([a-z][a-z0-9\-_=;]+)>#i', array($this, 'replaceStartStyle'), $message);
 
-        return preg_replace_callback('#</([a-z][a-z0-9\-_]+)>#i', array($this, 'replaceEndStyle'), $message);
+        return preg_replace_callback('#</([a-z][a-z0-9\-_]*)?>#i', array($this, 'replaceEndStyle'), $message);
     }
 
     /**
@@ -189,11 +189,20 @@ abstract class Output implements OutputInterface
             return '';
         }
 
-        if (!isset(static::$styles[strtolower($match[1])])) {
-            throw new \InvalidArgumentException(sprintf('Unknown style "%s".', $match[1]));
+        if (isset(static::$styles[strtolower($match[1])])) {
+            $parameters = static::$styles[strtolower($match[1])];
+        } else {
+            // bg=blue;fg=red
+            if (!preg_match_all('/([^=]+)=([^;]+)(;|$)/', strtolower($match[1]), $matches, PREG_SET_ORDER)) {
+                throw new \InvalidArgumentException(sprintf('Unknown style "%s".', $match[1]));
+            }
+
+            $parameters = array();
+            foreach ($matches as $match) {
+                $parameters[$match[1]] = $match[2];
+            }
         }
 
-        $parameters = static::$styles[strtolower($match[1])];
         $codes = array();
 
         if (isset($parameters['fg'])) {