Refactor i64 to u64 for position

This commit is contained in:
ClementTsang 2020-02-02 14:22:16 -05:00
parent 35f78a7e91
commit 1b09133e3b
3 changed files with 71 additions and 61 deletions

View File

@ -57,14 +57,14 @@ pub struct App {
pub update_process_gui: bool, pub update_process_gui: bool,
// Positioning // Positioning
pub scroll_direction: ScrollDirection, pub scroll_direction: ScrollDirection,
pub currently_selected_process_position: i64, pub currently_selected_process_position: u64,
pub currently_selected_disk_position: i64, pub currently_selected_disk_position: u64,
pub currently_selected_temperature_position: i64, pub currently_selected_temperature_position: u64,
pub currently_selected_cpu_table_position: i64, pub currently_selected_cpu_table_position: u64,
pub previous_disk_position: i64, pub previous_disk_position: u64,
pub previous_temp_position: i64, pub previous_temp_position: u64,
pub previous_process_position: i64, pub previous_process_position: u64,
pub previous_cpu_table_position: i64, pub previous_cpu_table_position: u64,
pub temperature_type: temperature::TemperatureType, pub temperature_type: temperature::TemperatureType,
pub update_rate_in_milliseconds: u64, pub update_rate_in_milliseconds: u64,
pub show_average_cpu: bool, pub show_average_cpu: bool,
@ -393,7 +393,7 @@ impl App {
self.second_char = ' '; self.second_char = ' ';
if self.currently_selected_process_position if self.currently_selected_process_position
< self.canvas_data.finalized_process_data.len() as i64 < self.canvas_data.finalized_process_data.len() as u64
{ {
let current_process = if self.is_grouped() { let current_process = if self.is_grouped() {
let group_pids = &self.canvas_data.finalized_process_data let group_pids = &self.canvas_data.finalized_process_data
@ -624,19 +624,19 @@ impl App {
match self.current_widget_selected { match self.current_widget_selected {
WidgetPosition::Process => { WidgetPosition::Process => {
self.currently_selected_process_position = self.currently_selected_process_position =
self.canvas_data.finalized_process_data.len() as i64 - 1 self.canvas_data.finalized_process_data.len() as u64 - 1
} }
WidgetPosition::Temp => { WidgetPosition::Temp => {
self.currently_selected_temperature_position = self.currently_selected_temperature_position =
self.canvas_data.temp_sensor_data.len() as i64 - 1 self.canvas_data.temp_sensor_data.len() as u64 - 1
} }
WidgetPosition::Disk => { WidgetPosition::Disk => {
self.currently_selected_disk_position = self.currently_selected_disk_position =
self.canvas_data.disk_data.len() as i64 - 1 self.canvas_data.disk_data.len() as u64 - 1
} }
WidgetPosition::Cpu => { WidgetPosition::Cpu => {
self.currently_selected_cpu_table_position = self.currently_selected_cpu_table_position =
self.canvas_data.cpu_data.len() as i64 - 1; self.canvas_data.cpu_data.len() as u64 - 1;
} }
_ => {} _ => {}
} }
@ -674,38 +674,42 @@ impl App {
} }
fn change_cpu_table_position(&mut self, num_to_change_by: i64) { fn change_cpu_table_position(&mut self, num_to_change_by: i64) {
if self.currently_selected_cpu_table_position + num_to_change_by >= 0 if self.currently_selected_cpu_table_position as i64 + num_to_change_by >= 0
&& self.currently_selected_cpu_table_position + num_to_change_by && self.currently_selected_cpu_table_position as i64 + num_to_change_by
< self.canvas_data.cpu_data.len() as i64 < self.canvas_data.cpu_data.len() as i64
{ {
self.currently_selected_cpu_table_position += num_to_change_by; self.currently_selected_cpu_table_position =
(self.currently_selected_cpu_table_position as i64 + num_to_change_by) as u64;
} }
} }
fn change_process_position(&mut self, num_to_change_by: i64) { fn change_process_position(&mut self, num_to_change_by: i64) {
if self.currently_selected_process_position + num_to_change_by >= 0 if self.currently_selected_process_position as i64 + num_to_change_by >= 0
&& self.currently_selected_process_position + num_to_change_by && self.currently_selected_process_position as i64 + num_to_change_by
< self.canvas_data.finalized_process_data.len() as i64 < self.canvas_data.finalized_process_data.len() as i64
{ {
self.currently_selected_process_position += num_to_change_by; self.currently_selected_process_position =
(self.currently_selected_process_position as i64 + num_to_change_by) as u64;
} }
} }
fn change_temp_position(&mut self, num_to_change_by: i64) { fn change_temp_position(&mut self, num_to_change_by: i64) {
if self.currently_selected_temperature_position + num_to_change_by >= 0 if self.currently_selected_temperature_position as i64 + num_to_change_by >= 0
&& self.currently_selected_temperature_position + num_to_change_by && self.currently_selected_temperature_position as i64 + num_to_change_by
< self.canvas_data.temp_sensor_data.len() as i64 < self.canvas_data.temp_sensor_data.len() as i64
{ {
self.currently_selected_temperature_position += num_to_change_by; self.currently_selected_temperature_position =
(self.currently_selected_temperature_position as i64 + num_to_change_by) as u64;
} }
} }
fn change_disk_position(&mut self, num_to_change_by: i64) { fn change_disk_position(&mut self, num_to_change_by: i64) {
if self.currently_selected_disk_position + num_to_change_by >= 0 if self.currently_selected_disk_position as i64 + num_to_change_by >= 0
&& self.currently_selected_disk_position + num_to_change_by && self.currently_selected_disk_position as i64 + num_to_change_by
< self.canvas_data.disk_data.len() as i64 < self.canvas_data.disk_data.len() as i64
{ {
self.currently_selected_disk_position += num_to_change_by; self.currently_selected_disk_position =
(self.currently_selected_disk_position as i64 + num_to_change_by) as u64;
} }
} }
} }

View File

@ -461,7 +461,7 @@ fn draw_cpu_legend<B: backend::Backend>(
) { ) {
let cpu_data: &[ConvertedCpuData] = &(app_state.canvas_data.cpu_data); let cpu_data: &[ConvertedCpuData] = &(app_state.canvas_data.cpu_data);
let num_rows = i64::from(draw_loc.height) - 5; let num_rows = u64::from(draw_loc.height) - 5;
let start_position = get_start_position( let start_position = get_start_position(
num_rows, num_rows,
&(app_state.scroll_direction), &(app_state.scroll_direction),
@ -481,7 +481,7 @@ fn draw_cpu_legend<B: backend::Backend>(
} }
} }
let mut cpu_row_counter = 0; let mut cpu_row_counter: i64 = 0;
let cpu_rows = stringified_cpu_data let cpu_rows = stringified_cpu_data
.iter() .iter()
@ -491,7 +491,7 @@ fn draw_cpu_legend<B: backend::Backend>(
cpu_string_row.iter(), cpu_string_row.iter(),
match app_state.current_widget_selected { match app_state.current_widget_selected {
app::WidgetPosition::Cpu => { app::WidgetPosition::Cpu => {
if cpu_row_counter if cpu_row_counter as u64
== app_state.currently_selected_cpu_table_position - start_position == app_state.currently_selected_cpu_table_position - start_position
{ {
cpu_row_counter = -1; cpu_row_counter = -1;
@ -741,7 +741,7 @@ fn draw_temp_table<B: backend::Backend>(
) { ) {
let temp_sensor_data: &[Vec<String>] = &(app_state.canvas_data.temp_sensor_data); let temp_sensor_data: &[Vec<String>] = &(app_state.canvas_data.temp_sensor_data);
let num_rows = i64::from(draw_loc.height) - 5; let num_rows = u64::from(draw_loc.height) - 5;
let start_position = get_start_position( let start_position = get_start_position(
num_rows, num_rows,
&(app_state.scroll_direction), &(app_state.scroll_direction),
@ -750,14 +750,14 @@ fn draw_temp_table<B: backend::Backend>(
); );
let sliced_vec: Vec<Vec<String>> = (&temp_sensor_data[start_position as usize..]).to_vec(); let sliced_vec: Vec<Vec<String>> = (&temp_sensor_data[start_position as usize..]).to_vec();
let mut temp_row_counter = 0; let mut temp_row_counter: i64 = 0;
let temperature_rows = sliced_vec.iter().map(|temp_row| { let temperature_rows = sliced_vec.iter().map(|temp_row| {
Row::StyledData( Row::StyledData(
temp_row.iter(), temp_row.iter(),
match app_state.current_widget_selected { match app_state.current_widget_selected {
app::WidgetPosition::Temp => { app::WidgetPosition::Temp => {
if temp_row_counter if temp_row_counter as u64
== app_state.currently_selected_temperature_position - start_position == app_state.currently_selected_temperature_position - start_position
{ {
temp_row_counter = -1; temp_row_counter = -1;
@ -807,7 +807,7 @@ fn draw_disk_table<B: backend::Backend>(
f: &mut Frame<B>, app_state: &mut app::App, draw_loc: Rect, f: &mut Frame<B>, app_state: &mut app::App, draw_loc: Rect,
) { ) {
let disk_data: &[Vec<String>] = &(app_state.canvas_data.disk_data); let disk_data: &[Vec<String>] = &(app_state.canvas_data.disk_data);
let num_rows = i64::from(draw_loc.height) - 5; let num_rows = u64::from(draw_loc.height) - 5;
let start_position = get_start_position( let start_position = get_start_position(
num_rows, num_rows,
&(app_state.scroll_direction), &(app_state.scroll_direction),
@ -816,14 +816,16 @@ fn draw_disk_table<B: backend::Backend>(
); );
let sliced_vec: Vec<Vec<String>> = (&disk_data[start_position as usize..]).to_vec(); let sliced_vec: Vec<Vec<String>> = (&disk_data[start_position as usize..]).to_vec();
let mut disk_counter = 0; let mut disk_counter: i64 = 0;
let disk_rows = sliced_vec.iter().map(|disk| { let disk_rows = sliced_vec.iter().map(|disk| {
Row::StyledData( Row::StyledData(
disk.iter(), disk.iter(),
match app_state.current_widget_selected { match app_state.current_widget_selected {
app::WidgetPosition::Disk => { app::WidgetPosition::Disk => {
if disk_counter == app_state.currently_selected_disk_position - start_position { if disk_counter as u64
== app_state.currently_selected_disk_position - start_position
{
disk_counter = -1; disk_counter = -1;
Style::default().fg(Color::Black).bg(Color::Cyan) Style::default().fg(Color::Black).bg(Color::Cyan)
} else { } else {
@ -956,7 +958,7 @@ fn draw_processes_table<B: backend::Backend>(
// hit the process we've currently scrolled to. // hit the process we've currently scrolled to.
// We also need to move the list - we can // We also need to move the list - we can
// do so by hiding some elements! // do so by hiding some elements!
let num_rows = i64::from(draw_loc.height) - 5; let num_rows = u64::from(draw_loc.height) - 5;
let position = get_start_position( let position = get_start_position(
num_rows, num_rows,
@ -966,14 +968,14 @@ fn draw_processes_table<B: backend::Backend>(
); );
// Sanity check // Sanity check
let start_position = if position >= process_data.len() as i64 { let start_position = if position >= process_data.len() as u64 {
std::cmp::max(0, process_data.len() as i64 - 1) std::cmp::max(0, process_data.len() as i64 - 1) as u64
} else { } else {
position position
}; };
let sliced_vec: Vec<ConvertedProcessData> = (&process_data[start_position as usize..]).to_vec(); let sliced_vec: Vec<ConvertedProcessData> = (&process_data[start_position as usize..]).to_vec();
let mut process_counter = 0; let mut process_counter: i64 = 0;
// Draw! // Draw!
let process_rows = sliced_vec.iter().map(|process| { let process_rows = sliced_vec.iter().map(|process| {
@ -991,7 +993,7 @@ fn draw_processes_table<B: backend::Backend>(
stringified_process_vec.into_iter(), stringified_process_vec.into_iter(),
match app_state.current_widget_selected { match app_state.current_widget_selected {
app::WidgetPosition::Process => { app::WidgetPosition::Process => {
if process_counter if process_counter as u64
== app_state.currently_selected_process_position - start_position == app_state.currently_selected_process_position - start_position
{ {
process_counter = -1; process_counter = -1;
@ -1138,9 +1140,9 @@ fn get_variable_intrinsic_widths(
} }
fn get_start_position( fn get_start_position(
num_rows: i64, scroll_direction: &app::ScrollDirection, previously_scrolled_position: &mut i64, num_rows: u64, scroll_direction: &app::ScrollDirection, previously_scrolled_position: &mut u64,
currently_selected_position: i64, currently_selected_position: u64,
) -> i64 { ) -> u64 {
match scroll_direction { match scroll_direction {
app::ScrollDirection::DOWN => { app::ScrollDirection::DOWN => {
if currently_selected_position < *previously_scrolled_position + num_rows { if currently_selected_position < *previously_scrolled_position + num_rows {

View File

@ -9,8 +9,8 @@ extern crate lazy_static;
use crossterm::{ use crossterm::{
event::{ event::{
self, DisableMouseCapture, EnableMouseCapture, Event as CEvent, KeyCode, KeyModifiers, poll, read, DisableMouseCapture, EnableMouseCapture, Event as CEvent, KeyCode,
MouseEvent, KeyModifiers, MouseEvent,
}, },
execute, execute,
terminal::LeaveAlternateScreen, terminal::LeaveAlternateScreen,
@ -148,25 +148,29 @@ fn main() -> error::Result<()> {
let (tx, rx) = mpsc::channel(); let (tx, rx) = mpsc::channel();
{ {
let tx = tx.clone(); let tx = tx.clone();
thread::spawn(move || { thread::spawn(move || loop {
let mut mouse_timer = Instant::now(); if poll(Duration::from_millis(20)).is_ok() {
let mut keyboard_timer = Instant::now(); let mut mouse_timer = Instant::now();
let mut keyboard_timer = Instant::now();
loop { loop {
if let Ok(event) = event::read() { if poll(Duration::from_millis(20)).is_ok() {
if let CEvent::Key(key) = event { if let Ok(event) = read() {
if Instant::now().duration_since(keyboard_timer).as_millis() >= 20 { if let CEvent::Key(key) = event {
if tx.send(Event::KeyInput(key)).is_err() { if Instant::now().duration_since(keyboard_timer).as_millis() >= 20 {
return; if tx.send(Event::KeyInput(key)).is_err() {
return;
}
keyboard_timer = Instant::now();
}
} else if let CEvent::Mouse(mouse) = event {
if Instant::now().duration_since(mouse_timer).as_millis() >= 20 {
if tx.send(Event::MouseInput(mouse)).is_err() {
return;
}
mouse_timer = Instant::now();
}
} }
keyboard_timer = Instant::now();
}
} else if let CEvent::Mouse(mouse) = event {
if Instant::now().duration_since(mouse_timer).as_millis() >= 20 {
if tx.send(Event::MouseInput(mouse)).is_err() {
return;
}
mouse_timer = Instant::now();
} }
} }
} }