summaryrefslogtreecommitdiffstats
path: root/third_party/rust/termion/examples/mouse.rs
blob: cbe8baf4649bb4ca177781679bd439a41a931f90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
extern crate termion;

use termion::event::*;
use termion::cursor::{self, DetectCursorPos};
use termion::input::{TermRead, MouseTerminal};
use termion::raw::IntoRawMode;
use std::io::{self, Write};

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))
            .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,
                               cursor::Goto(a, b))
                                .unwrap();
                    }
                }
            }
            _ => {}
        }

        stdout.flush().unwrap();
    }
}