Skip to content

Commit 38e9096

Browse files
POC synchronized output leveraging work by Funami580
See console-rs#618, and https://github.com/chris-laplante/console/tree/cpl/sync-output-drop-guard
1 parent 2a8b2ea commit 38e9096

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ readme = "README.md"
1313
exclude = ["screenshots/*"]
1414

1515
[dependencies]
16-
console = { version = "0.15", default-features = false, features = ["ansi-parsing"] }
16+
console = { git = "https://github.com/chris-laplante/console.git", branch = "cpl/sync-output-drop-guard", default-features = false, features = ["ansi-parsing"] }
1717
futures-core = { version = "0.3", default-features = false, optional = true }
1818
number_prefix = "0.4"
1919
portable-atomic = "1.0.0"

src/draw_target.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,8 @@ impl DrawState {
472472
return Ok(());
473473
}
474474

475+
let sync_guard = term.begin_sync();
476+
475477
if !self.lines.is_empty() && self.move_cursor {
476478
term.move_cursor_up(last_line_count.as_usize())?;
477479
} else {
@@ -547,6 +549,9 @@ impl DrawState {
547549
}
548550
term.write_str(&" ".repeat(last_line_filler))?;
549551

552+
// End synchronized update
553+
drop(sync_guard);
554+
550555
term.flush()?;
551556
*last_line_count = real_len - orphan_visual_line_count + shift;
552557
Ok(())

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,4 +259,4 @@ pub use crate::progress_bar::{ProgressBar, WeakProgressBar};
259259
pub use crate::rayon::ParallelProgressIterator;
260260
pub use crate::state::{ProgressFinish, ProgressState};
261261
pub use crate::style::ProgressStyle;
262-
pub use crate::term_like::TermLike;
262+
pub use crate::term_like::{NoOpSyncGuard, SyncGuardLike, TermLike};

src/term_like.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,24 @@ use std::io;
33

44
use console::Term;
55

6+
pub trait SyncGuardLike<'a> {
7+
fn finish_sync(self) -> io::Result<()>;
8+
}
9+
10+
impl<'a> SyncGuardLike<'a> for console::SyncGuard<'a> {
11+
fn finish_sync(self) -> io::Result<()> {
12+
self.finish_sync()
13+
}
14+
}
15+
16+
pub struct NoOpSyncGuard;
17+
18+
impl<'a> SyncGuardLike<'a> for NoOpSyncGuard {
19+
fn finish_sync(self) -> io::Result<()> {
20+
Ok(())
21+
}
22+
}
23+
624
/// A trait for minimal terminal-like behavior.
725
///
826
/// Anything that implements this trait can be used a draw target via [`ProgressDrawTarget::term_like`].
@@ -34,6 +52,10 @@ pub trait TermLike: Debug + Send + Sync {
3452
fn clear_line(&self) -> io::Result<()>;
3553

3654
fn flush(&self) -> io::Result<()>;
55+
56+
fn begin_sync<'a>(&'a self) -> io::Result<Box<dyn SyncGuardLike<'a> + 'a>> {
57+
Ok(Box::new(NoOpSyncGuard))
58+
}
3759
}
3860

3961
impl TermLike for Term {
@@ -76,4 +98,9 @@ impl TermLike for Term {
7698
fn flush(&self) -> io::Result<()> {
7799
self.flush()
78100
}
101+
102+
fn begin_sync<'a>(&'a self) -> io::Result<Box<dyn SyncGuardLike<'a> + 'a>> {
103+
console::SyncGuard::begin_sync(self)
104+
.map(|guard| Box::new(guard) as Box<dyn SyncGuardLike<'a>>)
105+
}
79106
}

0 commit comments

Comments
 (0)