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, KeyEvent}, execute, terminal::{disable_raw_mode, enable_raw_mode}, }; async fn set_cutoff(cutoff: f64, freq: f64) -> Result<(), reqwest::Error> { // Some simple CLI args requirements... let url = format!("http://localhost:3000/cutoff"); let mut map = HashMap::new(); map.insert("min_cutoff", cutoff); map.insert("max_cutoff", cutoff + 10.0); map.insert("freq_hz", freq); 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; let mut freq = 1.0; loop { let mut delay = Delay::new(Duration::from_millis(1_000)).fuse(); let mut event = reader.next().fuse(); let mut change = false; 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 let Event::Key(k) = event { if let KeyCode::Char(c) = k.code { change = true; match c { '[' => { cutoff -= 1.0; } ']' => { cutoff += 1.0; } '1' => { freq *= 0.9; } '2' => { freq *= 1.1; } _ => { change = false; } } } } if change { set_cutoff(cutoff, freq).await.unwrap(); 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() }