Skip to content

Instantly share code, notes, and snippets.

@jorendorff
Forked from levicole/editor.rs
Last active July 20, 2017 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jorendorff/103e20ca0a41f7697ffa3a5105ab55f9 to your computer and use it in GitHub Desktop.
Save jorendorff/103e20ca0a41f7697ffa3a5105ab55f9 to your computer and use it in GitHub Desktop.
extern crate termion;
use std::io::{Write, Stdout, stdout, stdin};
use termion::event::{Key, Event};
use termion::input::TermRead;
use termion::raw::{IntoRawMode, RawTerminal};
use termion::screen::AlternateScreen;
pub struct Editor {
screen: AlternateScreen<Stdout>,
stdout: RawTerminal<Stdout>,
size: (u16, u16)
}
impl Editor {
pub fn new() -> Result<Editor, &'static str> {
let screen = AlternateScreen::from(stdout());
let stdout = stdout().into_raw_mode().unwrap();
let size = match termion::terminal_size() {
Ok((w,h)) => (w, h),
Err(_) => return Err("Couldn't fetch the terminal size")
};
Ok(Editor {
screen: screen,
stdout: stdout,
size: size
})
}
pub fn run(&mut self) {
self.screen.flush().unwrap();
let stdin = stdin();
for c in 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