added the ability to control the scroll direction even if autoscroll is enabled. Kinda buggy.

This commit is contained in:
Chloe Fontenot 🏳️‍⚧️ 2024-11-13 14:32:34 -06:00
parent b81babe426
commit 8583987a67
4 changed files with 132 additions and 27 deletions

17
.vscode/launch.json vendored
View File

@ -7,15 +7,15 @@
{ {
"type": "lldb", "type": "lldb",
"request": "launch", "request": "launch",
"name": "Debug executable 'hello-rust'", "name": "Debug executable 'text_scroller-rs'",
"cargo": { "cargo": {
"args": [ "args": [
"build", "build",
"--bin=hello-rust", "--bin=text_scroller-rs",
"--package=hello-rust" "--package=text_scroller-rs"
], ],
"filter": { "filter": {
"name": "hello-rust", "name": "text_scroller-rs",
"kind": "bin" "kind": "bin"
} }
}, },
@ -25,16 +25,17 @@
{ {
"type": "lldb", "type": "lldb",
"request": "launch", "request": "launch",
"name": "Debug unit tests in executable 'hello-rust'", "name": "Debug unit tests in executable 'text_scroller-rs'",
"cargo": { "cargo": {
"args": [ "args": [
"test", "test",
"--no-run", "--no-run",
"--bin=hello-rust", "--bin=text_scroller-rs",
"--package=hello-rust" "--package=text_scroller-rs",
"-- -a"
], ],
"filter": { "filter": {
"name": "hello-rust", "name": "text_scroller-rs",
"kind": "bin" "kind": "bin"
} }
}, },

103
Cargo.lock generated
View File

@ -38,7 +38,7 @@ version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c" checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c"
dependencies = [ dependencies = [
"windows-sys", "windows-sys 0.59.0",
] ]
[[package]] [[package]]
@ -48,7 +48,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2109dbce0e72be3ec00bed26e6a7479ca384ad226efdd66db8fa2e3a38c83125" checksum = "2109dbce0e72be3ec00bed26e6a7479ca384ad226efdd66db8fa2e3a38c83125"
dependencies = [ dependencies = [
"anstyle", "anstyle",
"windows-sys", "windows-sys 0.59.0",
] ]
[[package]] [[package]]
@ -103,6 +103,16 @@ version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" 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]] [[package]]
name = "device_query" name = "device_query"
version = "0.2.8" version = "0.2.8"
@ -135,6 +145,12 @@ version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
[[package]]
name = "lazy_static"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.161" version = "0.2.161"
@ -214,6 +230,7 @@ name = "text_scroller-rs"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"clap", "clap",
"colored",
"device_query", "device_query",
"substring", "substring",
"text_io", "text_io",
@ -253,13 +270,37 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 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]] [[package]]
name = "windows-sys" name = "windows-sys"
version = "0.59.0" version = "0.59.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
dependencies = [ 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]] [[package]]
@ -268,28 +309,46 @@ version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [ dependencies = [
"windows_aarch64_gnullvm", "windows_aarch64_gnullvm 0.52.6",
"windows_aarch64_msvc", "windows_aarch64_msvc 0.52.6",
"windows_i686_gnu", "windows_i686_gnu 0.52.6",
"windows_i686_gnullvm", "windows_i686_gnullvm",
"windows_i686_msvc", "windows_i686_msvc 0.52.6",
"windows_x86_64_gnu", "windows_x86_64_gnu 0.52.6",
"windows_x86_64_gnullvm", "windows_x86_64_gnullvm 0.52.6",
"windows_x86_64_msvc", "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]] [[package]]
name = "windows_aarch64_gnullvm" name = "windows_aarch64_gnullvm"
version = "0.52.6" version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]] [[package]]
name = "windows_aarch64_msvc" name = "windows_aarch64_msvc"
version = "0.52.6" version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_i686_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]] [[package]]
name = "windows_i686_gnu" name = "windows_i686_gnu"
version = "0.52.6" version = "0.52.6"
@ -302,24 +361,48 @@ version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]] [[package]]
name = "windows_i686_msvc" name = "windows_i686_msvc"
version = "0.52.6" version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" 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]] [[package]]
name = "windows_x86_64_gnu" name = "windows_x86_64_gnu"
version = "0.52.6" version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" 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]] [[package]]
name = "windows_x86_64_gnullvm" name = "windows_x86_64_gnullvm"
version = "0.52.6" version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" 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]] [[package]]
name = "windows_x86_64_msvc" name = "windows_x86_64_msvc"
version = "0.52.6" version = "0.52.6"

View File

@ -5,6 +5,7 @@ edition = "2021"
[dependencies] [dependencies]
clap = {version = "4.5.20", features = ["derive"]} clap = {version = "4.5.20", features = ["derive"]}
colored = "2.1.0"
device_query = "0.2.0" device_query = "0.2.0"
substring = "1.4.5" substring = "1.4.5"
text_io = "0.1.12" text_io = "0.1.12"

View File

@ -4,6 +4,7 @@ use substring::Substring;
use device_query::{DeviceQuery, DeviceState, Keycode}; use device_query::{DeviceQuery, DeviceState, Keycode};
use std::sync::mpsc; use std::sync::mpsc;
use clap::Parser; use clap::Parser;
use colored::Colorize;
/// Search for a pattern in a file and display the lines that contain it. /// Search for a pattern in a file and display the lines that contain it.
#[derive(Parser)] #[derive(Parser)]
@ -40,8 +41,7 @@ fn main() {
let mut i: i64 = 0; let mut i: i64 = 0;
let (tx, rx) = mpsc::sync_channel(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_polling_rate: u64 = 50;
let initial_scroll_left: bool = true; let initial_scroll_left: bool = true;
// polling thread // polling thread
@ -49,8 +49,19 @@ fn main() {
let mut polling_rate = initial_polling_rate; let mut polling_rate = initial_polling_rate;
let mut scroll_left = initial_scroll_left; let mut scroll_left = initial_scroll_left;
let device_state = DeviceState::new(); let device_state = DeviceState::new();
loop { 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 // Poll for input
let keys = device_state.get_keys(); // poll the keys let keys = device_state.get_keys(); // poll the keys
(polling_rate, scroll_left) = detect_keys(keys, polling_rate, scroll_left); (polling_rate, scroll_left) = detect_keys(keys, polling_rate, scroll_left);
@ -61,14 +72,13 @@ fn main() {
// main thread // main thread
let mut polling_rate = initial_polling_rate; let mut polling_rate = initial_polling_rate;
let mut scroll_left = initial_scroll_left; let mut scroll_left = initial_scroll_left;
let mut send_status = String::new();
loop { loop {
// Get data from the polling thread // Get data from the polling thread
match rx.try_recv() { match rx.try_recv() {
Ok((new_rate, new_scroll)) => { Ok((new_rate, new_scroll)) => {
polling_rate = new_rate; polling_rate = new_rate;
if !args.autoscroll { scroll_left = new_scroll;
scroll_left = new_scroll;
}
} }
Err(mpsc::TryRecvError::Empty) => { Err(mpsc::TryRecvError::Empty) => {
//eprintln!("No new data. ;("); //eprintln!("No new data. ;(");
@ -80,15 +90,25 @@ fn main() {
} }
thread::sleep(Duration::from_millis(polling_rate)); 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. // Autoscroll: Autoscroll automatically toggles ths scrolling direction when the scrolling reaches a nonspace.
if args.autoscroll { if args.autoscroll {
if !char::is_whitespace(input.chars().nth(0).unwrap()) | !char::is_whitespace(input.chars().nth(input.len() - 1).unwrap()) { if !char::is_whitespace(input.chars().nth(0).unwrap()) | !char::is_whitespace(input.chars().nth(input.len() - 1).unwrap()) {
scroll_left = !scroll_left; 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 // Scroll text
if scroll_left { if scroll_left {
input = shift_left(input); input = shift_left(input);