22
33namespace jblond \Diff \Renderer \Text ;
44
5+ use InvalidArgumentException ;
56use jblond \cli \CliColors ;
67use jblond \Diff \Renderer \RendererAbstract ;
78
9+
810/**
911 * Unified diff generator for PHP DiffLib.
1012 *
@@ -25,6 +27,11 @@ class UnifiedCli extends RendererAbstract
2527 */
2628 private $ colors ;
2729
30+ /**
31+ * @var array
32+ */
33+ protected $ options ;
34+
2835 /**
2936 * UnifiedCli constructor.
3037 * @param array $options
@@ -33,14 +40,45 @@ public function __construct(array $options = [])
3340 {
3441 parent ::__construct ($ options );
3542 $ this ->colors = new CliColors ();
43+ $ this ->options = $ options ;
3644 }
3745
3846 /**
3947 * Render and return a unified diff.
4048 *
4149 * @return string Direct Output to the console
50+ * @throws InvalidArgumentException
4251 */
4352 public function render (): string
53+ {
54+ if (!isset ($ this ->options ['cliColor ' ])) {
55+ return $ this ->output ();
56+ }
57+ if (isset ($ this ->options ['cliColor ' ]) && $ this ->options ['cliColor ' ] == 'simple ' ) {
58+ return $ this ->output ();
59+ }
60+ throw new InvalidArgumentException ('Invalid cliColor option ' );
61+ }
62+
63+
64+ /**
65+ * @param $string
66+ * @param string $color
67+ * @return string
68+ */
69+ private function colorizeString ($ string , $ color = '' ): string
70+ {
71+ if (isset ($ this ->options ['cliColor ' ]) && $ this ->options ['cliColor ' ] == 'simple ' ) {
72+ return $ this ->colors ->getColoredString ($ string , $ color );
73+ }
74+ return $ string ;
75+ }
76+
77+ /**
78+ * Render and return a unified colored diff.
79+ * @return string
80+ */
81+ private function output (): string
4482 {
4583 $ diff = '' ;
4684 $ opCodes = $ this ->diff ->getGroupedOpCodes ();
@@ -56,7 +94,7 @@ public function render(): string
5694 $ i2 = -1 ;
5795 }
5896
59- $ diff .= $ this ->colors -> getColoredString (
97+ $ diff .= $ this ->colorizeString (
6098 '@@ - ' . ($ i1 + 1 ) . ', ' . ($ i2 - $ i1 ) . ' + ' . ($ j1 + 1 ) . ', ' . ($ j2 - $ j1 ) . " @@ \n" ,
6199 'purple '
62100 );
@@ -66,22 +104,22 @@ public function render(): string
66104 "\n " ,
67105 $ this ->diff ->getArrayRange ($ this ->diff ->getVersion1 (), $ i1 , $ i2 )
68106 );
69- $ diff .= $ this ->colors -> getColoredString (' ' . $ string . "\n" , 'grey ' );
107+ $ diff .= $ this ->colorizeString (' ' . $ string . "\n" , 'grey ' );
70108 continue ;
71109 }
72110 if ($ tag == 'replace ' || $ tag == 'delete ' ) {
73111 $ string = implode (
74112 "\n- " ,
75113 $ this ->diff ->getArrayRange ($ this ->diff ->getVersion1 (), $ i1 , $ i2 )
76114 );
77- $ diff .= $ this ->colors -> getColoredString ('- ' . $ string . "\n" , 'light_red ' );
115+ $ diff .= $ this ->colorizeString ('- ' . $ string . "\n" , 'light_red ' );
78116 }
79117 if ($ tag == 'replace ' || $ tag == 'insert ' ) {
80118 $ string = implode (
81119 "\n+ " ,
82120 $ this ->diff ->getArrayRange ($ this ->diff ->getVersion2 (), $ j1 , $ j2 )
83121 );
84- $ diff .= $ this ->colors -> getColoredString ('+ ' . $ string . "\n" , 'light_green ' );
122+ $ diff .= $ this ->colorizeString ('+ ' . $ string . "\n" , 'light_green ' );
85123 }
86124 }
87125 }
0 commit comments