broken code owo

This commit is contained in:
2024-11-04 11:38:11 -06:00
parent 631b307098
commit c101bd1440
3 changed files with 271 additions and 1 deletions

View File

@@ -3,6 +3,16 @@ use std::time::Duration;
use substring::Substring;
use device_query::{DeviceQuery, DeviceState, Keycode};
use std::sync::mpsc;
use clap::Parser;
/// Search for a pattern in a file and display the lines that contain it.
#[derive(Parser)]
struct Cli {
/// Turns on autoscroll
#[arg(short, long)]
autoscroll: bool,
}
#[macro_use] extern crate text_io;
macro_rules! either {
@@ -16,7 +26,10 @@ macro_rules! either {
}
}
fn main() {
let args = Cli::parse();
if args.autoscroll {
println!("Autoscroll is enabled!")
}
print!("Use the up/down arrow keys to adjust the update speed!\nleft/right controls the scroll direction!\nPlease enter a string: ");
let mut input: String = read!("{}\n");
// check the final character, add a space if there isn't one.
@@ -27,6 +40,7 @@ fn main() {
let mut i: i64 = 0;
let (tx, rx) = mpsc::sync_channel(0);
let tx_clone = tx.clone();
let initial_polling_rate: u64 = 50;
let initial_scroll_left: bool = true;
@@ -38,6 +52,22 @@ fn main() {
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;
}
}
}
// Poll for input
let keys = device_state.get_keys(); // poll the keys
(polling_rate, scroll_left) = detect_keys(keys, polling_rate, scroll_left);
@@ -51,6 +81,7 @@ fn main() {
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;
@@ -67,6 +98,16 @@ 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 space.
if args.autoscroll {
if input.chars().nth(input.len()) == Some(' ') {
scroll_left = !scroll_left;
// inform the other thread of the new state of scroll_left
tx_clone.send((polling_rate, scroll_left));
}
}
// Scroll text
if scroll_left {
input = shift_left(input);