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

POC synchronized output leveraging work by Funami580 #625

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
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>>)
}
}