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

InMemoryTerm: fix for write_line and add unit tests #378

Merged
merged 2 commits into from
Feb 17, 2022
Merged
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
90 changes: 85 additions & 5 deletions src/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,18 @@ impl TermLike for InMemoryTerm {

fn write_line(&self, s: &str) -> std::io::Result<()> {
let mut state = self.state.lock().unwrap();

// Don't try to handle writing lines with additional newlines embedded in them - it's not
// worth the extra code for something that indicatif doesn't even do. May revisit in future.
debug_assert!(
s.lines().count() <= 1,
"calling write_line with embedded newlines is not allowed"
);

// vte100 needs the full \r\n sequence to jump to the next line and reset the cursor to
// the beginning of the line. Be flexible and take either \n or \r\n
state.write_str(s)?;
if (s.len() < state.width as usize) && !s.ends_with('\n') {
state.write_str("\n")
} else {
Ok(())
}
state.write_str("\r\n")
}

fn write_str(&self, s: &str) -> std::io::Result<()> {
Expand Down Expand Up @@ -133,6 +139,80 @@ mod test {
use super::*;
use crate::{MultiProgress, ProgressBar, ProgressDrawTarget, ProgressFinish, ProgressStyle};

fn cursor_pos(in_mem: &InMemoryTerm) -> (u16, u16) {
in_mem
.state
.lock()
.unwrap()
.parser
.screen()
.cursor_position()
}

#[test]
fn line_wrapping() {
let in_mem = InMemoryTerm::new(10, 5);
assert_eq!(cursor_pos(&in_mem), (0, 0));

in_mem.write_str("ABCDE").unwrap();
assert_eq!(in_mem.contents(), "ABCDE");
assert_eq!(cursor_pos(&in_mem), (0, 5));

// Should wrap onto next line
in_mem.write_str("FG").unwrap();
assert_eq!(in_mem.contents(), "ABCDE\nFG");
assert_eq!(cursor_pos(&in_mem), (1, 2));

in_mem.write_str("HIJ").unwrap();
assert_eq!(in_mem.contents(), "ABCDE\nFGHIJ");
assert_eq!(cursor_pos(&in_mem), (1, 5));
}

#[test]
fn write_line() {
let in_mem = InMemoryTerm::new(10, 5);
assert_eq!(cursor_pos(&in_mem), (0, 0));

in_mem.write_line("A").unwrap();
assert_eq!(in_mem.contents(), "A");
assert_eq!(cursor_pos(&in_mem), (1, 0));

in_mem.write_line("B").unwrap();
assert_eq!(in_mem.contents(), "A\nB");
assert_eq!(cursor_pos(&in_mem), (2, 0));

in_mem.write_line("Longer than cols").unwrap();
assert_eq!(in_mem.contents(), "A\nB\nLonge\nr tha\nn col\ns");
assert_eq!(cursor_pos(&in_mem), (6, 0));
}

#[test]
fn basic_functionality() {
let in_mem = InMemoryTerm::new(10, 80);

in_mem.write_line("This is a test line").unwrap();
assert_eq!(in_mem.contents(), "This is a test line");

in_mem.write_line("And another line!").unwrap();
assert_eq!(in_mem.contents(), "This is a test line\nAnd another line!");

in_mem.move_cursor_up(1).unwrap();
in_mem.write_str("TEST").unwrap();

assert_eq!(in_mem.contents(), "This is a test line\nTESTanother line!");
}

#[test]
fn newlines() {
let in_mem = InMemoryTerm::new(10, 10);
in_mem.write_line("LINE ONE").unwrap();
in_mem.write_line("LINE TWO").unwrap();
in_mem.write_line("").unwrap();
in_mem.write_line("LINE FOUR").unwrap();

assert_eq!(in_mem.contents(), "LINE ONE\nLINE TWO\n\nLINE FOUR");
}

#[test]
fn basic_progress_bar() {
let in_mem = InMemoryTerm::new(10, 80);
Expand Down