Skip to content

Commit d4e593a

Browse files
committed
Factor out insert_codes
1 parent 7a542b1 commit d4e593a

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

src/ansi.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,15 @@ pub(crate) fn enable_windows_ansi() -> bool {
3636
pub(crate) fn enable_windows_ansi() -> bool {
3737
true
3838
}
39+
40+
pub(crate) fn insert_codes(rendered: &str, up_lines: Option<usize>) -> String {
41+
let mut buf = String::with_capacity(rendered.len() + 40);
42+
if let Some(up_lines) = up_lines {
43+
buf.push_str(&up_n_lines_and_home(up_lines));
44+
}
45+
buf.push_str(DISABLE_LINE_WRAP);
46+
buf.push_str(CLEAR_TO_END_OF_SCREEN);
47+
buf.push_str(rendered);
48+
buf.push_str(ENABLE_LINE_WRAP);
49+
buf
50+
}

src/lib.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ pub mod _changelog {
229229
use super::*; // so that hyperlinks work
230230
}
231231

232+
use crate::ansi::insert_codes;
232233
pub use crate::destination::Destination;
233234
pub use crate::helpers::*;
234235
pub use crate::options::Options;
@@ -746,22 +747,15 @@ impl<M: Model> InnerView<M> {
746747
// be simpler?)
747748
rendered.pop();
748749
}
749-
let mut buf = String::new();
750-
if let State::ProgressDrawn {
751-
ref last_drawn_string,
752-
cursor_y,
753-
..
754-
} = self.state
755-
{
756-
if *last_drawn_string == rendered {
757-
return Ok(());
758-
}
759-
buf.push_str(&ansi::up_n_lines_and_home(cursor_y));
760-
}
761-
buf.push_str(ansi::DISABLE_LINE_WRAP);
762-
buf.push_str(ansi::CLEAR_TO_END_OF_SCREEN);
763-
buf.push_str(&rendered);
764-
self.write_output(&buf);
750+
let up_lines = match self.state {
751+
State::ProgressDrawn {
752+
ref last_drawn_string,
753+
cursor_y,
754+
..
755+
} if *last_drawn_string != rendered => Some(cursor_y),
756+
_ => None,
757+
};
758+
self.write_output(&insert_codes(&rendered, up_lines));
765759
let cursor_y = rendered.as_bytes().iter().filter(|b| **b == b'\n').count();
766760
self.state = State::ProgressDrawn {
767761
last_drawn_time: now,

0 commit comments

Comments
 (0)