summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.rs2
-rw-r--r--src/terminal.rs93
2 files changed, 95 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs
index d1a25a6..3199528 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -13,6 +13,7 @@ pub mod error;
 pub mod path;
 pub mod prelude;
 pub mod result;
+pub mod terminal;
 
 
 pub enum Input {
@@ -38,6 +39,7 @@ fn repl() -> Result<()> {
   loop {
     prompt()?;
 
+    terminal::handle_input(io::stdin())?;
     let input = read()?;
     match input {
       Input::String(string) => execute(&string)?,
diff --git a/src/terminal.rs b/src/terminal.rs
new file mode 100644
index 0000000..6d65c82
--- /dev/null
+++ b/src/terminal.rs
@@ -0,0 +1,93 @@
+use std::io::{self, BufRead, BufReader, Read};
+use std::str;
+
+
+struct CharBufReader<R: Read> {
+  reader: BufReader<R>,
+  char_buffer: String,
+}
+
+
+impl<R: Read> CharBufReader<R> {
+  pub fn new(reader: BufReader<R>) -> CharBufReader<R> {
+    CharBufReader {
+      reader: reader,
+      char_buffer: String::new(),
+    }
+  }
+
+  pub fn fill_buf(&mut self)
+    -> std::result::Result<&str, io::Error>
+  {
+    loop {
+      let byte_buffer = self.reader.fill_buf()?;
+
+      match str::from_utf8(byte_buffer) {
+        Err(error) => {
+          let n_valid = error.valid_up_to();
+          if n_valid == 0 {
+            self.reader.consume(1);
+          } else {
+            match str::from_utf8(&byte_buffer[..n_valid]) {
+              Err(_) => {
+                self.reader.consume(1);
+              },
+              Ok(chars) => {
+                self.char_buffer.push_str(chars);
+
+                self.reader.consume(n_valid);
+
+                break;
+              },
+            }
+          }
+        },
+        Ok(chars) => {
+          self.char_buffer.push_str(chars);
+
+          let n_to_consume = byte_buffer.len();
+          self.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);
+  }
+}
+
+
+pub fn handle_input(input_stream: impl Read)
+  -> std::result::Result<(), io::Error>
+{
+  let reader = BufReader::new(input_stream);
+  let mut reader = CharBufReader::new(reader);
+
+  let string = reader.fill_buf()?;
+  println!("top level {:?}", string);
+
+  let n_to_consume = string.len();
+  reader.consume(n_to_consume);
+
+  Ok(())
+}
+
+
+#[cfg(test)]
+mod tests {
+  use super::*;
+  use std::io::Cursor;
+
+  #[test]
+  fn test_empty_input() {
+    let buffer = Cursor::new(vec![]);
+    let result = handle_input(buffer);
+    assert!(result.is_ok());
+  }
+}