diff options
author | Irene Knapp <ireneista@gmail.com> | 2021-01-16 14:12:06 -0800 |
---|---|---|
committer | Irene Knapp <ireneista@gmail.com> | 2021-01-16 14:12:06 -0800 |
commit | ceabb18d9529940985abb5bb13312f76482e584a (patch) | |
tree | 44a00afda48d79ae8b6dfb17f3a03567cf31e119 /src | |
parent | 380db764e0c5466f1564045c7da40fdde967612c (diff) |
Move the CharBufReader stuff into its own file
Diffstat (limited to 'src')
-rw-r--r-- | src/terminal.rs | 71 | ||||
-rw-r--r-- | src/terminal/decoding.rs | 68 |
2 files changed, 73 insertions, 66 deletions
diff --git a/src/terminal.rs b/src/terminal.rs index 27e3c68..041a884 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -1,74 +1,14 @@ -use crate::terminal::error::{TerminalError}; +use crate::terminal::decoding::CharBufReader; +use crate::terminal::error::TerminalError; use nix::sys::termios; -use std::io::{BufRead, BufReader, Read}; +use std::io::Read; use std::os::unix::io::AsRawFd; -use std::str; +pub mod decoding; pub mod error; -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, TerminalError> - { - loop { - let byte_buffer = self.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.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_terminal(input_stream: impl Read + AsRawFd) -> std::result::Result<(), TerminalError> { @@ -90,8 +30,7 @@ pub fn handle_input_terminal(input_stream: impl Read + AsRawFd) pub fn handle_input(input_stream: impl Read) -> std::result::Result<(), TerminalError> { - let reader = BufReader::new(input_stream); - let mut reader = CharBufReader::new(reader); + let mut reader = CharBufReader::new(input_stream); let string = reader.fill_buf().map_err(error::input)?; println!("top level {:?}", string); 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); + } +} + |