2015-05-27 23:55:49 +00:00
|
|
|
extern crate cursive;
|
|
|
|
|
|
|
|
use cursive::Cursive;
|
|
|
|
|
2016-06-26 00:10:18 +00:00
|
|
|
use cursive::view::{View, BoxView};
|
2015-05-27 23:55:49 +00:00
|
|
|
use cursive::printer::Printer;
|
2016-06-26 00:10:18 +00:00
|
|
|
use cursive::event::{EventResult, Event};
|
2015-05-27 23:55:49 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let mut siv = Cursive::new();
|
|
|
|
|
2016-06-26 00:10:18 +00:00
|
|
|
siv.add_layer(BoxView::new((30, 10), KeyCodeView::new(10)));
|
2015-05-27 23:55:49 +00:00
|
|
|
|
|
|
|
siv.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct KeyCodeView {
|
2015-05-28 01:04:33 +00:00
|
|
|
history: Vec<String>,
|
2015-05-27 23:55:49 +00:00
|
|
|
size: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl KeyCodeView {
|
|
|
|
fn new(size: usize) -> Self {
|
|
|
|
KeyCodeView {
|
|
|
|
history: Vec::new(),
|
|
|
|
size: size,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl View for KeyCodeView {
|
2015-06-01 16:20:06 +00:00
|
|
|
fn draw(&mut self, printer: &Printer) {
|
2016-06-26 00:10:18 +00:00
|
|
|
for (y, line) in self.history.iter().enumerate() {
|
|
|
|
printer.print((0, y), &line);
|
2015-05-27 23:55:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-28 01:04:33 +00:00
|
|
|
fn on_event(&mut self, event: Event) -> EventResult {
|
|
|
|
let line = match event {
|
|
|
|
Event::CharEvent(c) => format!("Char: {}", c),
|
|
|
|
Event::KeyEvent(key) => format!("Key: {}", key),
|
2015-05-28 05:13:51 +00:00
|
|
|
};
|
2015-05-28 01:04:33 +00:00
|
|
|
self.history.push(line);
|
2015-05-27 23:55:49 +00:00
|
|
|
|
|
|
|
while self.history.len() > self.size {
|
|
|
|
self.history.remove(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
EventResult::Consumed(None)
|
|
|
|
}
|
|
|
|
}
|