From 8583987a671e215ebce2ab6960fc6ea53b985b47 Mon Sep 17 00:00:00 2001 From: Chloe Fontenot Date: Wed, 13 Nov 2024 14:32:34 -0600 Subject: [PATCH] added the ability to control the scroll direction even if autoscroll is enabled. Kinda buggy. --- .vscode/launch.json | 17 ++++---- Cargo.lock | 103 +++++++++++++++++++++++++++++++++++++++----- Cargo.toml | 1 + src/main.rs | 38 ++++++++++++---- 4 files changed, 132 insertions(+), 27 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index c0c1d5c..25d9c0a 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -7,15 +7,15 @@ { "type": "lldb", "request": "launch", - "name": "Debug executable 'hello-rust'", + "name": "Debug executable 'text_scroller-rs'", "cargo": { "args": [ "build", - "--bin=hello-rust", - "--package=hello-rust" + "--bin=text_scroller-rs", + "--package=text_scroller-rs" ], "filter": { - "name": "hello-rust", + "name": "text_scroller-rs", "kind": "bin" } }, @@ -25,16 +25,17 @@ { "type": "lldb", "request": "launch", - "name": "Debug unit tests in executable 'hello-rust'", + "name": "Debug unit tests in executable 'text_scroller-rs'", "cargo": { "args": [ "test", "--no-run", - "--bin=hello-rust", - "--package=hello-rust" + "--bin=text_scroller-rs", + "--package=text_scroller-rs", + "-- -a" ], "filter": { - "name": "hello-rust", + "name": "text_scroller-rs", "kind": "bin" } }, diff --git a/Cargo.lock b/Cargo.lock index afcc955..19a57e8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -38,7 +38,7 @@ version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c" dependencies = [ - "windows-sys", + "windows-sys 0.59.0", ] [[package]] @@ -48,7 +48,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2109dbce0e72be3ec00bed26e6a7479ca384ad226efdd66db8fa2e3a38c83125" dependencies = [ "anstyle", - "windows-sys", + "windows-sys 0.59.0", ] [[package]] @@ -103,6 +103,16 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" +[[package]] +name = "colored" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8" +dependencies = [ + "lazy_static", + "windows-sys 0.48.0", +] + [[package]] name = "device_query" version = "0.2.8" @@ -135,6 +145,12 @@ version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + [[package]] name = "libc" version = "0.2.161" @@ -214,6 +230,7 @@ name = "text_scroller-rs" version = "0.1.0" dependencies = [ "clap", + "colored", "device_query", "substring", "text_io", @@ -253,13 +270,37 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", +] + [[package]] name = "windows-sys" version = "0.59.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" dependencies = [ - "windows-targets", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", ] [[package]] @@ -268,28 +309,46 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", "windows_i686_gnullvm", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", ] +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + [[package]] name = "windows_aarch64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + [[package]] name = "windows_aarch64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + [[package]] name = "windows_i686_gnu" version = "0.52.6" @@ -302,24 +361,48 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + [[package]] name = "windows_i686_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + [[package]] name = "windows_x86_64_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + [[package]] name = "windows_x86_64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + [[package]] name = "windows_x86_64_msvc" version = "0.52.6" diff --git a/Cargo.toml b/Cargo.toml index f8c90ef..2137517 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ edition = "2021" [dependencies] clap = {version = "4.5.20", features = ["derive"]} +colored = "2.1.0" device_query = "0.2.0" substring = "1.4.5" text_io = "0.1.12" diff --git a/src/main.rs b/src/main.rs index 6235b9e..c867901 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ use substring::Substring; use device_query::{DeviceQuery, DeviceState, Keycode}; use std::sync::mpsc; use clap::Parser; +use colored::Colorize; /// Search for a pattern in a file and display the lines that contain it. #[derive(Parser)] @@ -40,8 +41,7 @@ fn main() { let mut i: i64 = 0; let (tx, rx) = mpsc::sync_channel(0); - let tx_clone = tx.clone(); - + let (tx2, rx2) = mpsc::sync_channel(1); let initial_polling_rate: u64 = 50; let initial_scroll_left: bool = true; // polling thread @@ -49,8 +49,19 @@ fn main() { let mut polling_rate = initial_polling_rate; let mut scroll_left = initial_scroll_left; let device_state = DeviceState::new(); - loop { + match rx2.try_recv() { + Ok(new_scroll) => { + scroll_left = new_scroll; + } + Err(mpsc::TryRecvError::Empty) => { + //eprintln!("No new data. ;("); + } + Err(mpsc::TryRecvError::Disconnected) => { + eprintln!("Channel has been closed."); + break; + } + } // Poll for input let keys = device_state.get_keys(); // poll the keys (polling_rate, scroll_left) = detect_keys(keys, polling_rate, scroll_left); @@ -61,14 +72,13 @@ fn main() { // main thread let mut polling_rate = initial_polling_rate; let mut scroll_left = initial_scroll_left; + let mut send_status = String::new(); loop { // Get data from the polling thread match rx.try_recv() { Ok((new_rate, new_scroll)) => { polling_rate = new_rate; - if !args.autoscroll { - scroll_left = new_scroll; - } + scroll_left = new_scroll; } Err(mpsc::TryRecvError::Empty) => { //eprintln!("No new data. ;("); @@ -80,15 +90,25 @@ fn main() { } thread::sleep(Duration::from_millis(polling_rate)); - 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 nonspace. if args.autoscroll { if !char::is_whitespace(input.chars().nth(0).unwrap()) | !char::is_whitespace(input.chars().nth(input.len() - 1).unwrap()) { scroll_left = !scroll_left; + // Let the other thread know the scrolling has been changed + match tx2.try_send(scroll_left) { + Ok(_) => { + send_status = "OK!".green().to_string(); + } + Err(mpsc::TrySendError::Full(_)) => { + send_status = "BUF".red().to_string(); + } + Err(mpsc::TrySendError::Disconnected(_)) => { + send_status = "DIS".to_string(); + } + } } } - + println!("i: {}, com buf status: {}, upd: {}ms, scrll: {}, {}", {i +=1; i}, send_status, polling_rate, either!(scroll_left => "L"; "R"), input); // Scroll text if scroll_left { input = shift_left(input);