thermalcam_decoder/examples/cutoff.rs

93 lines
2.8 KiB
Rust
Raw Normal View History

2024-02-23 01:38:32 +02:00
use std::{collections::HashMap, io::stdout, time::Duration};
use futures::{future::FutureExt, select, StreamExt};
use futures_timer::Delay;
use crossterm::{
cursor::position,
event::{DisableMouseCapture, EnableMouseCapture, Event, EventStream, KeyCode},
execute,
terminal::{disable_raw_mode, enable_raw_mode},
};
async fn set_cutoff(cutoff: f64) -> Result<(), reqwest::Error> {
// Some simple CLI args requirements...
let url = format!("http://localhost:3000/cutoff");
let mut map = HashMap::new();
map.insert("cutoff", cutoff);
let client = reqwest::Client::new();
let res = client.post(url).json(&map).send().await?;
// eprintln!("Response: {:?} {}", res.version(), res.status());
// eprintln!("Headers: {:#?}\n", res.headers());
// let body = res.text().await?;
// println!("{body}");
Ok(())
}
const HELP: &str = r#"EventStream based on futures_util::Stream with tokio
- Keyboard, mouse and terminal resize events enabled
- Prints "." every second if there's no event
- Hit "c" to print current cursor position
- Use Esc to quit
"#;
async fn print_events() {
let mut reader = EventStream::new();
let mut cutoff = 30.0;
let mut last_cutoff = cutoff;
loop {
let mut delay = Delay::new(Duration::from_millis(1_000)).fuse();
let mut event = reader.next().fuse();
select! {
_ = delay => {
},
maybe_event = event => {
match maybe_event {
Some(Ok(event)) => {
if event == Event::Key(KeyCode::Char('c').into()) {
println!("Cursor position: {:?}\r", position());
}
if event == Event::Key(KeyCode::Esc.into()) {
break;
}
if event == Event::Key(KeyCode::Char('[').into()) {
cutoff -= 1.0;
}
if event == Event::Key(KeyCode::Char(']').into()) {
cutoff += 1.0;
}
if last_cutoff != cutoff {
set_cutoff(cutoff).await;
last_cutoff = cutoff;
println!("cutoff = {}\r", cutoff);
}
}
Some(Err(e)) => println!("Error: {:?}\r", e),
None => break,
}
}
};
}
}
#[tokio::main]
async fn main() -> std::io::Result<()> {
println!("{}", HELP);
enable_raw_mode()?;
let mut stdout = stdout();
execute!(stdout, EnableMouseCapture)?;
print_events().await;
execute!(stdout, DisableMouseCapture)?;
disable_raw_mode()
}