Skip to content

Commit 69afc12

Browse files
author
Stephan Dilly
committed
fix line ending drawing CRLF
(fixes #21)
1 parent 1b90436 commit 69afc12

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

src/components/diff.rs

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -226,19 +226,15 @@ impl DiffComponent {
226226
.bg(if selected { select_color } else { Color::Reset })
227227
.modifier(Modifier::BOLD);
228228

229+
let trimmed =
230+
line.content.trim_matches(|c| c == '\n' || c == '\r');
231+
229232
let filled = if selected {
230233
// selected line
231-
format!(
232-
"{:w$}\n",
233-
line.content.trim_matches('\n'),
234-
w = width as usize
235-
)
236-
} else if line.content.matches('\n').count() == 1 {
237-
// regular line, no selection (cheapest)
238-
line.content.clone()
234+
format!("{:w$}\n", trimmed, w = width as usize)
239235
} else {
240236
// weird eof missing eol line
241-
format!("{}\n", line.content.trim_matches('\n'))
237+
format!("{}\n", trimmed)
242238
};
243239
//TODO: allow customize tabsize
244240
let content = Cow::from(filled.replace("\t", " "));
@@ -371,3 +367,33 @@ impl Component for DiffComponent {
371367
self.focused = focus
372368
}
373369
}
370+
371+
#[cfg(test)]
372+
mod tests {
373+
use super::*;
374+
375+
#[test]
376+
fn test_lineendings() {
377+
let mut text = Vec::new();
378+
379+
DiffComponent::add_line(
380+
&mut text,
381+
10,
382+
&DiffLine {
383+
content: String::from("line 1\r\n"),
384+
line_type: DiffLineType::None,
385+
},
386+
false,
387+
false,
388+
false,
389+
);
390+
391+
assert_eq!(text.len(), 2);
392+
393+
if let Text::Styled(c, _) = &text[1] {
394+
assert_eq!(c, "line 1\n");
395+
} else {
396+
panic!("err")
397+
}
398+
}
399+
}

0 commit comments

Comments
 (0)