termion/examples/mouse.rs

49 lines
1.3 KiB
Rust
Raw Normal View History

extern crate termion;
2016-07-23 17:50:33 +01:00
use termion::event::{Key, Event, MouseEvent};
2016-07-23 19:41:43 +01:00
use termion::input::{TermRead, MouseTerminal};
2016-07-23 17:50:33 +01:00
use termion::raw::IntoRawMode;
use std::io::{Write, stdout, stdin};
2016-07-23 17:50:33 +01:00
fn main() {
let stdin = stdin();
2016-07-23 19:41:43 +01:00
let mut stdout = MouseTerminal::from(stdout().into_raw_mode().unwrap());
writeln!(stdout,
"{}{}q to exit. Type stuff, use alt, click around...",
termion::clear::All,
termion::cursor::Goto(1, 1))
.unwrap();
let mut x = 5;
let mut y = 5;
for c in stdin.events() {
let evt = c.unwrap();
match evt {
Event::Key(Key::Char('q')) => break,
Event::Mouse(me) => {
match me {
MouseEvent::Press(_, a, b) |
MouseEvent::Release(a, b) |
MouseEvent::Hold(a, b) => {
x = a;
y = b;
}
}
}
_ => {}
}
write!(stdout,
"{}{} {:?}{}",
termion::clear::All,
termion::cursor::Goto(x, y),
evt,
termion::cursor::Goto(x, y))
.unwrap();
stdout.flush().unwrap();
}
2016-07-23 17:50:33 +01:00
write!(stdout, "{}", termion::cursor::Show).unwrap();
}