Skip to content

Commit

Permalink
Fix rendering of consecutive newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
YS-L committed Mar 24, 2024
1 parent 6cf4507 commit 823a931
Show file tree
Hide file tree
Showing 5 changed files with 276 additions and 10 deletions.
193 changes: 193 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,199 @@ mod tests {
assert_eq!(lines, expected);
}

#[test]
fn test_multiple_newlines() {
let mut app = AppBuilder::new("tests/data/multiple_newlines.csv")
.build()
.unwrap();
thread::sleep(time::Duration::from_millis(100));

let backend = TestBackend::new(50, 45);
let mut terminal = Terminal::new(backend).unwrap();

step_and_draw(&mut app, &mut terminal, Control::Nothing);
step_and_draw(&mut app, &mut terminal, Control::ToggleLineWrap(true));
let expected = vec![
"──────────────────────────────────────────────────",
" a b c ",
"───┬─────────────────────────────────────┬────────",
"1 │ 1 this is a 12345 │ ",
" │ very long │ ",
" │ text that │ ",
" │ surely │ ",
" │ will not │ ",
" │ fit in │ ",
" │ your small │ ",
" │ screen │ ",
"2 │ 2 this 678910 │ ",
" │ │ ",
" │ is │ ",
" │ │ ",
" │ an │ ",
" │ │ ",
" │ even │ ",
" │ │ ",
" │ longer │ ",
" │ │ ",
" │ text │ ",
" │ │ ",
" │ that │ ",
" │ │ ",
" │ surely │ ",
" │ │ ",
" │ will │ ",
" │ │ ",
" │ not │ ",
" │ │ ",
" │ fit │ ",
" │ │ ",
" │ in │ ",
" │ │ ",
" │ your │ ",
" │ │ ",
" │ small │ ",
" │ │ ",
" │ screen │ ",
"3 │ 3 normal 123,456,789 │ ",
" │ text now │ ",
" │ │ ",
"───┴─────────────────────────────────────┴────────",
"Word wrap enabled ",
];
let actual_buffer = terminal.backend().buffer().clone();
let lines = to_lines(&actual_buffer);
assert_eq!(lines, expected);
}

#[test]
fn test_carriage_returns() {
let mut app = AppBuilder::new("tests/data/multi_lines_carriage_return.csv")
.build()
.unwrap();
thread::sleep(time::Duration::from_millis(100));

let backend = TestBackend::new(50, 45);
let mut terminal = Terminal::new(backend).unwrap();

step_and_draw(&mut app, &mut terminal, Control::Nothing);
step_and_draw(&mut app, &mut terminal, Control::ToggleLineWrap(true));
let expected = vec![
"──────────────────────────────────────────────────",
" a b c ",
"───┬─────────────────────────────────────┬────────",
"1 │ 1 this is a 12345 │ ",
" │ very long │ ",
" │ text that │ ",
" │ surely │ ",
" │ will not │ ",
" │ fit in │ ",
" │ your small │ ",
" │ screen │ ",
"2 │ 2 this 678910 │ ",
" │ │ ",
" │ is │ ",
" │ │ ",
" │ an │ ",
" │ │ ",
" │ even │ ",
" │ │ ",
" │ longer │ ",
" │ │ ",
" │ text │ ",
" │ │ ",
" │ that │ ",
" │ │ ",
" │ surely │ ",
" │ │ ",
" │ will │ ",
" │ │ ",
" │ not │ ",
" │ │ ",
" │ fit │ ",
" │ │ ",
" │ in │ ",
" │ │ ",
" │ your │ ",
" │ │ ",
" │ small │ ",
" │ │ ",
" │ screen │ ",
"3 │ 3 normal 123,456,789 │ ",
" │ text now │ ",
" │ │ ",
"───┴─────────────────────────────────────┴────────",
"Word wrap enabled ",
];
let actual_buffer = terminal.backend().buffer().clone();
let lines = to_lines(&actual_buffer);
assert_eq!(lines, expected);
}

#[test]
fn test_starts_with_newline() {
let mut app = AppBuilder::new("tests/data/starts_with_newline.csv")
.build()
.unwrap();
thread::sleep(time::Duration::from_millis(100));

let backend = TestBackend::new(50, 20);
let mut terminal = Terminal::new(backend).unwrap();

step_and_draw(&mut app, &mut terminal, Control::Nothing);
let expected = vec![
"──────────────────────────────────────────────────",
" a b c ",
"───┬─────────────────────────────────────┬────────",
"1 │ 1 this is a … 12345 │ ",
"2 │ 2 … 678910 │ ",
"3 │ 3 normal tex… 123,456,789 │ ",
" │ │ ",
" │ │ ",
" │ │ ",
" │ │ ",
" │ │ ",
" │ │ ",
" │ │ ",
" │ │ ",
" │ │ ",
" │ │ ",
" │ │ ",
" │ │ ",
"───┴─────────────────────────────────────┴────────",
"stdin [Row 1/3, Col 1/3] ",
];
let actual_buffer = terminal.backend().buffer().clone();
let lines = to_lines(&actual_buffer);
assert_eq!(lines, expected);

step_and_draw(&mut app, &mut terminal, Control::ToggleLineWrap(true));
let expected = vec![
"──────────────────────────────────────────────────",
" a b c ",
"───┬─────────────────────────────────────┬────────",
"1 │ 1 this is a 12345 │ ",
" │ very long │ ",
" │ text that │ ",
" │ surely │ ",
" │ will not │ ",
" │ fit in │ ",
" │ your small │ ",
" │ screen │ ",
"2 │ 2 678910 │ ",
" │ starts │ ",
" │ with new │ ",
" │ line │ ",
"3 │ 3 normal 123,456,789 │ ",
" │ text now │ ",
" │ │ ",
"───┴─────────────────────────────────────┴────────",
"Word wrap enabled ",
];
let actual_buffer = terminal.backend().buffer().clone();
let lines = to_lines(&actual_buffer);
assert_eq!(lines, expected);
}

#[test]
fn test_column_widths_boundary_condition() {
let mut app = AppBuilder::new("tests/data/cities.csv").build().unwrap();
Expand Down
6 changes: 4 additions & 2 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,11 @@ impl<'a> CsvTable<'a> {
wrap::LineWrapper::new(&spans, usable_width as usize, is_word_wrap);
let mut num_lines = 0;
loop {
if line_wrapper.next().is_none() {
line_wrapper.next();
num_lines += 1;
if line_wrapper.finished() {
break;
}
num_lines += 1;
}
num_lines
}
Expand Down Expand Up @@ -675,6 +676,7 @@ impl<'a> CsvTable<'a> {
state.is_word_wrap,
);
// state.debug = format!("get_row_heights elapsed: {:?}", _tic.elapsed());
// state.debug = format!("row_heights: {:?}, area_height: {:?}", row_heights, area.height);
ViewLayout {
column_widths,
row_heights,
Expand Down
23 changes: 15 additions & 8 deletions src/wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ impl<'a> LineWrapper<'a> {
}

pub fn next(&mut self) -> Option<Line<'a>> {
if self.finished() {
return None;
}
let mut out_spans = vec![];
let mut remaining_width = self.max_width;
loop {
Expand Down Expand Up @@ -73,14 +76,6 @@ impl<'a> LineWrapper<'a> {
break;
}
}
// Filter out empty spans
let out_spans = out_spans
.into_iter()
.filter(|s| !s.content.is_empty())
.collect::<Vec<_>>();
if out_spans.is_empty() {
return None;
}
Some(Line::from(out_spans))
}

Expand Down Expand Up @@ -165,6 +160,7 @@ mod tests {
assert_eq!(
wrapper.next(),
Some(Line::from(vec![
Span::raw(""),
Span::styled("my", style),
Span::raw("wor")
]))
Expand Down Expand Up @@ -297,4 +293,15 @@ mod tests {
assert_eq!(wrapper.next(), Some(Line::from(vec![Span::raw("é")])));
assert_eq!(wrapper.next(), None);
}

#[test]
fn test_multiple_newlines() {
let s = Span::raw("ééé\n\nééé");
let spans = vec![s.clone()];
let mut wrapper = LineWrapper::new(&spans, 4, false);
assert_eq!(wrapper.next(), Some(Line::from(vec![Span::raw("ééé")])));
assert_eq!(wrapper.next(), Some(Line::from(vec![Span::raw("")])));
assert_eq!(wrapper.next(), Some(Line::from(vec![Span::raw("ééé")])));
assert_eq!(wrapper.next(), None);
}
}
32 changes: 32 additions & 0 deletions tests/data/multi_lines_carriage_return.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
a,b,c
1,this is a very long text that surely will not fit in your small screen,12345
2,"this

is

an

even

longer

text

that

surely

will

not

fit

in

your

small

screen",678910
3,normal text now,"123,456,789"
32 changes: 32 additions & 0 deletions tests/data/multiple_newlines.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
a,b,c
1,this is a very long text that surely will not fit in your small screen,12345
2,"this

is

an

even

longer

text

that

surely

will

not

fit

in

your

small

screen",678910
3,normal text now,"123,456,789"

0 comments on commit 823a931

Please sign in to comment.