Skip to content

Commit

Permalink
Push a formatter instance around
Browse files Browse the repository at this point in the history
Not all the way yet, but this is at least part of the way.
  • Loading branch information
walles committed Oct 21, 2024
1 parent ca08235 commit 8e222fe
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 26 deletions.
3 changes: 2 additions & 1 deletion src/plusminus_lines_highlighter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use threadpool::ThreadPool;
use crate::lines_highlighter::{LineAcceptance, LinesHighlighter, Response};
use crate::refiner::Formatter;
use crate::string_future::StringFuture;
use crate::token_collector::{LINE_STYLE_NEW, LINE_STYLE_OLD};

#[derive(Debug)]
pub(crate) struct PlusMinusLinesHighlighter {
Expand Down Expand Up @@ -126,7 +127,7 @@ impl PlusMinusLinesHighlighter {
texts: vec![line.to_string() + "\n"],
prefixes: vec![prefix.to_string()],
last_seen_prefix: Some(prefix.to_string()),
formatter: Formatter {},
formatter: Formatter::new(LINE_STYLE_OLD, LINE_STYLE_NEW),
});
}

Expand Down
72 changes: 51 additions & 21 deletions src/refiner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,19 @@ use crate::token_collector::*;
use crate::tokenizer;

#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct Formatter {}
pub(crate) struct Formatter {
line_style_old: LineStyle,
line_style_new: LineStyle,
}

impl Formatter {
pub(crate) fn new(line_style_old: LineStyle, line_style_new: LineStyle) -> Self {
Formatter {
line_style_old,
line_style_new,
}
}

/// Format old and new lines in OLD and NEW colors.
///
/// No intra-line refinement.
Expand All @@ -18,9 +28,9 @@ impl Formatter {

for (prefix, prefix_text) in prefixes.iter().zip(prefix_texts.iter()) {
let line_style = if prefix.contains('+') {
LINE_STYLE_NEW
&self.line_style_new
} else {
LINE_STYLE_OLD
&self.line_style_old
};

// If the user adds a section with a missing trailing newline, we want
Expand All @@ -34,7 +44,7 @@ impl Formatter {
let last_line = pos == last_pos;

let to_push = render_row(
&line_style,
line_style,
prefix,
&[StyledToken::new(
line.to_string(),
Expand Down Expand Up @@ -326,25 +336,51 @@ fn to_lines(text: &str) -> Vec<String> {

#[cfg(test)]
mod tests {
use crate::ansi::Color::Green;
use crate::ansi::Color::Red;
use crate::ansi::Color::Yellow;
use crate::ansi::ANSI_STYLE_NORMAL;

use super::*;

#[cfg(test)]
use pretty_assertions::assert_eq;

const LINE_STYLE_OLD: LineStyle = {
LineStyle {
prefix_style: ANSI_STYLE_NORMAL.with_color(Red),
unchanged_style: ANSI_STYLE_NORMAL.with_color(Yellow),
midlighted_style: ANSI_STYLE_NORMAL.with_color(Red),
highlighted_style: ANSI_STYLE_NORMAL.with_color(Red).with_inverse(true),
}
};

const LINE_STYLE_NEW: LineStyle = {
LineStyle {
prefix_style: ANSI_STYLE_NORMAL.with_color(Green),
unchanged_style: ANSI_STYLE_NORMAL.with_color(Yellow),
midlighted_style: ANSI_STYLE_NORMAL.with_color(Green),
highlighted_style: ANSI_STYLE_NORMAL.with_color(Green).with_inverse(true),
}
};

const FORMATTER: Formatter = Formatter {
line_style_old: LINE_STYLE_OLD,
line_style_new: LINE_STYLE_NEW,
};

#[test]
fn test_simple_format_adds_and_removes() {
let formatter = Formatter {};

let empty: Vec<String> = Vec::new();
assert_eq!(formatter.format_simple(&[], &[]), empty);
assert_eq!(FORMATTER.format_simple(&[], &[]), empty);

// Test adds-only
assert_eq!(
formatter.format_simple(&["+"], &["a\n"]),
FORMATTER.format_simple(&["+"], &["a\n"]),
["".to_string() + GREEN + "+a" + NORMAL]
);
assert_eq!(
formatter.format_simple(&["+"], &["a\nb\n"]),
FORMATTER.format_simple(&["+"], &["a\nb\n"]),
[
"".to_string() + GREEN + "+a" + NORMAL,
"".to_string() + GREEN + "+b" + NORMAL,
Expand All @@ -353,11 +389,11 @@ mod tests {

// Test removes-only
assert_eq!(
formatter.format_simple(&["-"], &["a\n"]),
FORMATTER.format_simple(&["-"], &["a\n"]),
["".to_string() + OLD + "-a" + NORMAL]
);
assert_eq!(
formatter.format_simple(&["-"], &["a\nb\n"]),
FORMATTER.format_simple(&["-"], &["a\nb\n"]),
[
"".to_string() + OLD + "-a" + NORMAL,
"".to_string() + OLD + "-b" + NORMAL,
Expand All @@ -369,8 +405,6 @@ mod tests {
/// hangs, that's probably what happened again.
#[test]
fn test_format_simple_complexity() {
let formatter = Formatter {};

// Values from whan this file was added in a single commit:
// https://github.com/walles/moar/blob/59270d6f8cf454f7a79fcde36a7fcf794768ced9/sample-files/large-git-log-patch.txt
let lines = 300_000;
Expand All @@ -384,15 +418,13 @@ mod tests {
let prefixes = vec!["+"];
let texts = vec![text.as_str()];

let result = formatter.format_simple(&prefixes, &texts);
let result = FORMATTER.format_simple(&prefixes, &texts);
assert_eq!(text.lines().count(), result.len());
}

#[test]
fn test_quote_change() {
let formatter = Formatter {};

let result = formatter.format(
let result = FORMATTER.format(
&["-", "+"],
&[
"<unchanged text between quotes>\n",
Expand All @@ -414,12 +446,10 @@ mod tests {

#[test]
fn test_almost_empty_changes() {
let formatter = Formatter {};

let result = formatter.format(&["-"], &["x\n"]);
let result = FORMATTER.format(&["-"], &["x\n"]);
assert_eq!(result, [format!("{OLD}-x{NORMAL}"),]);

let result = formatter.format(&["+"], &["x\n"]);
let result = FORMATTER.format(&["+"], &["x\n"]);
assert_eq!(result, [format!("{GREEN}+x{NORMAL}"),]);
}

Expand Down
8 changes: 4 additions & 4 deletions src/token_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ pub(crate) struct StyledToken {
pub(crate) style: Style,
}

#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct LineStyle {
prefix_style: AnsiStyle,
unchanged_style: AnsiStyle,
pub(crate) prefix_style: AnsiStyle,
pub(crate) unchanged_style: AnsiStyle,
pub(crate) midlighted_style: AnsiStyle,
highlighted_style: AnsiStyle,
pub(crate) highlighted_style: AnsiStyle,
}

pub(crate) const LINE_STYLE_OLD: LineStyle = {
Expand Down

0 comments on commit 8e222fe

Please sign in to comment.