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

Don't add an extra line after the progress bar #338

Merged
merged 1 commit into from Dec 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/draw_target.rs
Expand Up @@ -173,7 +173,7 @@ impl ProgressDrawTarget {
if !draw_state.lines.is_empty() && draw_state.move_cursor {
term.move_cursor_up(*last_line_count)?;
} else {
term.clear_last_lines(*last_line_count)?;
clear_last_lines(term, *last_line_count)?;
}

let shift = match draw_state.alignment {
Expand Down Expand Up @@ -389,8 +389,14 @@ impl ProgressDrawState {
}

pub fn draw_to_term(&self, term: &Term) -> io::Result<()> {
for line in &self.lines {
term.write_line(line)?;
let len = self.lines.len();
for (idx, line) in self.lines.iter().enumerate() {
if idx + 1 != len {
term.write_line(line)?;
} else {
// Don't append a '\n' if this is the last line
term.write_str(line)?;
}
}
Ok(())
}
Expand Down Expand Up @@ -423,3 +429,16 @@ impl Default for MultiProgressAlignment {
Self::Top
}
}

/// Fork of console::clear_last_lines that assumes that the last line doesn't contain a '\n'
fn clear_last_lines(term: &Term, n: usize) -> io::Result<()> {
term.move_cursor_up(n.saturating_sub(1))?;
for i in 0..n {
term.clear_line()?;
if i + 1 != n {
term.move_cursor_down(1)?;
}
}
term.move_cursor_up(n.saturating_sub(1))?;
Ok(())
}