Skip to content

Allow reading from a Read/Write pair (where possible) #123

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 39 additions & 6 deletions src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,25 @@ impl<T: Read + Debug + AsRawFd + Send> TermRead for T {}
#[cfg(unix)]
#[derive(Debug, Clone)]
pub struct ReadWritePair {
#[allow(unused)]
read: Arc<Mutex<dyn TermRead>>,
write: Arc<Mutex<dyn TermWrite>>,
/// How output is printed: foreground and background colour, formatting, etc.
style: Style,
}

/// Where the term is writing.
#[derive(Debug, Clone)]
pub enum TermTarget {
/// standard output
Stdout,
/// standard error
Stderr,
#[cfg(unix)]
/// anything which implements the Read and Write traits XXX fixup
ReadWritePair(ReadWritePair),
}

/// Underlying data of the terminal: its target and buffer.
#[derive(Debug)]
pub struct TermInner {
target: TermTarget,
Expand Down Expand Up @@ -126,11 +130,14 @@ impl<'a> TermFeatures<'a> {
#[derive(Clone, Debug)]
pub struct Term {
inner: Arc<TermInner>,
/// See [is_msys_tty](TermFeatures::is_msys_tty).
pub(crate) is_msys_tty: bool,
/// If this is a user attended tty.
pub(crate) is_tty: bool,
}

impl Term {
/// Construct an instance from given data.
fn with_inner(inner: TermInner) -> Term {
let mut term = Term {
inner: Arc::new(inner),
Expand All @@ -143,7 +150,7 @@ impl Term {
term
}

/// Return a new unbuffered terminal.
/// Return a new unbuffered terminal, writing to stdout.
#[inline]
pub fn stdout() -> Term {
Term::with_inner(TermInner {
Expand All @@ -152,7 +159,7 @@ impl Term {
})
}

/// Return a new unbuffered terminal to stderr.
/// Return a new unbuffered terminal, writing to stderr.
#[inline]
pub fn stderr() -> Term {
Term::with_inner(TermInner {
Expand All @@ -161,15 +168,15 @@ impl Term {
})
}

/// Return a new buffered terminal.
/// Return a new buffered terminal, writing to stderr.
pub fn buffered_stdout() -> Term {
Term::with_inner(TermInner {
target: TermTarget::Stdout,
buffer: Some(Mutex::new(vec![])),
})
}

/// Return a new buffered terminal to stderr.
/// Return a new buffered terminal, writing to stderr.
pub fn buffered_stderr() -> Term {
Term::with_inner(TermInner {
target: TermTarget::Stderr,
Expand Down Expand Up @@ -287,7 +294,33 @@ impl Term {
return Ok("".into());
}
let mut rv = String::new();
io::stdin().read_line(&mut rv)?;
#[cfg(not(unix))]
{
io::stdin().read_line(&mut rv)?;
}
#[cfg(unix)]
{
match &self.inner.target {
TermTarget::Stdout | TermTarget::Stderr => {
io::stdin().read_line(&mut rv)?;
}
TermTarget::ReadWritePair(ReadWritePair { read, .. }) => {
let mut input = read.lock().unwrap();
// Read a single char until we hit a newline ("\n", an 0xA byte).
let mut output_bytes = Vec::new();
let mut current_char = [0u8; 1];
loop {
input.read_exact(&mut current_char)?;
if current_char[0] == b'\n' {
break;
} else {
output_bytes.push(current_char[0]);
}
}
rv = std::str::from_utf8(&output_bytes[..]).unwrap().to_string();
}
};
}
let len = rv.trim_end_matches(&['\r', '\n'][..]).len();
rv.truncate(len);
Ok(rv)
Expand Down