init
This commit is contained in:
130
src/main.rs
Normal file
130
src/main.rs
Normal file
@@ -0,0 +1,130 @@
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
use substring::Substring;
|
||||
use device_query::{DeviceQuery, DeviceState, Keycode};
|
||||
use std::sync::mpsc;
|
||||
|
||||
#[macro_use] extern crate text_io;
|
||||
macro_rules! either {
|
||||
($test:expr => $true_expr:expr; $false_expr:expr) => {
|
||||
if $test {
|
||||
$true_expr
|
||||
}
|
||||
else {
|
||||
$false_expr
|
||||
}
|
||||
}
|
||||
}
|
||||
fn main() {
|
||||
|
||||
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.
|
||||
if input.chars().nth(input.len()) != Some(' ') {
|
||||
input.push(' ');
|
||||
}
|
||||
|
||||
let mut i: i64 = 0;
|
||||
|
||||
let (tx, rx) = mpsc::sync_channel(0);
|
||||
|
||||
let initial_polling_rate: u64 = 50;
|
||||
let initial_scroll_left: bool = true;
|
||||
|
||||
// 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 {
|
||||
// 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;
|
||||
}
|
||||
Err(mpsc::TryRecvError::Empty) => {
|
||||
//eprintln!("No new data. ;(");
|
||||
}
|
||||
Err(mpsc::TryRecvError::Disconnected) => {
|
||||
eprintln!("Channel has been closed.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
thread::sleep(Duration::from_millis(polling_rate));
|
||||
|
||||
println!("i: {}, upd: {}ms, scrll: {}, {}", {i +=1; i}, polling_rate, either!(scroll_left => "L"; "R"), input);
|
||||
// Scroll text
|
||||
if scroll_left {
|
||||
input = shift_left(input);
|
||||
} else {
|
||||
input = shift_right(input);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn shift_left(mut input: String) -> String {
|
||||
let tmp: char = input.chars().nth(0).expect("char");
|
||||
input = input.substring(1,input.len()).to_string();
|
||||
input.push_str(&tmp.to_string());
|
||||
return input;
|
||||
}
|
||||
|
||||
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.push_str(&tmp);
|
||||
return input;
|
||||
}
|
||||
|
||||
fn detect_keys(keys: Vec<Keycode>, mut polling_rate: u64, mut scroll_left: bool) -> (u64, bool) {
|
||||
//detecting keydown events
|
||||
//println!("{} {} ", polling_rate, scroll_left);
|
||||
if keys.is_empty() {
|
||||
return (polling_rate, scroll_left);
|
||||
}
|
||||
if keys.contains(&Keycode::Up) {
|
||||
polling_rate = increase_poll(polling_rate);
|
||||
}
|
||||
if keys.contains(&Keycode::Down) {
|
||||
polling_rate = decrease_poll(polling_rate)
|
||||
}
|
||||
if keys.contains(&Keycode::Left) {
|
||||
scroll_left = true;
|
||||
}
|
||||
if keys.contains(&Keycode::Right) {
|
||||
scroll_left = false;
|
||||
}
|
||||
return (polling_rate, scroll_left);
|
||||
}
|
||||
|
||||
fn increase_poll(mut polling_rate: u64) -> u64 {
|
||||
if polling_rate < 1000 {
|
||||
polling_rate += 5;
|
||||
} else {
|
||||
eprintln!("polling rate can't go higher!");
|
||||
}
|
||||
return polling_rate;
|
||||
}
|
||||
|
||||
fn decrease_poll(mut polling_rate: u64) -> u64 {
|
||||
if polling_rate > 0 {
|
||||
polling_rate -= 5;
|
||||
} else {
|
||||
eprintln!("polling rate can't go lower!");
|
||||
}
|
||||
return polling_rate;
|
||||
}
|
Reference in New Issue
Block a user