use petitcarnets as tea;
use tea::{Mouse, MouseMode, Msg};
#[derive(Default)]
pub(crate) struct Mice {
events: Vec<String>,
pub(crate) last_click: Option<String>,
}
fn describe(mouse: &Mouse, name: &str) -> String {
format!("(X: {}, Y: {}) {name}", mouse.x, mouse.y)
}
impl tea::Model for Mice {
type Msg = ();
fn update(&mut self, msg: Msg<()>) -> tea::Cmd<()> {
let line = match msg {
Msg::KeyPress(key) if key.matches_any(&["q", "esc", "ctrl+c"]) => return tea::quit(),
Msg::MouseClick(m) => {
let line = describe(&m, &m.to_string());
self.last_click = Some(line.clone());
format!("click {line}")
}
Msg::MouseRelease(m) => format!("release {}", describe(&m, &m.to_string())),
Msg::MouseWheel(m) => format!("wheel {}", describe(&m, &m.to_string())),
Msg::MouseMotion(m) => format!("motion {}", describe(&m, &m.motion_string())),
Msg::Focus => "focus".to_string(),
Msg::Blur => "blur".to_string(),
_ => return tea::Cmd::none(),
};
self.events.push(line);
let excess = self.events.len().saturating_sub(10);
drop(self.events.drain(..excess));
tea::Cmd::none()
}
fn view(&self) -> tea::View {
let content = format!(
"\n Do mouse stuff. When you're done press q to quit.\n\n {}",
self.events.join("\n ")
);
tea::View::new(content)
.with_alt_screen(true)
.with_mouse_mode(MouseMode::AllMotion)
.with_report_focus(true)
}
}
pub(crate) fn main() -> Result<(), tea::Error> {
let mice = tea::Program::new(Mice::default()).run()?;
match mice.last_click {
Some(click) => println!("Last click: {click}"),
None => println!("No clicks"),
}
Ok(())
}