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 stdin = stdin();
let mut stdout = MouseTerminal::from(stdout().into_raw_mode().unwrap()); 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 x = 5;
let mut y = 1; let mut y = 5;
for c in stdin.events() { for c in stdin.events() {
let evt = c.unwrap(); let evt = c.unwrap();
writeln!(stdout, "{:?}{}{}", evt, termion::cursor::Goto(5, 5), termion::clear::CurrentLine).unwrap();
match evt { match evt {
Event::Key(Key::Char('q')) => break, Event::Key(Key::Char('q')) => break,
Event::Mouse(me) => { Event::Mouse(me) => {
match me { match me {
MouseEvent::Press(_, a, b) | MouseEvent::Press(_, a, b) |
MouseEvent::Release(a, b) => { MouseEvent::Release(a, b) |
MouseEvent::Hold(a, b) => {
x = a; x = a;
y = b; 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(); stdout.flush().unwrap();
} }

View File

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