1111
1212namespace Symfony \Component \Console \Formatter ;
1313
14- use Symfony \Component \Console \Exception \ InvalidArgumentException ;
14+ use Symfony \Component \Console \Color ;
1515
1616/**
1717 * Formatter style class for defining styles.
2020 */
2121class OutputFormatterStyle implements OutputFormatterStyleInterface
2222{
23- private static $ availableForegroundColors = [
24- 'black ' => ['set ' => 30 , 'unset ' => 39 ],
25- 'red ' => ['set ' => 31 , 'unset ' => 39 ],
26- 'green ' => ['set ' => 32 , 'unset ' => 39 ],
27- 'yellow ' => ['set ' => 33 , 'unset ' => 39 ],
28- 'blue ' => ['set ' => 34 , 'unset ' => 39 ],
29- 'magenta ' => ['set ' => 35 , 'unset ' => 39 ],
30- 'cyan ' => ['set ' => 36 , 'unset ' => 39 ],
31- 'white ' => ['set ' => 37 , 'unset ' => 39 ],
32- 'default ' => ['set ' => 39 , 'unset ' => 39 ],
33- ];
34- private static $ availableBackgroundColors = [
35- 'black ' => ['set ' => 40 , 'unset ' => 49 ],
36- 'red ' => ['set ' => 41 , 'unset ' => 49 ],
37- 'green ' => ['set ' => 42 , 'unset ' => 49 ],
38- 'yellow ' => ['set ' => 43 , 'unset ' => 49 ],
39- 'blue ' => ['set ' => 44 , 'unset ' => 49 ],
40- 'magenta ' => ['set ' => 45 , 'unset ' => 49 ],
41- 'cyan ' => ['set ' => 46 , 'unset ' => 49 ],
42- 'white ' => ['set ' => 47 , 'unset ' => 49 ],
43- 'default ' => ['set ' => 49 , 'unset ' => 49 ],
44- ];
45- private static $ availableOptions = [
46- 'bold ' => ['set ' => 1 , 'unset ' => 22 ],
47- 'underscore ' => ['set ' => 4 , 'unset ' => 24 ],
48- 'blink ' => ['set ' => 5 , 'unset ' => 25 ],
49- 'reverse ' => ['set ' => 7 , 'unset ' => 27 ],
50- 'conceal ' => ['set ' => 8 , 'unset ' => 28 ],
51- ];
52-
23+ private $ color ;
5324 private $ foreground ;
5425 private $ background ;
26+ private $ options ;
5527 private $ href ;
56- private $ options = [];
5728 private $ handlesHrefGracefully ;
5829
5930 /**
@@ -64,51 +35,23 @@ class OutputFormatterStyle implements OutputFormatterStyleInterface
6435 */
6536 public function __construct (string $ foreground = null , string $ background = null , array $ options = [])
6637 {
67- if (null !== $ foreground ) {
68- $ this ->setForeground ($ foreground );
69- }
70- if (null !== $ background ) {
71- $ this ->setBackground ($ background );
72- }
73- if (\count ($ options )) {
74- $ this ->setOptions ($ options );
75- }
38+ $ this ->color = new Color ($ this ->foreground = $ foreground ?: '' , $ this ->background = $ background ?: '' , $ this ->options = $ options );
7639 }
7740
7841 /**
7942 * {@inheritdoc}
8043 */
8144 public function setForeground (string $ color = null )
8245 {
83- if (null === $ color ) {
84- $ this ->foreground = null ;
85-
86- return ;
87- }
88-
89- if (!isset (static ::$ availableForegroundColors [$ color ])) {
90- throw new InvalidArgumentException (sprintf ('Invalid foreground color specified: "%s". Expected one of (%s). ' , $ color , implode (', ' , array_keys (static ::$ availableForegroundColors ))));
91- }
92-
93- $ this ->foreground = static ::$ availableForegroundColors [$ color ];
46+ $ this ->color = new Color ($ this ->foreground = $ color ?: '' , $ this ->background , $ this ->options );
9447 }
9548
9649 /**
9750 * {@inheritdoc}
9851 */
9952 public function setBackground (string $ color = null )
10053 {
101- if (null === $ color ) {
102- $ this ->background = null ;
103-
104- return ;
105- }
106-
107- if (!isset (static ::$ availableBackgroundColors [$ color ])) {
108- throw new InvalidArgumentException (sprintf ('Invalid background color specified: "%s". Expected one of (%s). ' , $ color , implode (', ' , array_keys (static ::$ availableBackgroundColors ))));
109- }
110-
111- $ this ->background = static ::$ availableBackgroundColors [$ color ];
54+ $ this ->color = new Color ($ this ->foreground , $ this ->background = $ color ?: '' , $ this ->options );
11255 }
11356
11457 public function setHref (string $ url ): void
@@ -121,76 +64,44 @@ public function setHref(string $url): void
12164 */
12265 public function setOption (string $ option )
12366 {
124- if (!isset (static ::$ availableOptions [$ option ])) {
125- throw new InvalidArgumentException (sprintf ('Invalid option specified: "%s". Expected one of (%s). ' , $ option , implode (', ' , array_keys (static ::$ availableOptions ))));
126- }
127-
128- if (!\in_array (static ::$ availableOptions [$ option ], $ this ->options )) {
129- $ this ->options [] = static ::$ availableOptions [$ option ];
130- }
67+ $ this ->options [] = $ option ;
68+ $ this ->color = new Color ($ this ->foreground , $ this ->background , $ this ->options );
13169 }
13270
13371 /**
13472 * {@inheritdoc}
13573 */
13674 public function unsetOption (string $ option )
13775 {
138- if (!isset (static ::$ availableOptions [$ option ])) {
139- throw new InvalidArgumentException (sprintf ('Invalid option specified: "%s". Expected one of (%s). ' , $ option , implode (', ' , array_keys (static ::$ availableOptions ))));
140- }
141-
142- $ pos = array_search (static ::$ availableOptions [$ option ], $ this ->options );
76+ $ pos = array_search ($ option , $ this ->options );
14377 if (false !== $ pos ) {
14478 unset($ this ->options [$ pos ]);
14579 }
80+
81+ $ this ->color = new Color ($ this ->foreground , $ this ->background , $ this ->options );
14682 }
14783
14884 /**
14985 * {@inheritdoc}
15086 */
15187 public function setOptions (array $ options )
15288 {
153- $ this ->options = [];
154-
155- foreach ($ options as $ option ) {
156- $ this ->setOption ($ option );
157- }
89+ $ this ->color = new Color ($ this ->foreground , $ this ->background , $ this ->options = $ options );
15890 }
15991
16092 /**
16193 * {@inheritdoc}
16294 */
16395 public function apply (string $ text )
16496 {
165- $ setCodes = [];
166- $ unsetCodes = [];
167-
16897 if (null === $ this ->handlesHrefGracefully ) {
16998 $ this ->handlesHrefGracefully = 'JetBrains-JediTerm ' !== getenv ('TERMINAL_EMULATOR ' ) && !getenv ('KONSOLE_VERSION ' );
17099 }
171100
172- if (null !== $ this ->foreground ) {
173- $ setCodes [] = $ this ->foreground ['set ' ];
174- $ unsetCodes [] = $ this ->foreground ['unset ' ];
175- }
176- if (null !== $ this ->background ) {
177- $ setCodes [] = $ this ->background ['set ' ];
178- $ unsetCodes [] = $ this ->background ['unset ' ];
179- }
180-
181- foreach ($ this ->options as $ option ) {
182- $ setCodes [] = $ option ['set ' ];
183- $ unsetCodes [] = $ option ['unset ' ];
184- }
185-
186101 if (null !== $ this ->href && $ this ->handlesHrefGracefully ) {
187102 $ text = "\033]8;; $ this ->href \033\\$ text \033]8;; \033\\" ;
188103 }
189104
190- if (0 === \count ($ setCodes )) {
191- return $ text ;
192- }
193-
194- return sprintf ("\033[%sm%s \033[%sm " , implode ('; ' , $ setCodes ), $ text , implode ('; ' , $ unsetCodes ));
105+ return $ this ->color ->apply ($ text );
195106 }
196107}
0 commit comments