From ceabb18d9529940985abb5bb13312f76482e584a Mon Sep 17 00:00:00 2001 From: Irene Knapp Date: Sat, 16 Jan 2021 14:12:06 -0800 Subject: Move the CharBufReader stuff into its own file --- src/terminal.rs | 71 ++++-------------------------------------------- src/terminal/decoding.rs | 68 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 66 deletions(-) create mode 100644 src/terminal/decoding.rs 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 { - reader: BufReader, - char_buffer: String, -} - - -impl CharBufReader { - pub fn new(reader: BufReader) -> CharBufReader { - 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 { + byte_reader: BufReader, + char_buffer: String, +} + + +impl CharBufReader { + pub fn new(input_stream: R) -> CharBufReader { + 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); + } +} + -- cgit 1.4.1