Created
July 20, 2017 17:49
-
-
Save levicole/f780c29b726fa1adbdd511aed79e6710 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate termion; | |
use std::io::{Write, Stdout, Stdin, stdout, stdin}; | |
use termion::cursor; | |
use termion::event::{Key, Event}; | |
use termion::input::TermRead; | |
use termion::raw::{IntoRawMode, RawTerminal}; | |
use termion::screen::AlternateScreen; | |
pub struct Editor<'a> { | |
screen: &'a mut AlternateScreen<Stdout>, | |
stdin: &'a mut Stdin, | |
stdout: &'a mut RawTerminal<Stdout>, | |
size: (u16, u16) | |
} | |
impl<'a> Editor<'a> { | |
pub fn new() -> Result<Editor<'a>, &'static str> { | |
let mut screen = AlternateScreen::from(stdout()); | |
let mut stdin = stdin(); | |
let mut stdout = stdout().into_raw_mode().unwrap(); | |
let size = match termion::terminal_size() { | |
Ok((w,h)) => (w, h), | |
Err(e) => return Err("Couldn't fetch the terminal size") | |
}; | |
Ok(Editor { | |
screen: screen, | |
stdin: stdin, | |
stdout: stdout, | |
size: size | |
}) | |
} | |
pub fn run(&mut self) { | |
self.screen.flush().unwrap(); | |
for c in self.stdin.events() { | |
let evt = c.unwrap(); | |
match evt { | |
Event::Key(Key::Char(ch)) => { | |
write!(self.stdout, "{}", ch).unwrap(); | |
}, | |
Event::Key(Key::Esc) => break, | |
_ => (), | |
}; | |
self.screen.flush().unwrap(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment