hecto/src/editor.rs

49 lines
991 B
Rust
Raw Normal View History

2021-01-28 16:47:40 -05:00
use std::io::{self, stdout};
use termion::event::Key;
use termion::input::TermRead;
use termion::raw::IntoRawMode;
pub struct Editor {
should_quit: bool,
}
impl Editor {
pub fn run(&mut self) {
let _stdout = stdout().into_raw_mode().unwrap();
loop {
if let Err(error) = self.process_keypress() {
die(error);
}
if self.should_quit {
break;
}
}
}
pub fn default() -> Self {
Self { should_quit: false }
}
fn process_keypress(&mut self) -> Result<(), std::io::Error> {
let pressed_key = read_key()?;
match pressed_key {
Key::Ctrl('q') => self.should_quit = true,
_ => (),
}
Ok(())
}
}
fn read_key() -> Result<Key, std::io::Error> {
loop {
if let Some(key) = io::stdin().lock().keys().next() {
return key;
}
}
}
fn die(e: std::io::Error) {
panic!(e);
}