rs-kilo/src/editor.rs

71 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::{BufReader, Error, Stdin};
use std::io::prelude::*;
use term_parser::{Action, ActionIter};
/// Main structure for the editor
pub struct Editor {
/// Reference to terminal settings before setting up raw mode
original_termios: Termios,
}
2019-08-22 14:25:18 -04:00
impl Editor {
pub fn new() -> Self {
Editor {
2019-08-22 16:44:47 -04:00
original_termios: get_termios(STDIN_FILENO)
}
}
fn read_key(&mut self) -> char {
let stdin = io::stdin();
let mut in_str = String::new();
let mut input = BufReader::new(stdin.take(1));
input.read_to_string(&mut in_str).unwrap();
let mut chars = in_str.chars();
chars.next().unwrap()
}
fn read_term_code(&mut self) -> ActionIter<BufReader<Stdin>> {
let stdin = io::stdin();
let buffer = BufReader::new(stdin);
let term_code_iter = ActionIter::new(buffer);
2019-08-22 14:25:18 -04:00
2019-08-22 16:44:47 -04:00
term_code_iter
}
pub fn process_keypress(&mut self) -> Option<()> {
for term_code in self.read_term_code() {
let term_code = match term_code {
Ok(code) => code,
Err(e) => panic!("{:?}\r\n", e),
};
print!("{:?}\r\n", term_code);
}
/* let char = self.read_key();
if char == 'q' {
disable_raw_mode(&self.original_termios)?;
// Break out of the input loop
return None;
2019-08-22 14:25:18 -04:00
}
2019-08-22 16:44:47 -04:00
// Echo characters
if is_cntrl(char) {
print!("{}\r\n", char as u8);
} else {
print!("{} ('{}')\r\n", char as u8, char);
} */
Some(())
2019-08-22 14:25:18 -04:00
}
}