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

avoid the display issue with git add -p without losing CR #1000

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 0 additions & 11 deletions src/delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,17 +182,6 @@ impl<'a> StateMachine<'a> {
fn ingest_line(&mut self, raw_line_bytes: &[u8]) {
// TODO: retain raw_line as Cow
self.raw_line = String::from_utf8_lossy(raw_line_bytes).to_string();
// When a file has \r\n line endings, git sometimes adds ANSI escape sequences between the
// \r and \n, in which case byte_lines does not remove the \r. Remove it now.
if let Some(cr_index) = self.raw_line.rfind('\r') {
if ansi::strip_ansi_codes(&self.raw_line[cr_index + 1..]).is_empty() {
self.raw_line = format!(
"{}{}",
&self.raw_line[..cr_index],
&self.raw_line[cr_index + 1..]
);
}
}
if self.config.max_line_length > 0
&& self.raw_line.len() > self.config.max_line_length
// Do not truncate long hunk headers
Expand Down
13 changes: 13 additions & 0 deletions src/paint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,19 @@ impl<'p> Painter<'p> {
painted_prefix(state.clone(), config),
config,
);
// Replace control characters in output with caret notation.
for n in 0..32 {
// HT(0x09), LF(0x0a), ESC(0x1b) are skipped
if n != 0x09 && n != 0x0a && n != 0x1b {
line = line.replace(
&char::from_u32(n).unwrap().to_string(),
&format!(
"\x1b[7m^{}\x1b[27m",
&char::from_u32(n + 64).unwrap().to_string()
),
);
}
}
let (bg_fill_mode, fill_style) =
Painter::get_should_right_fill_background_color_and_fill_style(
diff_sections,
Expand Down
3 changes: 2 additions & 1 deletion src/tests/test_example_diffs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,14 @@ commit 94907c0f136f46dc46ffae2dc92dca9af7eb7c2e
}

#[test]
fn test_orphan_carriage_return_is_stripped() {
fn test_carriage_return_is_replaced_with_caret_notation() {
let config = integration_test_utils::make_config_from_args(&[]);
let output = integration_test_utils::run_delta(
GIT_DIFF_SINGLE_HUNK_WITH_SEQUENCE_OF_CR_ESCAPE_SEQUENCES_LF,
&config,
);
assert!(output.bytes().all(|b: u8| b != b'\r'));
assert!(output.contains("\x1b[7m^M\x1b[27m"));
}

#[test]
Expand Down