Skip to content

Commit 4b174c4

Browse files
Copilotswissspidy
andcommitted
Replace wp_text_diff HTML output with CLI-friendly colored diff
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent c1c8cf9 commit 4b174c4

File tree

1 file changed

+78
-21
lines changed

1 file changed

+78
-21
lines changed

src/Post_Revision_Command.php

Lines changed: 78 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -157,30 +157,87 @@ public function diff( $args, $assoc_args ) {
157157
$left_string = $from_revision->{$field};
158158
$right_string = $to_revision->{$field};
159159

160-
// Generate the diff
161-
$diff_args = [
162-
'title_left' => sprintf(
163-
'%s (%s) - ID %d',
164-
$from_revision->post_title,
165-
$from_revision->post_modified,
166-
$from_revision->ID
167-
),
168-
'title_right' => sprintf(
169-
'%s (%s) - ID %d',
170-
$to_revision->post_title,
171-
$to_revision->post_modified,
172-
$to_revision->ID
173-
),
174-
];
175-
176-
$diff = wp_text_diff( $left_string, $right_string, $diff_args );
177-
178-
if ( ! $diff ) {
160+
// Split content into lines for diff
161+
$left_lines = explode( "\n", $left_string );
162+
$right_lines = explode( "\n", $right_string );
163+
164+
// Create Text_Diff object
165+
$text_diff = new \Text_Diff( 'auto', [ $left_lines, $right_lines ] );
166+
167+
// Check if there are any changes
168+
if ( 0 === $text_diff->countAddedLines() && 0 === $text_diff->countDeletedLines() ) {
179169
WP_CLI::success( 'No difference found.' );
180170
return;
181171
}
182172

183-
// Output the diff
184-
WP_CLI::line( $diff );
173+
// Display header
174+
WP_CLI::line(
175+
WP_CLI::colorize(
176+
sprintf(
177+
'%%y--- %s (%s) - ID %d%%n',
178+
$from_revision->post_title,
179+
$from_revision->post_modified,
180+
$from_revision->ID
181+
)
182+
)
183+
);
184+
WP_CLI::line(
185+
WP_CLI::colorize(
186+
sprintf(
187+
'%%y+++ %s (%s) - ID %d%%n',
188+
$to_revision->post_title,
189+
$to_revision->post_modified,
190+
$to_revision->ID
191+
)
192+
)
193+
);
194+
WP_CLI::line( '' );
195+
196+
// Render the diff using CLI-friendly format
197+
$this->render_cli_diff( $text_diff );
198+
}
199+
200+
/**
201+
* Renders a diff in CLI-friendly format with colors.
202+
*
203+
* @param \Text_Diff $diff The diff object to render.
204+
*/
205+
private function render_cli_diff( $diff ) {
206+
$edits = $diff->getDiff();
207+
208+
foreach ( $edits as $edit ) {
209+
switch ( get_class( $edit ) ) {
210+
case 'Text_Diff_Op_copy':
211+
// Unchanged lines - show in default color
212+
foreach ( $edit->orig as $line ) {
213+
WP_CLI::line( ' ' . $line );
214+
}
215+
break;
216+
217+
case 'Text_Diff_Op_add':
218+
// Added lines - show in green
219+
foreach ( $edit->final as $line ) {
220+
WP_CLI::line( WP_CLI::colorize( '%g+ ' . $line . '%n' ) );
221+
}
222+
break;
223+
224+
case 'Text_Diff_Op_delete':
225+
// Deleted lines - show in red
226+
foreach ( $edit->orig as $line ) {
227+
WP_CLI::line( WP_CLI::colorize( '%r- ' . $line . '%n' ) );
228+
}
229+
break;
230+
231+
case 'Text_Diff_Op_change':
232+
// Changed lines - show deletions in red, additions in green
233+
foreach ( $edit->orig as $line ) {
234+
WP_CLI::line( WP_CLI::colorize( '%r- ' . $line . '%n' ) );
235+
}
236+
foreach ( $edit->final as $line ) {
237+
WP_CLI::line( WP_CLI::colorize( '%g+ ' . $line . '%n' ) );
238+
}
239+
break;
240+
}
241+
}
185242
}
186243
}

0 commit comments

Comments
 (0)