Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into johan/half-line-hig…
Browse files Browse the repository at this point in the history
…hlights
  • Loading branch information
walles committed Oct 19, 2024
2 parents 0c04848 + be9a880 commit 56b3bf9
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 30 deletions.
5 changes: 3 additions & 2 deletions src/ansi.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::constants::{
BOLD, FAINT, GREEN, INVERSE_VIDEO, NORMAL, NORMAL_INTENSITY, NO_INVERSE_VIDEO, RED, YELLOW,
BOLD, DEFAULT_COLOR, FAINT, GREEN, INVERSE_VIDEO, NORMAL, NORMAL_INTENSITY, NO_INVERSE_VIDEO,
RED, YELLOW,
};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -69,7 +70,7 @@ impl AnsiStyle {

if self.color != before.color {
match self.color {
Color::Default => return_me.push_str("\x1b[39m"),
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
2 changes: 1 addition & 1 deletion src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub const OLD: &str = "\x1b[31m"; // Red
pub const NEW: &str = "\x1b[32m"; // Green
pub const PARSE_ERROR: &str = "\x1b[33m\x1b[7m"; // Inverse yellow

pub const INVERSE_VIDEO: &str = "\x1b[7m";
Expand All @@ -11,6 +10,7 @@ pub const BOLD: &str = "\x1b[1m";
pub const FAINT: &str = "\x1b[2m";
pub const NORMAL_INTENSITY: &str = "\x1b[22m"; // Neither bold nor faint

pub const DEFAULT_COLOR: &str = "\x1b[39m";
pub const YELLOW: &str = "\x1b[33m";
pub const GREEN: &str = "\x1b[32m";
pub const CYAN: &str = "\x1b[36m";
Expand Down
2 changes: 1 addition & 1 deletion src/hunk_highlighter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ mod tests {
assert_eq!(result.highlighted.len(), 1);
assert_eq!(
result.highlighted[0].get(),
"\u{1b}[32m+No trailing newline\u{1b}[31m\u{1b}[7m⏎\u{1b}[0m\n\u{1b}[2m\\ No newline at end of file\u{1b}[0m\n",
"\u{1b}[32m+No trailing newline\u{1b}[0m\u{1b}[31m\u{1b}[7m⏎\u{1b}[0m\n\u{1b}[2m\\ No newline at end of file\u{1b}[0m\n",
);

assert!(!test_me.more_lines_expected());
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ mod tests {
use pretty_assertions::assert_eq;

fn new(text: &str) -> String {
return format!("{NEW}{text}{NORMAL}");
return format!("{GREEN}{text}{NORMAL}");
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/plusminus_header_highlighter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ mod tests {
format!(
"\
{BOLD}--- {INVERSE_VIDEO}{NORMAL_INTENSITY}{OLD}x{NOT_INVERSE_VIDEO}{BOLD}{DEFAULT_COLOR}.txt{NORMAL}\n\
{BOLD}+++ {INVERSE_VIDEO}{NORMAL_INTENSITY}{NEW}y{NOT_INVERSE_VIDEO}{BOLD}{DEFAULT_COLOR}.txt{NORMAL}\n"
{BOLD}+++ {INVERSE_VIDEO}{NORMAL_INTENSITY}{GREEN}y{NOT_INVERSE_VIDEO}{BOLD}{DEFAULT_COLOR}.txt{NORMAL}\n"
),
highlighted
);
Expand Down
2 changes: 1 addition & 1 deletion src/plusminus_lines_highlighter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ mod tests {
assert_eq!(result.highlighted.len(), 1);
assert_eq!(
result.highlighted[0].get(),
"\u{1b}[32m+No trailing newline\u{1b}[31m\u{1b}[7m⏎\u{1b}[0m\n\u{1b}[2m\\ No newline at end of file\u{1b}[0m\n",
"\u{1b}[32m+No trailing newline\u{1b}[0m\u{1b}[31m\u{1b}[7m⏎\u{1b}[0m\n\u{1b}[2m\\ No newline at end of file\u{1b}[0m\n",
);
}
}
39 changes: 21 additions & 18 deletions src/refiner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@ use crate::line_collector::NO_EOF_NEWLINE_MARKER_HOLDER;
use crate::token_collector::*;
use crate::tokenizer;

/// Like format!(), but faster for our special case
fn format_simple_line(old_new: &str, plus_minus: &str, contents: &str) -> String {
let mut line = String::with_capacity(old_new.len() + 1 + contents.len() + NORMAL.len());
line.push_str(old_new);
line.push_str(plus_minus);
line.push_str(contents);
line.push_str(NORMAL);
return line;
}

/// Format old and new lines in OLD and NEW colors.
///
/// No intra-line refinement.
Expand All @@ -23,7 +13,11 @@ fn format_simple(prefixes: &[&str], prefix_texts: &[&str]) -> Vec<String> {
let mut lines: Vec<String> = Vec::new();

for (prefix, prefix_text) in prefixes.iter().zip(prefix_texts.iter()) {
let old_new = if prefix.contains('+') { NEW } else { OLD };
let line_style = if prefix.contains('+') {
LINE_STYLE_NEW
} else {
LINE_STYLE_OLD
};

// If the user adds a section with a missing trailing newline, we want
// to draw a highlighted-in-red newline symbol at the end of the last
Expand All @@ -34,10 +28,19 @@ fn format_simple(prefixes: &[&str], prefix_texts: &[&str]) -> Vec<String> {
for (pos, line) in prefix_text.lines().enumerate() {
let last_line = pos == last_pos;

let to_push = render_row(
&line_style,
prefix,
&[StyledToken::new(
line.to_string(),
Style::DiffPartMidlighted,
)],
false,
);
if last_line && draw_missing_trailing_newline {
lines.push(format!("{NEW}+{line}{OLD}{INVERSE_VIDEO}⏎{NORMAL}"));
lines.push(format!("{to_push}{OLD}{INVERSE_VIDEO}⏎{NORMAL}"));
} else {
lines.push(format_simple_line(old_new, prefix, line));
lines.push(to_push);
}
}

Expand Down Expand Up @@ -329,13 +332,13 @@ mod tests {
// Test adds-only
assert_eq!(
format_simple(&["+"], &["a\n"]),
["".to_string() + NEW + "+a" + NORMAL]
["".to_string() + GREEN + "+a" + NORMAL]
);
assert_eq!(
format_simple(&["+"], &["a\nb\n"]),
[
"".to_string() + NEW + "+a" + NORMAL,
"".to_string() + NEW + "+b" + NORMAL,
"".to_string() + GREEN + "+a" + NORMAL,
"".to_string() + GREEN + "+b" + NORMAL,
]
);

Expand Down Expand Up @@ -390,7 +393,7 @@ mod tests {
"{OLD}-{INVERSE_VIDEO}<{NO_INVERSE_VIDEO}{YELLOW}unchanged text between quotes{INVERSE_VIDEO}{OLD}>{NORMAL}"
),
format!(
"{NEW}+{INVERSE_VIDEO}[{NO_INVERSE_VIDEO}{YELLOW}unchanged text between quotes{INVERSE_VIDEO}{NEW}]{NORMAL}"
"{GREEN}+{INVERSE_VIDEO}[{NO_INVERSE_VIDEO}{YELLOW}unchanged text between quotes{INVERSE_VIDEO}{GREEN}]{NORMAL}"
),
]
)
Expand All @@ -402,7 +405,7 @@ mod tests {
assert_eq!(result, [format!("{OLD}-x{NORMAL}"),]);

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

#[test]
Expand Down
8 changes: 4 additions & 4 deletions src/token_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub(crate) struct StyledToken {
pub(crate) struct LineStyle {
prefix_style: AnsiStyle,
unchanged_style: AnsiStyle,
midlighted_style: AnsiStyle,
pub(crate) midlighted_style: AnsiStyle,
highlighted_style: AnsiStyle,
}

Expand Down Expand Up @@ -107,7 +107,7 @@ impl StyledToken {
}

#[must_use]
fn render_row(
pub(crate) fn render_row(
line_style: &LineStyle,
prefix: &str,
row: &[StyledToken],
Expand Down Expand Up @@ -392,7 +392,7 @@ pub fn brighten_filename(row: &mut [StyledToken]) {
#[cfg(test)]
mod tests {
use super::*;
use crate::constants::NEW;
use crate::constants::GREEN;
use crate::constants::NORMAL;
use crate::constants::OLD;

Expand Down Expand Up @@ -440,7 +440,7 @@ mod tests {
},
],
);
assert_eq!(rendered, format!("{NEW}+hej{NORMAL}\n"));
assert_eq!(rendered, format!("{GREEN}+hej{NORMAL}\n"));
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion testdata/add-remove-trailing-newline.riff-output
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
[36m@@ -1,1 +1,3 @@[0m
Hello
[32m+[0m
[32m+No trailing newline[31m[7m⏎[0m
[32m+No trailing newline[0m[31m[7m⏎[0m
[2m\ No newline at end of file[0m

0 comments on commit 56b3bf9

Please sign in to comment.