Skip to content

Commit

Permalink
POC synchronized output leveraging work by Funami580
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-laplante committed Feb 6, 2024
1 parent 44618a8 commit b9c6d05
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -13,7 +13,7 @@ readme = "README.md"
exclude = ["screenshots/*"]

[dependencies]
console = { version = "0.15", default-features = false, features = ["ansi-parsing"] }
console = { git = "https://github.com/chris-laplante/console.git", branch = "cpl/sync-output-drop-guard", default-features = false, features = ["ansi-parsing"] }
futures-core = { version = "0.3", default-features = false, optional = true }
number_prefix = "0.4"
portable-atomic = "1.0.0"
Expand Down
5 changes: 5 additions & 0 deletions src/draw_target.rs
Expand Up @@ -472,6 +472,8 @@ impl DrawState {
return Ok(());
}

let sync_guard = term.begin_sync();

if !self.lines.is_empty() && self.move_cursor {
term.move_cursor_up(last_line_count.as_usize())?;
} else {
Expand Down Expand Up @@ -547,6 +549,9 @@ impl DrawState {
}
term.write_str(&" ".repeat(last_line_filler))?;

// End synchronized update
drop(sync_guard);

term.flush()?;
*last_line_count = real_len - orphan_visual_line_count + shift;
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Expand Up @@ -269,4 +269,4 @@ pub use crate::progress_bar::{ProgressBar, WeakProgressBar};
pub use crate::rayon::ParallelProgressIterator;
pub use crate::state::{ProgressFinish, ProgressState};
pub use crate::style::ProgressStyle;
pub use crate::term_like::TermLike;
pub use crate::term_like::{NoOpSyncGuard, SyncGuardLike, TermLike};
27 changes: 27 additions & 0 deletions src/term_like.rs
Expand Up @@ -3,6 +3,24 @@ use std::io;

use console::Term;

pub trait SyncGuardLike<'a> {
fn finish_sync(self) -> io::Result<()>;
}

impl<'a> SyncGuardLike<'a> for console::SyncGuard<'a> {
fn finish_sync(self) -> io::Result<()> {
self.finish_sync()
}
}

pub struct NoOpSyncGuard;

impl<'a> SyncGuardLike<'a> for NoOpSyncGuard {
fn finish_sync(self) -> io::Result<()> {
Ok(())
}
}

/// A trait for minimal terminal-like behavior.
///
/// Anything that implements this trait can be used a draw target via [`ProgressDrawTarget::term_like`].
Expand Down Expand Up @@ -34,6 +52,10 @@ pub trait TermLike: Debug + Send + Sync {
fn clear_line(&self) -> io::Result<()>;

fn flush(&self) -> io::Result<()>;

fn begin_sync<'a>(&'a self) -> io::Result<Box<dyn SyncGuardLike<'a> + 'a>> {
Ok(Box::new(NoOpSyncGuard))
}
}

impl TermLike for Term {
Expand Down Expand Up @@ -76,4 +98,9 @@ impl TermLike for Term {
fn flush(&self) -> io::Result<()> {
self.flush()
}

fn begin_sync<'a>(&'a self) -> io::Result<Box<dyn SyncGuardLike<'a> + 'a>> {
console::SyncGuard::begin_sync(self)
.map(|guard| Box::new(guard) as Box<dyn SyncGuardLike<'a>>)
}
}

0 comments on commit b9c6d05

Please sign in to comment.