summary refs log tree commit diff
path: root/src/terminal
diff options
context:
space:
mode:
authorIrene Knapp <ireneista@gmail.com>2021-01-16 14:12:06 -0800
committerIrene Knapp <ireneista@gmail.com>2021-01-16 14:12:06 -0800
commitceabb18d9529940985abb5bb13312f76482e584a (patch)
tree44a00afda48d79ae8b6dfb17f3a03567cf31e119 /src/terminal
parent380db764e0c5466f1564045c7da40fdde967612c (diff)
Move the CharBufReader stuff into its own file
Diffstat (limited to 'src/terminal')
-rw-r--r--src/terminal/decoding.rs68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/terminal/decoding.rs b/src/terminal/decoding.rs
new file mode 100644
index 0000000..249b257
--- /dev/null
+++ b/src/terminal/decoding.rs
@@ -0,0 +1,68 @@
+use crate::terminal::error::{self, TerminalError};
+
+use std::io::{BufRead, BufReader, Read};
+use std::str;
+
+
+pub struct CharBufReader<R: Read> {
+  byte_reader: BufReader<R>,
+  char_buffer: String,
+}
+
+
+impl<R: Read> CharBufReader<R> {
+  pub fn new(input_stream: R) -> CharBufReader<R> {
+    let byte_reader = BufReader::new(input_stream);
+
+    CharBufReader {
+      byte_reader: byte_reader,
+      char_buffer: String::new(),
+    }
+  }
+
+  pub fn fill_buf(&mut self)
+    -> std::result::Result<&str, TerminalError>
+  {
+    loop {
+      let byte_buffer = self.byte_reader.fill_buf().map_err(error::input)?;
+
+      match str::from_utf8(byte_buffer) {
+        Err(error) => {
+          let n_valid = error.valid_up_to();
+          if n_valid == 0 {
+            self.byte_reader.consume(1);
+          } else {
+            match str::from_utf8(&byte_buffer[..n_valid]) {
+              Err(_) => {
+                self.byte_reader.consume(1);
+              },
+              Ok(chars) => {
+                self.char_buffer.push_str(chars);
+
+                self.byte_reader.consume(n_valid);
+
+                break;
+              },
+            }
+          }
+        },
+        Ok(chars) => {
+          self.char_buffer.push_str(chars);
+
+          let n_to_consume = byte_buffer.len();
+          self.byte_reader.consume(n_to_consume);
+
+          break;
+        }
+      }
+    }
+
+    return Ok(&self.char_buffer);
+  }
+
+  pub fn consume(&mut self, amount: usize) {
+    self.char_buffer.replace_range(..amount, "");
+    println!("consume {:?}", self.char_buffer);
+  }
+}
+