mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-27 11:16:03 +00:00
3d1507c3cf
Didn't the proper methods in the standard lib, so I implemented a simple utf-8 char reader.
53 lines
1.1 KiB
Rust
53 lines
1.1 KiB
Rust
extern crate cursive;
|
|
|
|
use cursive::Cursive;
|
|
|
|
use cursive::view::{View,BoxView};
|
|
use cursive::printer::Printer;
|
|
use cursive::event::{EventResult,Event};
|
|
|
|
fn main() {
|
|
let mut siv = Cursive::new();
|
|
|
|
siv.add_layer(BoxView::new((30,10), KeyCodeView::new(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 {
|
|
fn draw(&mut self, printer: &Printer, _: bool) {
|
|
for (y,line) in self.history.iter().enumerate() {
|
|
printer.print((0,y), &line);
|
|
}
|
|
}
|
|
|
|
fn on_event(&mut self, event: Event) -> EventResult {
|
|
let line = match event {
|
|
Event::CharEvent(c) => format!("Char: {}", c),
|
|
Event::KeyEvent(key) => format!("Key: {}", key),
|
|
};
|
|
self.history.push(line);
|
|
|
|
while self.history.len() > self.size {
|
|
self.history.remove(0);
|
|
}
|
|
|
|
EventResult::Consumed(None)
|
|
}
|
|
}
|
|
|