Skip to content

Commit

Permalink
first crack at adding SyncGuard from Funami580
Browse files Browse the repository at this point in the history
Co-authored-by: Funami580 <63090225+Funami580@users.noreply.github.com>
  • Loading branch information
chris-laplante and Funami580 committed Feb 6, 2024
1 parent 3915728 commit 5d0d3ea
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
pub use crate::kb::Key;
pub use crate::term::{
user_attended, user_attended_stderr, Term, TermFamily, TermFeatures, TermTarget,
user_attended, user_attended_stderr, SyncGuard, Term, TermFamily, TermFeatures, TermTarget,
};
pub use crate::utils::{
colors_enabled, colors_enabled_stderr, measure_text_width, pad_str, pad_str_with,
Expand Down
50 changes: 50 additions & 0 deletions src/term.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::cell::Cell;
use std::fmt::{Debug, Display};
use std::io::{self, Read, Write};
use std::sync::{Arc, Mutex, RwLock};
Expand Down Expand Up @@ -78,6 +79,19 @@ impl<'a> TermFeatures<'a> {
is_a_color_terminal(self.0)
}

#[inline]
pub fn is_synchronized_output_supported(&self) -> bool {
#[cfg(unix)]
{
supports_synchronized_output()
}
#[cfg(not(unix))]
{
// TODO
false
}
}

/// Check if this terminal is an msys terminal.
///
/// This is sometimes useful to disable features that are known to not
Expand Down Expand Up @@ -656,6 +670,42 @@ impl<'a> Read for &'a Term {
}
}

pub struct SyncGuard<'a> {
term: Cell<Option<&'a Term>>,
}

impl<'a> SyncGuard<'a> {
pub fn begin_sync(term: &'a Term) -> io::Result<Self> {
let ret = if term.features().is_synchronized_output_supported() {
term.write_str("\x1b[?2026h")?;
Some(term)
} else {
None
};

Ok(Self {
term: Cell::new(ret),
})
}

pub fn finish_sync(self) -> io::Result<()> {
self.finish_sync_inner()
}

fn finish_sync_inner(&self) -> io::Result<()> {
if let Some(term) = self.term.take() {
term.write_str("\x1b[?2026l")?;
}
Ok(())
}
}

impl Drop for SyncGuard<'_> {
fn drop(&mut self) {
let _ = self.finish_sync_inner();
}
}

#[cfg(all(unix, not(target_arch = "wasm32")))]
pub use crate::unix_term::*;
#[cfg(target_arch = "wasm32")]
Expand Down

0 comments on commit 5d0d3ea

Please sign in to comment.