Skip to content

Commit

Permalink
Merge branch 'johan/half-line-highlights'
Browse files Browse the repository at this point in the history
  • Loading branch information
walles committed Oct 22, 2024
2 parents be9a880 + 2e648c3 commit 30b6018
Show file tree
Hide file tree
Showing 28 changed files with 425 additions and 395 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Riff is a wrapper around `diff` that highlights which parts of lines have change

![Screenshot of riff in action](screenshot.png 'git show')

Unchanged parts of changed lines are shown in yellow.

`riff` also [helpfully highlights conflicts and merge commits](#more-features).

Much like `git`, Riff sends its output to a pager, trying these in order:
Expand Down
Binary file modified screenshot-diff2-conflict.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified screenshot-git-merge.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion src/ansi.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use crate::constants::{
BOLD, DEFAULT_COLOR, FAINT, GREEN, INVERSE_VIDEO, NORMAL, NORMAL_INTENSITY, NO_INVERSE_VIDEO,
RED,
RED, YELLOW,
};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Color {
Default,
Red,
Green,
Yellow,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -72,6 +73,7 @@ impl AnsiStyle {
Color::Default => return_me.push_str(DEFAULT_COLOR),
Color::Red => return_me.push_str(RED),
Color::Green => return_me.push_str(GREEN),
Color::Yellow => return_me.push_str(YELLOW),
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/conflicts_highlighter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ impl ConflictsHighlighter {
move || {
let c1_or_newline = if c1.is_empty() { "\n" } else { &c1 };
let c2_or_newline = if c2.is_empty() { "\n" } else { &c2 };
let (c1_tokens, c2_tokens, _, _) =
refiner::to_highlighted_tokens(c1_or_newline, c2_or_newline, true);
let (c1_tokens, c2_tokens) =
refiner::to_highlighted_tokens(c1_or_newline, c2_or_newline);

let c1_style = if base_header.is_empty() {
LINE_STYLE_CONFLICT_OLD
Expand Down Expand Up @@ -270,8 +270,8 @@ impl ConflictsHighlighter {
let base_or_newline = if base.is_empty() { "\n" } else { &base };

let c1_or_newline = if c1.is_empty() { "\n" } else { &c1 };
let (mut base_vs_c1_tokens, c1_tokens, _, _) =
refiner::to_highlighted_tokens(base_or_newline, c1_or_newline, true);
let (mut base_vs_c1_tokens, c1_tokens) =
refiner::to_highlighted_tokens(base_or_newline, c1_or_newline);
if c1.is_empty() {
// In the base, show only diffs vs c2
base_vs_c1_tokens.iter_mut().for_each(|token| {
Expand All @@ -282,8 +282,8 @@ impl ConflictsHighlighter {
token_collector::render(&LINE_STYLE_CONFLICT_NEW, c1_prefix, &c1_tokens);

let c2_or_newline = if c2.is_empty() { "\n" } else { &c2 };
let (mut base_vs_c2_tokens, c2_tokens, _, _) =
refiner::to_highlighted_tokens(base_or_newline, c2_or_newline, true);
let (mut base_vs_c2_tokens, c2_tokens) =
refiner::to_highlighted_tokens(base_or_newline, c2_or_newline);
if c2.is_empty() {
// In the base, show only diffs vs c1
base_vs_c2_tokens.iter_mut().for_each(|token| {
Expand Down
24 changes: 16 additions & 8 deletions src/hunk_highlighter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::constants::NO_EOF_NEWLINE_COLOR;
use crate::hunk_header::HunkHeader;
use crate::lines_highlighter::{LineAcceptance, LinesHighlighter, Response};
use crate::plusminus_lines_highlighter::PlusMinusLinesHighlighter;
use crate::refiner::Formatter;
use crate::string_future::StringFuture;

#[derive(Debug)]
Expand All @@ -20,6 +21,8 @@ pub(crate) struct HunkLinesHighlighter {

/// We'll count these values down as we consume lines.
remaining_line_counts: Vec<usize>,

formatter: Formatter,
}

impl LinesHighlighter for HunkLinesHighlighter {
Expand Down Expand Up @@ -86,7 +89,7 @@ impl HunkLinesHighlighter {
/// Create a new LinesHighlighter from a line of input.
///
/// Returns None if this line doesn't start a new LinesHighlighter.
pub(crate) fn from_line(line: &str) -> Option<Self>
pub(crate) fn from_line(line: &str, formatter: Formatter) -> Option<Self>
where
Self: Sized,
{
Expand All @@ -96,6 +99,7 @@ impl HunkLinesHighlighter {
remaining_line_counts: hunk_header.linecounts.clone(),
initial_line_counts: hunk_header.linecounts,
lines_highlighter: None,
formatter,
});
}

Expand Down Expand Up @@ -139,7 +143,9 @@ impl HunkLinesHighlighter {
return Ok(return_me);
}
}
if let Some(highlighter) = PlusMinusLinesHighlighter::from_line(line, prefix_length) {
if let Some(highlighter) =
PlusMinusLinesHighlighter::from_line(line, prefix_length, self.formatter)
{
self.lines_highlighter = Some(Box::new(highlighter));
return Ok(return_me);
}
Expand Down Expand Up @@ -270,6 +276,7 @@ impl HunkLinesHighlighter {

#[cfg(test)]
mod tests {
use crate::refiner::tests::FORMATTER;
use crate::{line_collector::NO_EOF_NEWLINE_MARKER_HOLDER, lines_highlighter::LineAcceptance};

use super::*;
Expand All @@ -279,7 +286,7 @@ mod tests {
fn test_happy_path() {
let thread_pool = ThreadPool::new(1);

let mut test_me = HunkLinesHighlighter::from_line("@@ -1,2 +1,2 @@").unwrap();
let mut test_me = HunkLinesHighlighter::from_line("@@ -1,2 +1,2 @@", FORMATTER).unwrap();

// First call to consume_line() should get us the hunk header
let mut result = test_me
Expand Down Expand Up @@ -308,8 +315,8 @@ mod tests {
assert_eq!(
result.highlighted[0].get(),
concat!(
"\u{1b}[2m\u{1b}[31m-Hello, my name is Johan\u{1b}[0m\n",
"\u{1b}[2m\u{1b}[32m+\u{1b}[0mHello, my \u{1b}[7m\u{1b}[32mfirst \u{1b}[0mname is Johan\n"
"\u{1b}[31m-\u{1b}[33mHello, my name is Johan\u{1b}[0m\n",
"\u{1b}[32m+\u{1b}[33mHello, my \u{1b}[7m\u{1b}[32mfirst \u{1b}[27m\u{1b}[33mname is Johan\u{1b}[0m\n"
)
);
assert_eq!(result.highlighted[1].get(), " I like pie.\n");
Expand All @@ -320,7 +327,7 @@ mod tests {

#[test]
fn test_decrease_remaining_line_count() {
let mut test_me = HunkLinesHighlighter::from_line("@@ -1,2 +1,2 @@").unwrap();
let mut test_me = HunkLinesHighlighter::from_line("@@ -1,2 +1,2 @@", FORMATTER).unwrap();
assert_eq!(test_me.remaining_line_counts, vec![2, 2]);

test_me.decrease_remaining_line_counts("+").unwrap();
Expand All @@ -347,7 +354,7 @@ mod tests {
*no_eof_newline_marker = Some("\\ No newline at end of file".to_string());
}

let mut test_me = HunkLinesHighlighter::from_line("@@ -1,1 +1,2 @@").unwrap();
let mut test_me = HunkLinesHighlighter::from_line("@@ -1,1 +1,2 @@", FORMATTER).unwrap();
assert_eq!(test_me.remaining_line_counts, vec![1, 2]);

let thread_pool = ThreadPool::new(1);
Expand Down Expand Up @@ -380,7 +387,8 @@ mod tests {

#[test]
fn test_decrease_remaining_line_count_merge() {
let mut test_me = HunkLinesHighlighter::from_line("@@@ -1,5 -1,5 +1,5 @@@").unwrap();
let mut test_me =
HunkLinesHighlighter::from_line("@@@ -1,5 -1,5 +1,5 @@@", FORMATTER).unwrap();
assert_eq!(test_me.remaining_line_counts, vec![5, 5, 5]);

test_me.decrease_remaining_line_counts(" -").unwrap();
Expand Down
13 changes: 11 additions & 2 deletions src/line_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::hunk_highlighter::HunkLinesHighlighter;
use crate::io::ErrorKind;
use crate::lines_highlighter::{LineAcceptance, LinesHighlighter};
use crate::plusminus_header_highlighter::PlusMinusHeaderHighlighter;
use crate::refiner::Formatter;
use std::io::{self, BufWriter, Write};
use std::process::{self, exit};
use std::sync::mpsc::{sync_channel, Receiver, SyncSender};
Expand Down Expand Up @@ -101,6 +102,8 @@ pub(crate) struct LineCollector {
// returns a string and another that does a background computation first.
// But I failed to figure out how when I tried, more Googling needed!
print_queue_putter: SyncSender<StringFuture>,

formatter: Formatter,
}

impl Drop for LineCollector {
Expand Down Expand Up @@ -138,7 +141,11 @@ impl Drop for LineCollector {
}

impl LineCollector {
pub fn new<W: io::Write + Send + 'static>(output: W, color: bool) -> LineCollector {
pub fn new<W: io::Write + Send + 'static>(
output: W,
color: bool,
formatter: Formatter,
) -> LineCollector {
// This is how many entries we can look ahead. An "entry" in this case
// being either a plain text section or an oldnew section.
//
Expand Down Expand Up @@ -181,6 +188,8 @@ impl LineCollector {
consumer_thread: Some(consumer),
thread_pool: ThreadPool::new(num_cpus::get()),
print_queue_putter: queue_putter,

formatter,
};
}

Expand Down Expand Up @@ -283,7 +292,7 @@ impl LineCollector {
}
}

if let Some(hunk_highlighter) = HunkLinesHighlighter::from_line(&line) {
if let Some(hunk_highlighter) = HunkLinesHighlighter::from_line(&line, self.formatter) {
self.drain_plain();
self.lines_highlighter = Some(Box::new(hunk_highlighter));
return Ok(());
Expand Down
Loading

0 comments on commit 30b6018

Please sign in to comment.