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

Add try_read_byte method. #124

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ pub struct Options {
pub struct NBReader {
reader: Receiver<result::Result<PipedChar, PipeError>>,
buffer: String,
bytes:Vec<u8>,
eof: bool,
timeout: Option<time::Duration>,
}
Expand Down Expand Up @@ -172,6 +173,7 @@ impl NBReader {
NBReader {
reader: rx,
buffer: String::with_capacity(1024),
bytes:Vec::with_capacity(1024),
eof: false,
timeout: options.timeout_ms.map(time::Duration::from_millis),
}
Expand Down Expand Up @@ -199,6 +201,29 @@ impl NBReader {
Ok(())
}

/// reads all available byte(u8) from the read channel and stores them in self.bytes
fn read_into_bytes(&mut self) -> Result<(), Error> {
if self.eof {
return Ok(());
}
while let Ok(from_channel) = self.reader.try_recv() {
match from_channel {
Ok(PipedChar::Char(c)) => self.bytes.push(c),
Ok(PipedChar::EOF) => self.eof = true,
// this is just from experience, e.g. "sleep 5" returns the other error which
// most probably means that there is no stdout stream at all -> send EOF
// this only happens on Linux, not on OSX
Err(PipeError::IO(ref err)) => {
// For an explanation of why we use `raw_os_error` see:
// https://github.com/zhiburt/ptyprocess/commit/df003c8e3ff326f7d17bc723bc7c27c50495bb62
self.eof = err.raw_os_error() == Some(5)
}
}
}
Ok(())
}


/// Read until needle is found (blocking!) and return tuple with:
/// 1. yet unread string until and without needle
/// 2. matched needle
Expand Down Expand Up @@ -299,6 +324,18 @@ impl NBReader {
None
}
}

pub fn try_read_byte(&mut self) -> Option<u8> {
// discard eventual errors, EOF will be handled in read_until correctly
let _ = self.read_into_bytes();
if !self.bytes.is_empty() {
let byte=self.bytes.remove(0);
return Some(byte)
} else {
None
}
}

}

#[cfg(test)]
Expand Down
4 changes: 4 additions & 0 deletions src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ impl<W: Write> StreamSession<W> {
self.reader.try_read()
}

pub fn try_read_byte(&mut self) -> Option<u8> {
self.reader.try_read_byte()
}

// wrapper around reader::read_until to give more context for errors
fn exp(&mut self, needle: &ReadUntil) -> Result<(String, String), Error> {
self.reader.read_until(needle)
Expand Down