2016-09-29 05:45:27 +00:00
|
|
|
use cursive::event::{Event, EventResult};
|
2017-10-12 23:38:55 +00:00
|
|
|
use cursive::traits::*;
|
2020-04-15 19:20:50 +00:00
|
|
|
use cursive::Printer;
|
2015-05-27 23:55:49 +00:00
|
|
|
|
2018-01-16 02:55:27 +00:00
|
|
|
// This example define a custom view that prints any event it receives.
|
|
|
|
// This is a handy way to check the input received by cursive.
|
|
|
|
|
2015-05-27 23:55:49 +00:00
|
|
|
fn main() {
|
2020-04-15 19:20:50 +00:00
|
|
|
let mut siv = cursive::default();
|
2018-06-16 20:23:09 +00:00
|
|
|
siv.add_layer(KeyCodeView::new(10).full_width().fixed_height(10));
|
2015-05-27 23:55:49 +00:00
|
|
|
|
|
|
|
siv.run();
|
|
|
|
}
|
|
|
|
|
2018-01-16 02:55:27 +00:00
|
|
|
// Our view will have a small history of the last events.
|
2015-05-27 23:55:49 +00:00
|
|
|
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(),
|
2019-05-20 22:45:48 +00:00
|
|
|
size,
|
2015-05-27 23:55:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:44 +00:00
|
|
|
// Let's implement the `View` trait.
|
|
|
|
// `View` contains many methods, but only a few are required.
|
2015-05-27 23:55:49 +00:00
|
|
|
impl View for KeyCodeView {
|
2016-07-16 18:20:40 +00:00
|
|
|
fn draw(&self, printer: &Printer) {
|
2018-01-16 02:55:27 +00:00
|
|
|
// We simply draw every event from the history.
|
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);
|
2015-05-27 23:55:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-28 01:04:33 +00:00
|
|
|
fn on_event(&mut self, event: Event) -> EventResult {
|
2018-01-16 02:55:27 +00:00
|
|
|
// Each line will be a debug-format of the event.
|
2016-07-11 01:27:26 +00:00
|
|
|
let line = format!("{:?}", event);
|
2015-05-28 01:04:33 +00:00
|
|
|
self.history.push(line);
|
2015-05-27 23:55:49 +00:00
|
|
|
|
2018-01-16 02:55:27 +00:00
|
|
|
// Keep a fixed-sized history.
|
2015-05-27 23:55:49 +00:00
|
|
|
while self.history.len() > self.size {
|
|
|
|
self.history.remove(0);
|
|
|
|
}
|
|
|
|
|
2018-01-16 02:55:27 +00:00
|
|
|
// No need to return any callback.
|
2015-05-27 23:55:49 +00:00
|
|
|
EventResult::Consumed(None)
|
|
|
|
}
|
|
|
|
}
|