aboutsummaryrefslogtreecommitdiffstats
path: root/src/blocking
diff options
context:
space:
mode:
Diffstat (limited to 'src/blocking')
-rw-r--r--src/blocking/mod.rs4
-rw-r--r--src/blocking/reader.rs46
-rw-r--r--src/blocking/writer.rs43
3 files changed, 93 insertions, 0 deletions
diff --git a/src/blocking/mod.rs b/src/blocking/mod.rs
new file mode 100644
index 0000000..1ffaccd
--- /dev/null
+++ b/src/blocking/mod.rs
@@ -0,0 +1,4 @@
+pub mod reader;
+pub use reader::Reader;
+pub mod writer;
+pub use writer::Writer;
diff --git a/src/blocking/reader.rs b/src/blocking/reader.rs
new file mode 100644
index 0000000..86c348d
--- /dev/null
+++ b/src/blocking/reader.rs
@@ -0,0 +1,46 @@
+/// Reads ttyrec frames from a `std::io::Read` instance.
+pub struct Reader<T: std::io::Read> {
+ input: T,
+ parser: crate::Parser,
+ buf: [u8; 4096],
+}
+
+impl<T: std::io::Read> Reader<T> {
+ /// Creates a new `Reader` from a `std::io::Read` instance.
+ pub fn new(input: T) -> Self {
+ Self {
+ input,
+ parser: crate::Parser::new(),
+ buf: [0; 4096],
+ }
+ }
+
+ /// Returns the next parsed frame from the input stream.
+ ///
+ /// # Errors
+ /// * `crate::Error::EOF`: The input stream has been closed.
+ /// * `crate::Error::Read`: There was an error reading from the input
+ /// stream.
+ pub fn read_frame(&mut self) -> crate::Result<crate::Frame> {
+ loop {
+ if let Some(frame) = self.parser.next_frame() {
+ return Ok(frame);
+ }
+ let bytes = self
+ .input
+ .read(&mut self.buf)
+ .map_err(|source| crate::Error::Read { source })?;
+ if bytes == 0 {
+ return Err(crate::Error::EOF);
+ }
+ self.parser.add_bytes(&self.buf[..bytes]);
+ }
+ }
+
+ /// How much the timestamps in this file should be offset by.
+ ///
+ /// See `Parser::offset`.
+ pub fn offset(&self) -> Option<std::time::Duration> {
+ self.parser.offset()
+ }
+}
diff --git a/src/blocking/writer.rs b/src/blocking/writer.rs
new file mode 100644
index 0000000..5dade25
--- /dev/null
+++ b/src/blocking/writer.rs
@@ -0,0 +1,43 @@
+/// Writes ttyrec frames to a `std::io::Write` instance.
+pub struct Writer<T: std::io::Write> {
+ output: T,
+ creator: crate::Creator,
+}
+
+impl<T: std::io::Write> Writer<T> {
+ /// Creates a new `Writer` from a `std::io::Write` instance.
+ pub fn new(output: T) -> Self {
+ Self {
+ output,
+ creator: crate::Creator::new(),
+ }
+ }
+
+ /// Writes a new frame to the output stream, using the current time and
+ /// given data.
+ ///
+ /// # Errors
+ /// * `crate::Error::Write`: There was an error writing to the input
+ /// stream.
+ pub fn frame(&mut self, data: &[u8]) -> crate::Result<()> {
+ self.frame_at(std::time::Instant::now(), data)
+ }
+
+ /// Writes a new frame to the output stream, using the given time and
+ /// data.
+ ///
+ /// # Errors
+ /// * `crate::Error::Write`: There was an error writing to the input
+ /// stream.
+ pub fn frame_at(
+ &mut self,
+ cur_time: std::time::Instant,
+ data: &[u8],
+ ) -> crate::Result<()> {
+ let frame = self.creator.frame_at(cur_time, data);
+ let bytes: Vec<u8> = frame.try_into()?;
+ self.output
+ .write_all(&bytes)
+ .map_err(|source| crate::Error::Write { source })
+ }
+}