From 9a9fe869115ccc839867ebeb1c6c6439252b7f02 Mon Sep 17 00:00:00 2001 From: Chloe Fontenot Date: Wed, 13 Nov 2024 22:52:25 -0600 Subject: [PATCH] performance improvements --- src/main.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index ff7e39e..bc6ae52 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ use device_query::{DeviceQuery, DeviceState, Keycode}; use std::sync::mpsc; use clap::Parser; use colored::Colorize; +use std::io::{stdout, Write}; /// Search for a pattern in a file and display the lines that contain it. #[derive(Parser)] @@ -27,11 +28,12 @@ macro_rules! either { } } fn main() { + let mut lock = stdout().lock(); // obtain std::out lock let args = Cli::parse(); if args.autoscroll { - println!("Autoscroll is enabled!") + writeln!(lock, "Autoscroll is enabled!").unwrap(); } - print!("Use the up/down arrow keys to adjust the update speed!\nleft/right controls the scroll direction!\nPlease enter a string: "); + write!(lock, "Use the up/down arrow keys to adjust the update speed!\nleft/right controls the scroll direction!\nPlease enter a string: ").unwrap(); 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(' ') { @@ -77,7 +79,7 @@ fn main() { // main thread let mut polling_rate = initial_polling_rate; let mut scroll_left = initial_scroll_left; - let mut send_status = String::new(); + let mut send_status = "N/A".to_string(); loop { // Get data from the polling thread match rx.try_recv() { @@ -114,7 +116,7 @@ fn main() { } } } - print!("i: {}, com buf status: {}, upd: {}ms, scrll: {}, {}\r\n", {i +=1; i}, send_status, polling_rate, either!(scroll_left => "L"; "R"), input); + write!(lock, "i: {}, com buf status: {}, upd: {}ms, scrll: {}, {}\r\n", {i +=1; i}, send_status, polling_rate, either!(scroll_left => "L"; "R"), input).unwrap(); // Scroll text if scroll_left { input = shift_left(input); @@ -141,7 +143,6 @@ fn shift_right(mut input: String) -> String { fn detect_keys(keys: Vec, mut polling_rate: u64, mut scroll_left: bool) -> (bool, u64, bool) { let mut user_update = false; //detecting keydown events - //println!("{} {} ", polling_rate, scroll_left); if keys.is_empty() { return (user_update, polling_rate, scroll_left); }