Skip to main content

quickstart/
quickstart.rs

1use std::time::Duration;
2use terminal_graphics_interactive::{Colour, Display, Event, MouseKind, Terminal};
3
4fn main() -> std::io::Result<()> {
5    let mut terminal = Terminal::start()?;
6
7    // `draw` needs a display at most one row shorter than the window
8    let (width, height) = terminal.size();
9    let mut display = Display::new(width, height.saturating_sub(1));
10
11    loop {
12        terminal.draw(&display)?;
13
14        // wait up to 50 ms for input; `None` means a frame passed with no input
15        match terminal.poll(Duration::from_millis(50))? {
16            Some(Event::Key(key)) if key.is_char('q') || key.is_ctrl('c') => return Ok(()),
17            Some(Event::Mouse(mouse)) if matches!(mouse.kind, MouseKind::Down(_)) => {
18                if let Some((x, y)) = mouse.cell_in(&display) {
19                    display.set_pixel(x, y, 'X', Colour::BrightYellow, Colour::Blue);
20                }
21            }
22            _ => {}
23        }
24    }
25}