From b49071623265da8e87a5fad737f418ce1ac26e17 Mon Sep 17 00:00:00 2001 From: Chloe Fontenot Date: Wed, 13 Nov 2024 13:27:53 -0600 Subject: [PATCH] I forgor to push something --- src/main.rs | 47 +++++++++++++++-------------------------------- 1 file changed, 15 insertions(+), 32 deletions(-) diff --git a/src/main.rs b/src/main.rs index aca2cc4..b567281 100644 --- a/src/main.rs +++ b/src/main.rs @@ -44,48 +44,33 @@ fn main() { let initial_polling_rate: u64 = 50; let initial_scroll_left: bool = true; - + if args.autoscroll { // polling thread thread::spawn(move || { let mut polling_rate = initial_polling_rate; let mut scroll_left = initial_scroll_left; let device_state = DeviceState::new(); - loop { - if args.autoscroll { - - match rx.try_recv() { - Ok((new_rate, new_scroll)) => { - polling_rate = new_rate; - scroll_left = new_scroll; - } - Err(mpsc::TryRecvError::Empty) => { - //eprintln!("No new data. ;("); - } - Err(mpsc::TryRecvError::Disconnected) => { - eprintln!("Channel has been closed."); - break; - } - } + loop { + // Poll for input + let keys = device_state.get_keys(); // poll the keys + (polling_rate, scroll_left) = detect_keys(keys, polling_rate, scroll_left); + let _ = tx.send((polling_rate, scroll_left)); + thread::sleep(Duration::from_millis(100)); // poll every 50 ms } - // Poll for input - let keys = device_state.get_keys(); // poll the keys - (polling_rate, scroll_left) = detect_keys(keys, polling_rate, scroll_left); - let _ = tx.send((polling_rate, scroll_left)); - thread::sleep(Duration::from_millis(100)); // poll every 50 ms - } - }); + } // main thread let mut polling_rate = initial_polling_rate; let mut scroll_left = initial_scroll_left; loop { // Get data from the polling thread - match rx.try_recv() { Ok((new_rate, new_scroll)) => { polling_rate = new_rate; - scroll_left = new_scroll; + if !args.autoscroll { + scroll_left = new_scroll; + } } Err(mpsc::TryRecvError::Empty) => { //eprintln!("No new data. ;("); @@ -99,12 +84,10 @@ fn main() { println!("i: {}, upd: {}ms, scrll: {}, {}", {i +=1; i}, polling_rate, either!(scroll_left => "L"; "R"), input); - // Autoscroll: Autoscroll automatically toggles ths scrolling direction when the scrolling reaches a space. + // Autoscroll: Autoscroll automatically toggles ths scrolling direction when the scrolling reaches a nonspace. if args.autoscroll { - if input.chars().nth(input.len()) == Some(' ') { + if !char::is_whitespace(input.chars().nth(0).unwrap()) | !char::is_whitespace(input.chars().nth(input.len() - 1).unwrap()) { scroll_left = !scroll_left; - // inform the other thread of the new state of scroll_left - tx_clone.send((polling_rate, scroll_left)); } } @@ -118,7 +101,7 @@ fn main() { } fn shift_left(mut input: String) -> String { - let tmp: char = input.chars().nth(0).expect("char"); + let tmp: char = input.chars().nth(0).unwrap(); input = input.substring(1,input.len()).to_string(); input.push_str(&tmp.to_string()); return input; @@ -126,7 +109,7 @@ fn shift_left(mut input: String) -> String { fn shift_right(mut input: String) -> String { let tmp: String = input.substring(0, input.len() - 1).to_string(); - input = input.chars().nth(input.len() - 1).expect("char").to_string(); + input = input.chars().nth(input.len() - 1).unwrap().to_string(); input.push_str(&tmp); return input; }