mirror of
https://github.com/pcvolkmer/checkbar.git
synced 2025-04-19 11:06:50 +00:00
Extract method to read click event from input
This commit is contained in:
parent
c7c3773f55
commit
84fee2495a
31
src/main.rs
31
src/main.rs
@ -59,6 +59,21 @@ async fn run_click_cmd(cmd: String) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn read_click_event() -> Result<ClickEvent, ()> {
|
||||||
|
let stdin = std::io::stdin();
|
||||||
|
let mut input = String::new();
|
||||||
|
|
||||||
|
if stdin.read_line(&mut input).is_ok() {
|
||||||
|
// Return click event after removing leading comma
|
||||||
|
if let Ok(click_event) =
|
||||||
|
serde_json::from_str::<ClickEvent>(input.replace(",{", "{").as_str())
|
||||||
|
{
|
||||||
|
return Ok(click_event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(())
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main(flavor = "multi_thread", worker_threads = 2)]
|
#[tokio::main(flavor = "multi_thread", worker_threads = 2)]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
println!(
|
println!(
|
||||||
@ -71,22 +86,8 @@ async fn main() {
|
|||||||
println!("[");
|
println!("[");
|
||||||
|
|
||||||
let inputs = task::spawn(async {
|
let inputs = task::spawn(async {
|
||||||
let stdin = std::io::stdin();
|
|
||||||
loop {
|
loop {
|
||||||
let mut input = String::new();
|
if let Ok(click_event) = read_click_event() {
|
||||||
|
|
||||||
if stdin.read_line(&mut input).is_err() {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if input.is_empty() {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove leading comma
|
|
||||||
let input = input.replace(",{", "{");
|
|
||||||
|
|
||||||
if let Ok(click_event) = serde_json::from_str::<ClickEvent>(input.as_str()) {
|
|
||||||
// Ignore click event if not left mouse button
|
// Ignore click event if not left mouse button
|
||||||
if click_event.button != 1 {
|
if click_event.button != 1 {
|
||||||
continue;
|
continue;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user