termion/examples/mouse.rs

47 lines
1.5 KiB
Rust
Raw Normal View History

extern crate termion;
use termion::event::*;
use termion::cursor::{self, DetectCursorPos};
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::{self, Write};
2016-07-23 17:50:33 +01:00
fn main() {
let stdin = io::stdin();
let mut stdout = MouseTerminal::from(io::stdout().into_raw_mode().unwrap());
writeln!(stdout,
"{}{}q to exit. Type stuff, use alt, click around...",
termion::clear::All,
termion::cursor::Goto(1, 1))
2017-03-24 20:53:05 +00:00
.unwrap();
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) => {
write!(stdout, "{}", cursor::Goto(a, b)).unwrap();
let (x, y) = stdout.cursor_pos().unwrap();
write!(stdout,
"{}{}Cursor is at: ({},{}){}",
cursor::Goto(5, 5),
termion::clear::UntilNewline,
x,
y,
2017-03-24 20:53:05 +00:00
cursor::Goto(a, b))
.unwrap();
}
}
}
_ => {}
}
stdout.flush().unwrap();
}
}