rs-kilo/src/editor.rs
2019-08-23 14:57:26 -04:00

81 lines
1.7 KiB
Rust

//! 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<Vec<char>> {
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<char> = 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();
}
}