summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/error.rs9
-rw-r--r--src/main.rs2
-rw-r--r--src/terminal.rs34
-rw-r--r--src/terminal/error.rs31
4 files changed, 70 insertions, 6 deletions
diff --git a/src/error.rs b/src/error.rs
index 018d812..b8289f6 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -1,5 +1,6 @@
 use crate::path::GenericPath;
 use crate::path::error::{FileNameError, DirectoryNameError};
+use crate::terminal::error::TerminalError;
 
 
 type ParseError<'a> =
@@ -16,6 +17,7 @@ pub enum Error {
   PathLexicallyRelative(GenericPath),
   PathLexicallyInvalid(GenericPath),
   PathEmpiricallyFile(GenericPath),
+  Terminal(TerminalError),
 }
 
 impl std::error::Error for Error { }
@@ -48,6 +50,7 @@ impl std::fmt::Display for Error {
         f.write_fmt(format_args!(
             "There's a file at {}, not a directory.",
             path)),
+      Error::Terminal(e) => e.fmt(f),
     }
   }
 }
@@ -81,3 +84,9 @@ impl From<DirectoryNameError> for Error {
     Error::DirectoryName(e)
   }
 }
+
+impl From<TerminalError> for Error {
+  fn from(e: TerminalError) -> Error {
+    Error::Terminal(e)
+  }
+}
diff --git a/src/main.rs b/src/main.rs
index 3199528..d7a2aff 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -39,7 +39,7 @@ fn repl() -> Result<()> {
   loop {
     prompt()?;
 
-    terminal::handle_input(io::stdin())?;
+    terminal::handle_input_terminal(io::stdin())?;
     let input = read()?;
     match input {
       Input::String(string) => execute(&string)?,
diff --git a/src/terminal.rs b/src/terminal.rs
index 6d65c82..27e3c68 100644
--- a/src/terminal.rs
+++ b/src/terminal.rs
@@ -1,6 +1,12 @@
-use std::io::{self, BufRead, BufReader, Read};
+use crate::terminal::error::{TerminalError};
+
+use nix::sys::termios;
+use std::io::{BufRead, BufReader, Read};
+use std::os::unix::io::AsRawFd;
 use std::str;
 
+pub mod error;
+
 
 struct CharBufReader<R: Read> {
   reader: BufReader<R>,
@@ -17,10 +23,10 @@ impl<R: Read> CharBufReader<R> {
   }
 
   pub fn fill_buf(&mut self)
-    -> std::result::Result<&str, io::Error>
+    -> std::result::Result<&str, TerminalError>
   {
     loop {
-      let byte_buffer = self.reader.fill_buf()?;
+      let byte_buffer = self.reader.fill_buf().map_err(error::input)?;
 
       match str::from_utf8(byte_buffer) {
         Err(error) => {
@@ -63,13 +69,31 @@ impl<R: Read> CharBufReader<R> {
 }
 
 
+pub fn handle_input_terminal(input_stream: impl Read + AsRawFd)
+  -> std::result::Result<(), TerminalError>
+{
+  let fd = input_stream.as_raw_fd();
+  let mut termios = termios::tcgetattr(fd)
+      .map_err(error::mode_setting)?;
+  termios.local_flags.insert(termios::LocalFlags::ECHO);
+  termios.local_flags.remove(termios::LocalFlags::ECHOCTL);
+  termios.local_flags.remove(termios::LocalFlags::ICANON);
+  termios.control_chars[termios::SpecialCharacterIndices::VMIN as usize] = 1;
+  termios.control_chars[termios::SpecialCharacterIndices::VTIME as usize] = 0;
+  termios::tcsetattr(fd, termios::SetArg::TCSANOW, &termios)
+      .map_err(error::mode_setting)?;
+
+  handle_input(input_stream)
+}
+
+
 pub fn handle_input(input_stream: impl Read)
-  -> std::result::Result<(), io::Error>
+  -> std::result::Result<(), TerminalError>
 {
   let reader = BufReader::new(input_stream);
   let mut reader = CharBufReader::new(reader);
 
-  let string = reader.fill_buf()?;
+  let string = reader.fill_buf().map_err(error::input)?;
   println!("top level {:?}", string);
 
   let n_to_consume = string.len();
diff --git a/src/terminal/error.rs b/src/terminal/error.rs
new file mode 100644
index 0000000..6666e49
--- /dev/null
+++ b/src/terminal/error.rs
@@ -0,0 +1,31 @@
+#[derive(Clone,Debug,Eq,Hash,Ord,PartialEq,PartialOrd)]
+pub enum TerminalError {
+  Input(String),
+  ModeSetting(String),
+}
+
+impl std::error::Error for TerminalError { }
+
+impl std::fmt::Display for TerminalError {
+  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+    match self {
+      TerminalError::Input(s) =>
+        f.write_fmt(format_args!(
+            "Can't read terminal input: {}", s)),
+      TerminalError::ModeSetting(s) =>
+        f.write_fmt(format_args!(
+            "Can't set terminal mode: {}", s)),
+    }
+  }
+}
+
+
+pub fn input(e: impl std::error::Error) -> TerminalError {
+  TerminalError::ModeSetting(format!("{}", e))
+}
+
+
+pub fn mode_setting(e: impl std::error::Error) -> TerminalError {
+  TerminalError::ModeSetting(format!("{}", e))
+}
+