Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: color hunks using delta #88

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ criterion = "0.5.1"
insta = "1.36.1"

[dependencies]
ansi-to-tui = "4.0.1"
chrono = "0.4.34"
clap = { version = "4.5.3", features = ["derive"] }
crossterm = "0.27.0"
Expand Down
108 changes: 40 additions & 68 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use similar::TextDiff;
use std::borrow::Cow;
use std::iter;
use std::path::PathBuf;
use std::process::Command;
use std::process::Stdio;
use std::rc::Rc;

#[derive(Default, Clone, Debug)]
Expand Down Expand Up @@ -88,79 +90,49 @@ fn create_hunk_items(config: Rc<Config>, hunk: &Hunk, depth: usize) -> impl Iter
.chain(format_diff_hunk_items(&config, depth + 1, hunk))
}

fn format_diff_hunk_items(
config: &Config,
depth: usize,
hunk: &Hunk,
) -> impl Iterator<Item = Item> {
let old = hunk.old_content();
let new = hunk.new_content();

let diff = TextDiff::configure()
.algorithm(Algorithm::Patience)
.diff_lines(&old, &new);

let changes = diff
.ops()
.iter()
.flat_map(|op| diff.iter_inline_changes(op))
.collect::<Vec<_>>();

format_changes(config, &changes)
.into_iter()
.map(move |line| Item {
display: line,
fn format_diff_hunk_items(config: &Config, depth: usize, hunk: &Hunk) -> Vec<Item> {
let mut child = Command::new("delta")
.arg("--color-only")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("couldn't spawn delta");

use std::io::Write;
let child_stdin = child.stdin.as_mut().unwrap();

child_stdin
.write_all(hunk.header.as_bytes())
.expect("couldn't write input to delta");

child_stdin
.write_all(hunk.content.as_bytes())
.expect("couldn't write input to delta");

let out = String::from_utf8(
child
.wait_with_output()
.expect("awaiting delta failed")
.stdout,
)
.expect("Error turning command output to String");

// TODO ansi is output by delta, but ansi_to_tui fails?

let colored_text = ansi_to_tui::IntoText::into_text(&out).expect("couldn't parse ansi text");

hunk.content
.lines()
.zip(&colored_text.lines[1..])
.map(move |(line, colored_line)| Item {
display: colored_line.clone(),
unselectable: true,
depth,
target_data: None,
..Default::default()
})
}

fn format_changes(
config: &Config,
changes: &[similar::InlineChange<'_, str>],
) -> Vec<Line<'static>> {
let style = &config.style;
let lines = changes
.iter()
.map(|change| {
let line_style = match change.tag() {
ChangeTag::Equal => Style::new(),
ChangeTag::Delete => (&style.line_removed).into(),
ChangeTag::Insert => (&style.line_added).into(),
};

let prefix = match change.tag() {
ChangeTag::Equal => " ",
ChangeTag::Delete => "-",
ChangeTag::Insert => "+",
};

let some_emph = change.iter_strings_lossy().any(|(emph, _value)| emph);

Line::from(
iter::once(Span::styled(prefix, line_style))
.chain(change.iter_strings_lossy().map(|(emph, value)| {
Span::styled(
value.to_string(),
if some_emph {
if emph {
line_style.patch(&style.line_highlight.changed)
} else {
line_style.patch(&style.line_highlight.unchanged)
}
} else {
line_style
},
)
}))
.collect::<Vec<_>>(),
)
})
.collect::<Vec<_>>();

lines
.collect()
}

pub(crate) fn log(
Expand Down
Loading