//! Editor functionality use crate::helpers::*; use nix::sys::termios::Termios; use nix::unistd::read; use std::io; use std::io::prelude::*; use std::io::{BufReader, Error, Stdin}; use std::str::Chars; /// Main structure for the editor /// /// impl blocks are split similarly to the original C implementation pub struct Editor {} // init impl Editor { pub fn new() -> Self { Editor {} } } // Terminal impl Editor { fn read_key(&mut self) -> Option> { let stdin = io::stdin(); let stdin = stdin.lock(); let mut in_str = String::new(); let mut input = BufReader::with_capacity(3, stdin); input.read_to_string(&mut in_str).unwrap(); let mut output: Vec = vec![]; for char in in_str.chars() { output.push(char); } if output.len() == 0 { return None; } return Some(output); } } // Input impl Editor { pub fn process_keypress(&mut self) -> Option<()> { match self.read_key() { // Just continue the input loop on an "empty" keypress None => Some(()), Some(chars) => { let first = chars[0]; if first == ctrl_key('q') { // Break out of the input loop return None; } print!("{:?}\r\n", chars); // Continue the main input loop Some(()) } } } } // Output impl Editor { pub fn refresh_screen() { let stdout = io::stdout(); let mut handle = stdout.lock(); let mut buffer = String::from("\x1b[2J").into_bytes(); handle.write_all(&mut buffer).unwrap(); } }