added mouse hold support (#48)

This adds support for the escape codes generated in rxvt and xterm
format by holding a button and moving the mouse around.
This commit is contained in:
IGI-111 2016-09-07 11:05:41 +02:00 committed by ticki
parent 98a4ccce4b
commit 5ebda9866f
2 changed files with 40 additions and 17 deletions

View File

@ -9,20 +9,24 @@ fn main() {
let stdin = stdin();
let mut stdout = MouseTerminal::from(stdout().into_raw_mode().unwrap());
write!(stdout, "{}{}q to exit. Type stuff, use alt, click around...", termion::clear::All, termion::cursor::Goto(1, 1)).unwrap();
writeln!(stdout,
"{}{}q to exit. Type stuff, use alt, click around...",
termion::clear::All,
termion::cursor::Goto(1, 1))
.unwrap();
let mut x = 1;
let mut y = 1;
let mut x = 5;
let mut y = 5;
for c in stdin.events() {
let evt = c.unwrap();
writeln!(stdout, "{:?}{}{}", evt, termion::cursor::Goto(5, 5), termion::clear::CurrentLine).unwrap();
match evt {
Event::Key(Key::Char('q')) => break,
Event::Mouse(me) => {
match me {
MouseEvent::Press(_, a, b) |
MouseEvent::Release(a, b) => {
MouseEvent::Release(a, b) |
MouseEvent::Hold(a, b) => {
x = a;
y = b;
}
@ -30,7 +34,13 @@ fn main() {
}
_ => {}
}
writeln!(stdout, "{:?}{}", evt, termion::cursor::Goto(x, y)).unwrap();
write!(stdout,
"{}{} {:?}{}",
termion::clear::All,
termion::cursor::Goto(x, y),
evt,
termion::cursor::Goto(x, y))
.unwrap();
stdout.flush().unwrap();
}

View File

@ -26,6 +26,10 @@ pub enum MouseEvent {
///
/// The coordinates are one-based.
Release(u16, u16),
/// A mouse button is held over the given coordinates.
///
/// The coordinates are one-based.
Hold(u16, u16),
}
/// A mouse button.
@ -159,20 +163,28 @@ where I: Iterator<Item = Result<u8, Error>>
let cx = nums.next().unwrap().parse::<u16>().unwrap();
let cy = nums.next().unwrap().parse::<u16>().unwrap();
let button = match cb {
0 => MouseButton::Left,
1 => MouseButton::Middle,
2 => MouseButton::Right,
64 => MouseButton::WheelUp,
65 => MouseButton::WheelDown,
let event = match cb {
0...2 | 64...65 => {
let button = match cb {
0 => MouseButton::Left,
1 => MouseButton::Middle,
2 => MouseButton::Right,
64 => MouseButton::WheelUp,
65 => MouseButton::WheelDown,
_ => return error,
};
match c {
b'M' => MouseEvent::Press(button, cx, cy),
b'm' => MouseEvent::Release(cx, cy),
_ => return error,
}
}
32 => MouseEvent::Hold(cx, cy),
_ => return error,
};
Event::Mouse(match c {
b'M' => MouseEvent::Press(button, cx, cy),
b'm' => MouseEvent::Release(cx, cy),
_ => return error,
})
Event::Mouse(event)
}
Some(Ok(c @ b'0'...b'9')) => {
// Numbered escape code.
@ -203,6 +215,7 @@ where I: Iterator<Item = Result<u8, Error>>
33 => MouseEvent::Press(MouseButton::Middle, cx, cy),
34 => MouseEvent::Press(MouseButton::Right, cx, cy),
35 => MouseEvent::Release(cx, cy),
64 => MouseEvent::Hold(cx, cy),
96 |
97 => MouseEvent::Press(MouseButton::WheelUp, cx, cy),
_ => return error,