summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWilfred Hughes <me@wilfred.me.uk>2024-02-04 22:52:02 -0800
committerWilfred Hughes <me@wilfred.me.uk>2024-02-08 08:48:56 -0800
commit16fcdbc905f855dd387034465d1c21b459126a43 (patch)
treea382f84d57dbec2e4bb24dc874ebf7fed1edca84
parent7d0d0113aa3faa3953737d7e2b24cb97191e5de1 (diff)
Basic mock
-rw-r--r--src/display/patch.rs88
-rw-r--r--src/main.rs13
2 files changed, 98 insertions, 3 deletions
diff --git a/src/display/patch.rs b/src/display/patch.rs
new file mode 100644
index 000000000..0101db178
--- /dev/null
+++ b/src/display/patch.rs
@@ -0,0 +1,88 @@
+use owo_colors::OwoColorize as _;
+
+use crate::constants::Side;
+use crate::diff::myers_diff;
+use crate::display::style::apply_colors;
+use crate::line_parser::split_lines_keep_newline;
+use crate::options::DisplayOptions;
+use crate::parse::syntax::MatchedPos;
+use crate::summary::FileFormat;
+
+pub(crate) fn print(
+ display_options: &DisplayOptions,
+ file_format: &FileFormat,
+ lhs_src: &str,
+ rhs_src: &str,
+ lhs_mps: &[MatchedPos],
+ rhs_mps: &[MatchedPos],
+) {
+ println!("{}", "--- foo.el".bright_yellow().bold());
+ println!("{}", "+++ foo.el".bright_yellow().bold());
+
+ let (lhs_colored_lines, rhs_colored_lines) = if display_options.use_color {
+ (
+ apply_colors(
+ lhs_src,
+ Side::Left,
+ display_options.syntax_highlight,
+ file_format,
+ display_options.background_color,
+ lhs_mps,
+ ),
+ apply_colors(
+ rhs_src,
+ Side::Right,
+ display_options.syntax_highlight,
+ file_format,
+ display_options.background_color,
+ rhs_mps,
+ ),
+ )
+ } else {
+ (
+ lhs_src.lines().map(|s| format!("{}\n", s)).collect(),
+ rhs_src.lines().map(|s| format!("{}\n", s)).collect(),
+ )
+ };
+
+ let lhs_lines = split_lines_keep_newline(lhs_src);
+ let rhs_lines = split_lines_keep_newline(rhs_src);
+
+ let mut lhs_i = 0;
+ let mut rhs_i = 0;
+
+ let mut lhs_i_last_printed = 0;
+
+ for diff_res in myers_diff::slice_unique_by_hash(&lhs_lines, &rhs_lines) {
+ match diff_res {
+ myers_diff::DiffResult::Left(_lhs_line) => {
+ println!("{}", "@@ -1,2 +3,4 @@".dimmed());
+
+ if lhs_i_last_printed < lhs_i - 3 {
+ print!(" {}", lhs_colored_lines[lhs_i - 3]);
+ print!(" {}", lhs_colored_lines[lhs_i - 2]);
+ print!(" {}", lhs_colored_lines[lhs_i - 1]);
+ }
+ lhs_i_last_printed = lhs_i;
+
+ print!("{} {}", "-".dimmed(), lhs_colored_lines[lhs_i]);
+ lhs_i += 1;
+ }
+ myers_diff::DiffResult::Both(_lhs_line, _rhs_line) => {
+ lhs_i += 1;
+ rhs_i += 1;
+ }
+ myers_diff::DiffResult::Right(_rhs_line) => {
+ print!("{} {}", "+".dimmed(), rhs_colored_lines[rhs_i]);
+
+ rhs_i += 1;
+
+ if rhs_i != 43 && rhs_i != 44 {
+ print!(" {}", rhs_colored_lines[rhs_i + 1]);
+ print!(" {}", rhs_colored_lines[rhs_i + 2]);
+ print!(" {}", rhs_colored_lines[rhs_i + 3]);
+ }
+ }
+ }
+ }
+}
diff --git a/src/main.rs b/src/main.rs
index 8eb24a833..1747525ac 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -310,11 +310,11 @@ fn main() {
match display_options.display_mode {
DisplayMode::Inline
| DisplayMode::SideBySide
- | DisplayMode::SideBySideShowBoth => {
+ | DisplayMode::SideBySideShowBoth
+ | DisplayMode::Patch => {
print_diff_result(&display_options, &diff_result);
}
DisplayMode::Json => display::json::print(&diff_result),
- DisplayMode::Patch => display::patch::print(&diff_result),
}
}
}
@@ -882,7 +882,14 @@ fn print_diff_result(display_options: &DisplayOptions, summary: &DiffResult) {
);
}
DisplayMode::Json => unreachable!(),
- DisplayMode::Patch => todo!(),
+ DisplayMode::Patch => display::patch::print(
+ display_options,
+ &summary.file_format,
+ lhs_src,
+ rhs_src,
+ &summary.lhs_positions,
+ &summary.rhs_positions,
+ ),
}
}
(FileContent::Binary, FileContent::Binary) => {