Skip to content

Instantly share code, notes, and snippets.

@levicole
Created July 20, 2017 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save levicole/f780c29b726fa1adbdd511aed79e6710 to your computer and use it in GitHub Desktop.
Save levicole/f780c29b726fa1adbdd511aed79e6710 to your computer and use it in GitHub Desktop.
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