cursive/examples/key_codes.rs

47 lines
927 B
Rust
Raw Normal View History

extern crate cursive;
use cursive::{Cursive, Printer};
use cursive::traits::*;
use cursive::event::{Event, EventResult};
fn main() {
let mut siv = Cursive::new();
2017-10-08 23:01:04 +00:00
siv.add_layer(KeyCodeView::new(10).fixed_size((70, 10)));
siv.run();
}
struct KeyCodeView {
history: Vec<String>,
size: usize,
}
impl KeyCodeView {
fn new(size: usize) -> Self {
KeyCodeView {
history: Vec::new(),
size: size,
}
}
}
impl View for KeyCodeView {
2016-07-16 18:20:40 +00:00
fn draw(&self, printer: &Printer) {
2016-06-26 00:10:18 +00:00
for (y, line) in self.history.iter().enumerate() {
2016-09-28 22:07:02 +00:00
printer.print((0, y), line);
}
}
fn on_event(&mut self, event: Event) -> EventResult {
let line = format!("{:?}", event);
self.history.push(line);
while self.history.len() > self.size {
self.history.remove(0);
}
EventResult::Consumed(None)
}
}