2
2
3
3
namespace jblond \Diff \Renderer \Text ;
4
4
5
+ use InvalidArgumentException ;
5
6
use jblond \cli \CliColors ;
6
7
use jblond \Diff \Renderer \RendererAbstract ;
7
8
9
+
8
10
/**
9
11
* Unified diff generator for PHP DiffLib.
10
12
*
@@ -25,6 +27,11 @@ class UnifiedCli extends RendererAbstract
25
27
*/
26
28
private $ colors ;
27
29
30
+ /**
31
+ * @var array
32
+ */
33
+ protected $ options ;
34
+
28
35
/**
29
36
* UnifiedCli constructor.
30
37
* @param array $options
@@ -33,14 +40,45 @@ public function __construct(array $options = [])
33
40
{
34
41
parent ::__construct ($ options );
35
42
$ this ->colors = new CliColors ();
43
+ $ this ->options = $ options ;
36
44
}
37
45
38
46
/**
39
47
* Render and return a unified diff.
40
48
*
41
49
* @return string Direct Output to the console
50
+ * @throws InvalidArgumentException
42
51
*/
43
52
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
44
82
{
45
83
$ diff = '' ;
46
84
$ opCodes = $ this ->diff ->getGroupedOpCodes ();
@@ -56,7 +94,7 @@ public function render(): string
56
94
$ i2 = -1 ;
57
95
}
58
96
59
- $ diff .= $ this ->colors -> getColoredString (
97
+ $ diff .= $ this ->colorizeString (
60
98
'@@ - ' . ($ i1 + 1 ) . ', ' . ($ i2 - $ i1 ) . ' + ' . ($ j1 + 1 ) . ', ' . ($ j2 - $ j1 ) . " @@ \n" ,
61
99
'purple '
62
100
);
@@ -66,22 +104,22 @@ public function render(): string
66
104
"\n " ,
67
105
$ this ->diff ->getArrayRange ($ this ->diff ->getVersion1 (), $ i1 , $ i2 )
68
106
);
69
- $ diff .= $ this ->colors -> getColoredString (' ' . $ string . "\n" , 'grey ' );
107
+ $ diff .= $ this ->colorizeString (' ' . $ string . "\n" , 'grey ' );
70
108
continue ;
71
109
}
72
110
if ($ tag == 'replace ' || $ tag == 'delete ' ) {
73
111
$ string = implode (
74
112
"\n- " ,
75
113
$ this ->diff ->getArrayRange ($ this ->diff ->getVersion1 (), $ i1 , $ i2 )
76
114
);
77
- $ diff .= $ this ->colors -> getColoredString ('- ' . $ string . "\n" , 'light_red ' );
115
+ $ diff .= $ this ->colorizeString ('- ' . $ string . "\n" , 'light_red ' );
78
116
}
79
117
if ($ tag == 'replace ' || $ tag == 'insert ' ) {
80
118
$ string = implode (
81
119
"\n+ " ,
82
120
$ this ->diff ->getArrayRange ($ this ->diff ->getVersion2 (), $ j1 , $ j2 )
83
121
);
84
- $ diff .= $ this ->colors -> getColoredString ('+ ' . $ string . "\n" , 'light_green ' );
122
+ $ diff .= $ this ->colorizeString ('+ ' . $ string . "\n" , 'light_green ' );
85
123
}
86
124
}
87
125
}
0 commit comments