summary refs log tree commit diff
diff options
context:
space:
mode:
authorIrene Knapp <ireneista@gmail.com>2021-01-15 22:51:46 -0800
committerIrene Knapp <ireneista@gmail.com>2021-01-15 22:51:46 -0800
commit031c033745060fc2c83db5a2bf63fd1942ad3176 (patch)
tree83a91dfe1fad531c98db05beb8e83b215f46c7cd
parent24c8a559bcf442e1769f3f0cb98e6430e3db1be5 (diff)
Add some rudimentary terminal stuff, which breaks the previously existing functionality
-rw-r--r--Cargo.lock8
-rw-r--r--Cargo.toml1
-rw-r--r--src/main.rs2
-rw-r--r--src/terminal.rs93
4 files changed, 100 insertions, 4 deletions
diff --git a/Cargo.lock b/Cargo.lock
index c3482f9..4f2703d 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -354,9 +354,9 @@ checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c"
 
 [[package]]
 name = "proc-macro2"
-version = "1.0.20"
+version = "1.0.24"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "175c513d55719db99da20232b06cda8bab6b83ec2d04e3283edf0213c37c1a29"
+checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71"
 dependencies = [
  "unicode-xid",
 ]
@@ -484,9 +484,9 @@ checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c"
 
 [[package]]
 name = "syn"
-version = "1.0.39"
+version = "1.0.58"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "891d8d6567fe7c7f8835a3a98af4208f3846fba258c1bc3c31d6e506239f11f9"
+checksum = "cc60a3d73ea6594cd712d830cc1f0390fd71542d8c8cd24e70cc54cdfd5e05d5"
 dependencies = [
  "proc-macro2",
  "quote",
diff --git a/Cargo.toml b/Cargo.toml
index f245856..a0470e6 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -5,6 +5,7 @@ authors = ["Irene Knapp <ireneista@gmail.com>"]
 edition = "2018"
 
 [dependencies]
+#futures = "0.3"
 lalrpop-util = "0.19"
 
 [build-dependencies]
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());
+  }
+}