rs-kilo/src/editor.rs

81 lines
1.7 KiB
Rust
Raw Normal View History

2019-08-22 14:25:18 -04:00
//! Editor functionality
2019-08-22 16:44:47 -04:00
use crate::helpers::*;
2019-08-22 14:25:18 -04:00
2019-08-22 16:44:47 -04:00
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;
2019-08-22 16:44:47 -04:00
/// Main structure for the editor
///
/// impl blocks are split similarly to the original C implementation
pub struct Editor {}
2019-08-22 14:25:18 -04:00
// init
2019-08-22 14:25:18 -04:00
impl Editor {
pub fn new() -> Self {
Editor {}
2019-08-22 16:44:47 -04:00
}
}
2019-08-22 16:44:47 -04:00
// Terminal
impl Editor {
fn read_key(&mut self) -> Option<Vec<char>> {
2019-08-22 16:44:47 -04:00
let stdin = io::stdin();
let stdin = stdin.lock();
2019-08-22 16:44:47 -04:00
let mut in_str = String::new();
let mut input = BufReader::with_capacity(3, stdin);
2019-08-22 16:44:47 -04:00
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;
2019-08-22 16:44:47 -04:00
}
return Some(output);
}
}
2019-08-22 16:44:47 -04:00
// 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];
2019-08-22 16:44:47 -04:00
if first == ctrl_key('q') {
// Break out of the input loop
return None;
}
print!("{:?}\r\n", chars);
// Continue the main input loop
Some(())
}
}
2019-08-22 14:25:18 -04:00
}
}
// 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();
}
}