Skip to content

Commit

Permalink
feat(cli): display diff during dry-run
Browse files Browse the repository at this point in the history
  • Loading branch information
justinrubek committed May 13, 2024
1 parent 6e20c30 commit 247522d
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
33 changes: 33 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ anyhow = "1.0.59"
cargo-lock = "8.0.3"
cargo_metadata = "0.15.3"
cargo_toml = "0.15.2"
console = "0.15.8"
conventional_commit_parser = "0.9.4"
gix = "0.62.0"
memmap = "0.7.0"
Expand Down Expand Up @@ -44,6 +45,10 @@ features = ["serde"]
version = "1.0.140"
features = ["derive"]

[dependencies.similar]
version = "2.5.0"
features = ["inline"]

[lib]
path = "src/lib.rs"
name = "bomper"
Expand Down
55 changes: 53 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use bomper::{
VersionIncrement,
},
};
use std::io::Write;
use console::{style, Style};
use similar::{ChangeTag, TextDiff};
use std::{fmt, io::Write};

pub struct App {
pub args: BaseArgs,
Expand Down Expand Up @@ -85,10 +87,59 @@ impl App {
/// This function is responsible for respecting the `dry_run` flag, so it will only persist changes
/// if the flag is not set.
fn apply_changes(changes: Vec<FileReplacer>, args: &BaseArgs) -> Result<()> {
struct Line(Option<usize>);

impl fmt::Display for Line {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.0 {
None => write!(f, " "),
Some(idx) => write!(f, "{:<4}", idx + 1),
}
}
}

if args.dry_run {
println!("Dry run, not persisting changes");
for replacer in changes {
println!("Would have replaced: {}", replacer.path.display());
let original = std::fs::read_to_string(&replacer.path)?;
let new = std::fs::read_to_string(&replacer.temp_file)?;

println!("{}", style(replacer.path.display()).cyan());
let (_, w) = console::Term::stdout().size();
// write `─` for the width of the terminal
println!("{:-^1$}", style("─").cyan(), w as usize);

let diff = TextDiff::from_lines(&original, &new);
for (idx, group) in diff.grouped_ops(3).iter().enumerate() {
if idx > 0 {
println!("{:-^1$}", "-", 80);
}
for op in group {
for change in diff.iter_inline_changes(op) {
let (sign, s) = match change.tag() {
ChangeTag::Delete => ("-", Style::new().red()),
ChangeTag::Insert => ("+", Style::new().green()),
ChangeTag::Equal => (" ", Style::new().dim()),
};
print!(
"{}{} |{}",
style(Line(change.old_index())).dim(),
style(Line(change.new_index())).dim(),
s.apply_to(sign).bold(),
);
for (emphasized, value) in change.iter_strings_lossy() {
if emphasized {
print!("{}", s.apply_to(value).underlined().on_black());
} else {
print!("{}", s.apply_to(value));
}
}
if change.missing_newline() {
println!();
}
}
}
}
}

return Ok(());
Expand Down

0 comments on commit 247522d

Please sign in to comment.