diff --git a/examples/cutoff.rs b/examples/cutoff.rs index b9691b9..e6e2f0e 100644 --- a/examples/cutoff.rs +++ b/examples/cutoff.rs @@ -5,16 +5,18 @@ use futures_timer::Delay; use crossterm::{ cursor::position, - event::{DisableMouseCapture, EnableMouseCapture, Event, EventStream, KeyCode}, + event::{DisableMouseCapture, EnableMouseCapture, Event, EventStream, KeyCode, KeyEvent}, execute, terminal::{disable_raw_mode, enable_raw_mode}, }; -async fn set_cutoff(cutoff: f64) -> Result<(), reqwest::Error> { +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("cutoff", cutoff); + 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?; @@ -38,10 +40,12 @@ 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 => { }, @@ -55,15 +59,30 @@ async fn print_events() { if event == Event::Key(KeyCode::Esc.into()) { break; } - if event == Event::Key(KeyCode::Char('[').into()) { - cutoff -= 1.0; + 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 event == Event::Key(KeyCode::Char(']').into()) { - cutoff += 1.0; - } - if last_cutoff != cutoff { - set_cutoff(cutoff).await; - last_cutoff = cutoff; + if change { + set_cutoff(cutoff, freq).await.unwrap(); println!("cutoff = {}\r", cutoff); } }