Update tui version... legends aren't showing up yet, will have to fork again.

This commit is contained in:
ClementTsang 2019-12-06 00:57:04 -05:00
parent 078cca4100
commit e7477ce517
13 changed files with 257 additions and 313 deletions

View File

@ -17,7 +17,7 @@ path = "src/main.rs"
[dependencies] [dependencies]
chrono = "0.4.9" chrono = "0.4.9"
clap = "2.33.0" clap = "2.33.0"
crossterm = "^0.10" crossterm = "0.13"
failure = "0.1.5" failure = "0.1.5"
futures-preview = "0.3.0-alpha.18" futures-preview = "0.3.0-alpha.18"
fern = "0.5" fern = "0.5"
@ -32,10 +32,8 @@ sysinfo = "0.9.4"
tokio = "0.2.0-alpha.4" tokio = "0.2.0-alpha.4"
winapi = "0.3.8" winapi = "0.3.8"
[dependencies.tui-temp-fork] [dependencies.tui]
#git = "https://github.com/ClementTsang/tui-rs" version = "0.7"
path = "../tui-rs"
version = "0.6"
default-features = false default-features = false
features = ['crossterm'] features = ['crossterm']

View File

@ -1,4 +1,5 @@
max_width = 150 max_width = 150
newline_style = "Unix"
reorder_imports = true reorder_imports = true
control_brace_style = "ClosingNextLine" control_brace_style = "ClosingNextLine"
fn_args_layout = "Compressed" fn_args_layout = "Compressed"

View File

@ -69,8 +69,7 @@ impl App {
} }
} }
pub fn on_enter(&mut self) { pub fn on_enter(&mut self) {}
}
pub fn on_esc(&mut self) { pub fn on_esc(&mut self) {
if self.show_help { if self.show_help {
@ -86,8 +85,7 @@ impl App {
if self.awaiting_second_d { if self.awaiting_second_d {
self.awaiting_second_d = false; self.awaiting_second_d = false;
self.kill_highlighted_process().unwrap_or(()); self.kill_highlighted_process().unwrap_or(());
} } else {
else {
self.awaiting_second_d = true; self.awaiting_second_d = true;
} }
} }

View File

@ -69,8 +69,7 @@ fn vangelis_cpu_usage_calculation(prev_idle : &mut f64, prev_non_idle : &mut f64
let result = if total_delta - idle_delta != 0_f64 { let result = if total_delta - idle_delta != 0_f64 {
total_delta - idle_delta total_delta - idle_delta
} } else {
else {
1_f64 1_f64
}; };
@ -83,20 +82,16 @@ fn get_ordering<T : std::cmp::PartialOrd>(a_val : T, b_val : T, reverse_order :
if a_val > b_val { if a_val > b_val {
if reverse_order { if reverse_order {
std::cmp::Ordering::Less std::cmp::Ordering::Less
} } else {
else {
std::cmp::Ordering::Greater std::cmp::Ordering::Greater
} }
} } else if a_val < b_val {
else if a_val < b_val {
if reverse_order { if reverse_order {
std::cmp::Ordering::Greater std::cmp::Ordering::Greater
} } else {
else {
std::cmp::Ordering::Less std::cmp::Ordering::Less
} }
} } else {
else {
std::cmp::Ordering::Equal std::cmp::Ordering::Equal
} }
} }
@ -118,14 +113,11 @@ fn get_process_cpu_stats(pid : u32) -> std::io::Result<f64> {
} }
/// Note that cpu_percentage should be represented WITHOUT the \times 100 factor! /// Note that cpu_percentage should be represented WITHOUT the \times 100 factor!
fn linux_cpu_usage( fn linux_cpu_usage(pid: u32, cpu_usage: f64, cpu_percentage: f64, previous_pid_stats: &mut HashMap<String, (f64, Instant)>) -> std::io::Result<f64> {
pid : u32, cpu_usage : f64, cpu_percentage : f64, previous_pid_stats : &mut HashMap<String, (f64, Instant)>,
) -> std::io::Result<f64> {
// Based heavily on https://stackoverflow.com/a/23376195 and https://stackoverflow.com/a/1424556 // Based heavily on https://stackoverflow.com/a/23376195 and https://stackoverflow.com/a/1424556
let before_proc_val: f64 = if previous_pid_stats.contains_key(&pid.to_string()) { let before_proc_val: f64 = if previous_pid_stats.contains_key(&pid.to_string()) {
previous_pid_stats.get(&pid.to_string()).unwrap_or(&(0_f64, Instant::now())).0 previous_pid_stats.get(&pid.to_string()).unwrap_or(&(0_f64, Instant::now())).0
} } else {
else {
0_f64 0_f64
}; };
let after_proc_val = get_process_cpu_stats(pid)?; let after_proc_val = get_process_cpu_stats(pid)?;
@ -192,8 +184,7 @@ pub async fn get_sorted_processes_list(
} }
} }
} }
} } else if cfg!(target_os = "windows") {
else if cfg!(target_os = "windows") {
// Windows // Windows
let process_hashmap = sys.get_process_list(); let process_hashmap = sys.get_process_list();
@ -206,12 +197,10 @@ pub async fn get_sorted_processes_list(
cpu_usage_percent: f64::from(process_val.cpu_usage()), cpu_usage_percent: f64::from(process_val.cpu_usage()),
}); });
} }
} } else if cfg!(target_os = "macos") {
else if cfg!(target_os = "macos") {
// TODO: macOS // TODO: macOS
debug!("Mac"); debug!("Mac");
} } else {
else {
// TODO: Others? // TODO: Others?
debug!("Else"); debug!("Else");
// Solaris: https://stackoverflow.com/a/4453581 // Solaris: https://stackoverflow.com/a/4453581

View File

@ -37,19 +37,16 @@ pub fn kill_process_given_pid(pid : u64) -> crate::utils::error::Result<()> {
if cfg!(target_os = "linux") { if cfg!(target_os = "linux") {
// Linux // Linux
Command::new("kill").arg(pid.to_string()).output()?; Command::new("kill").arg(pid.to_string()).output()?;
} } else if cfg!(target_os = "windows") {
else if cfg!(target_os = "windows") {
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
let process = Process::open(pid as DWORD)?; let process = Process::open(pid as DWORD)?;
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
process.kill()?; process.kill()?;
} } else if cfg!(target_os = "macos") {
else if cfg!(target_os = "macos") {
// TODO: macOS // TODO: macOS
// See how sysinfo does it... https://docs.rs/sysinfo/0.9.5/sysinfo/trait.ProcessExt.html // See how sysinfo does it... https://docs.rs/sysinfo/0.9.5/sysinfo/trait.ProcessExt.html
debug!("Sorry, macOS support is not implemented yet!"); debug!("Sorry, macOS support is not implemented yet!");
} } else {
else {
// TODO: Others? // TODO: Others?
debug!("Sorry, other support this is not implemented yet!"); debug!("Sorry, other support this is not implemented yet!");
} }

View File

@ -1,5 +1,5 @@
use crate::{app, constants, utils::error}; use crate::{app, constants, utils::error};
use tui_temp_fork::{ use tui::{
backend, backend,
layout::{Alignment, Constraint, Direction, Layout}, layout::{Alignment, Constraint, Direction, Layout},
style::{Color, Modifier, Style}, style::{Color, Modifier, Style},
@ -80,8 +80,7 @@ pub fn draw_data<B : backend::Backend>(terminal : &mut Terminal<B>, app_state :
.alignment(Alignment::Left) .alignment(Alignment::Left)
.wrap(true) .wrap(true)
.render(&mut f, middle_dialog_chunk[1]); .render(&mut f, middle_dialog_chunk[1]);
} } else {
else {
let vertical_chunks = Layout::default() let vertical_chunks = Layout::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.margin(1) .margin(1)
@ -125,8 +124,7 @@ pub fn draw_data<B : backend::Backend>(terminal : &mut Terminal<B>, app_state :
if i == 0 { if i == 0 {
// Skip, we want to render the average cpu last! // Skip, we want to render the average cpu last!
continue; continue;
} } else {
else {
avg_cpu_exist_offset = 1; avg_cpu_exist_offset = 1;
} }
} }
@ -247,8 +245,7 @@ pub fn draw_data<B : backend::Backend>(terminal : &mut Terminal<B>, app_state :
if disk_counter == app_state.currently_selected_temperature_position - start_position { if disk_counter == app_state.currently_selected_temperature_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 {
if disk_counter >= 0 { if disk_counter >= 0 {
disk_counter += 1; disk_counter += 1;
} }
@ -292,8 +289,7 @@ pub fn draw_data<B : backend::Backend>(terminal : &mut Terminal<B>, app_state :
if disk_counter == app_state.currently_selected_disk_position - start_position { if disk_counter == 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 {
if disk_counter >= 0 { if disk_counter >= 0 {
disk_counter += 1; disk_counter += 1;
} }
@ -393,8 +389,7 @@ pub fn draw_data<B : backend::Backend>(terminal : &mut Terminal<B>, app_state :
if process_counter == app_state.currently_selected_process_position - start_position { if process_counter == app_state.currently_selected_process_position - start_position {
process_counter = -1; process_counter = -1;
Style::default().fg(Color::Black).bg(Color::Cyan) Style::default().fg(Color::Black).bg(Color::Cyan)
} } else {
else {
if process_counter >= 0 { if process_counter >= 0 {
process_counter += 1; process_counter += 1;
} }
@ -412,8 +407,7 @@ pub fn draw_data<B : backend::Backend>(terminal : &mut Terminal<B>, app_state :
let direction_val = if app_state.process_sorting_reverse { let direction_val = if app_state.process_sorting_reverse {
"".to_string() "".to_string()
} } else {
else {
"".to_string() "".to_string()
}; };
@ -459,11 +453,9 @@ fn get_start_position(
app::ScrollDirection::DOWN => { app::ScrollDirection::DOWN => {
if *currently_selected_position < num_rows { if *currently_selected_position < num_rows {
0 0
} } else if *currently_selected_position - num_rows < *previous_position {
else if *currently_selected_position - num_rows < *previous_position {
*previous_position *previous_position
} } else {
else {
*previous_position = *currently_selected_position - num_rows + 1; *previous_position = *currently_selected_position - num_rows + 1;
*previous_position *previous_position
} }
@ -472,8 +464,7 @@ fn get_start_position(
if *currently_selected_position == *previous_position - 1 { if *currently_selected_position == *previous_position - 1 {
*previous_position = if *previous_position > 0 { *previous_position - 1 } else { 0 }; *previous_position = if *previous_position > 0 { *previous_position - 1 } else { 0 };
*previous_position *previous_position
} } else {
else {
*previous_position *previous_position
} }
} }

View File

@ -6,8 +6,7 @@ pub fn update_temp_row(app_data : &data_collection::Data, temp_type : &data_coll
if (&app_data.list_of_temperature_sensor).is_empty() { if (&app_data.list_of_temperature_sensor).is_empty() {
sensor_vector.push(vec!["No Sensors Found".to_string(), "".to_string()]) sensor_vector.push(vec!["No Sensors Found".to_string(), "".to_string()])
} } else {
else {
for sensor in &app_data.list_of_temperature_sensor { for sensor in &app_data.list_of_temperature_sensor {
sensor_vector.push(vec![ sensor_vector.push(vec![
sensor.component_name.to_string(), sensor.component_name.to_string(),
@ -44,37 +43,29 @@ pub fn update_disk_row(app_data : &data_collection::Data) -> Vec<Vec<String>> {
( (
if read_bytes_per_sec < 1024 { if read_bytes_per_sec < 1024 {
format!("{}B", read_bytes_per_sec) format!("{}B", read_bytes_per_sec)
} } else if read_bytes_per_sec < 1024 * 1024 {
else if read_bytes_per_sec < 1024 * 1024 {
format!("{}KB", read_bytes_per_sec / 1024) format!("{}KB", read_bytes_per_sec / 1024)
} } else {
else {
format!("{}MB", read_bytes_per_sec / 1024 / 1024) format!("{}MB", read_bytes_per_sec / 1024 / 1024)
}, },
if write_bytes_per_sec < 1024 { if write_bytes_per_sec < 1024 {
format!("{}B", write_bytes_per_sec) format!("{}B", write_bytes_per_sec)
} } else if write_bytes_per_sec < 1024 * 1024 {
else if write_bytes_per_sec < 1024 * 1024 {
format!("{}KB", write_bytes_per_sec / 1024) format!("{}KB", write_bytes_per_sec / 1024)
} } else {
else {
format!("{}MB", write_bytes_per_sec / 1024 / 1024) format!("{}MB", write_bytes_per_sec / 1024 / 1024)
}, },
) )
} } else {
else {
("0B".to_string(), "0B".to_string()) ("0B".to_string(), "0B".to_string())
} }
} } else {
else {
("0B".to_string(), "0B".to_string()) ("0B".to_string(), "0B".to_string())
} }
} } else {
else {
("0B".to_string(), "0B".to_string()) ("0B".to_string(), "0B".to_string())
} }
} } else {
else {
("0B".to_string(), "0B".to_string()) ("0B".to_string(), "0B".to_string())
}; };
disk_vector.push(vec![ disk_vector.push(vec![
@ -83,14 +74,12 @@ pub fn update_disk_row(app_data : &data_collection::Data) -> Vec<Vec<String>> {
format!("{:.0}%", disk.used_space as f64 / disk.total_space as f64 * 100_f64), format!("{:.0}%", disk.used_space as f64 / disk.total_space as f64 * 100_f64),
if disk.free_space < 1024 { if disk.free_space < 1024 {
disk.free_space.to_string() + "MB" disk.free_space.to_string() + "MB"
} } else {
else {
(disk.free_space / 1024).to_string() + "GB" (disk.free_space / 1024).to_string() + "GB"
}, },
if disk.total_space < 1024 { if disk.total_space < 1024 {
disk.total_space.to_string() + "MB" disk.total_space.to_string() + "MB"
} } else {
else {
(disk.total_space / 1024).to_string() + "GB" (disk.total_space / 1024).to_string() + "GB"
}, },
io_activity.0, io_activity.0,
@ -113,16 +102,13 @@ pub fn update_process_row(app_data : &data_collection::Data) -> Vec<Vec<String>>
"{:.1}%", "{:.1}%",
if let Some(mem_usage) = process.mem_usage_percent { if let Some(mem_usage) = process.mem_usage_percent {
mem_usage mem_usage
} } else if let Some(mem_usage_kb) = process.mem_usage_kb {
else if let Some(mem_usage_kb) = process.mem_usage_kb {
if let Some(mem_data) = app_data.memory.last() { if let Some(mem_data) = app_data.memory.last() {
(mem_usage_kb / 1024) as f64 / mem_data.mem_total_in_mb as f64 * 100_f64 (mem_usage_kb / 1024) as f64 / mem_data.mem_total_in_mb as f64 * 100_f64
} } else {
else {
0_f64 0_f64
} }
} } else {
else {
0_f64 0_f64
} }
), ),
@ -224,8 +210,7 @@ fn convert_mem_data(mem_data : &[data_collection::mem::MemData]) -> Vec<(f64, f6
((TIME_STARTS_FROM as f64 - current_time.duration_since(data.instant).as_millis() as f64) * 10_f64).floor(), ((TIME_STARTS_FROM as f64 - current_time.duration_since(data.instant).as_millis() as f64) * 10_f64).floor(),
if data.mem_total_in_mb == 0 { if data.mem_total_in_mb == 0 {
-1000.0 -1000.0
} } else {
else {
data.mem_used_in_mb as f64 / data.mem_total_in_mb as f64 * 100_f64 data.mem_used_in_mb as f64 / data.mem_total_in_mb as f64 * 100_f64
}, },
); );
@ -303,18 +288,14 @@ pub fn convert_network_data_points(network_data : &[data_collection::network::Ne
let num_bytes = last_num_bytes_entry.rx; let num_bytes = last_num_bytes_entry.rx;
if num_bytes < 1024 { if num_bytes < 1024 {
format!("RX: {:4} B", num_bytes).to_string() format!("RX: {:4} B", num_bytes).to_string()
} } else if num_bytes < (1024 * 1024) {
else if num_bytes < (1024 * 1024) {
format!("RX: {:4}KB", num_bytes / 1024).to_string() format!("RX: {:4}KB", num_bytes / 1024).to_string()
} } else if num_bytes < (1024 * 1024 * 1024) {
else if num_bytes < (1024 * 1024 * 1024) {
format!("RX: {:4}MB", num_bytes / 1024 / 1024).to_string() format!("RX: {:4}MB", num_bytes / 1024 / 1024).to_string()
} } else {
else {
format!("RX: {:4}GB", num_bytes / 1024 / 1024 / 1024).to_string() format!("RX: {:4}GB", num_bytes / 1024 / 1024 / 1024).to_string()
} }
} } else {
else {
"0B".to_string() "0B".to_string()
}; };
@ -322,18 +303,14 @@ pub fn convert_network_data_points(network_data : &[data_collection::network::Ne
let num_bytes = last_num_bytes_entry.tx; let num_bytes = last_num_bytes_entry.tx;
if num_bytes < 1024 { if num_bytes < 1024 {
format!("TX: {:4} B", num_bytes).to_string() format!("TX: {:4} B", num_bytes).to_string()
} } else if num_bytes < (1024 * 1024) {
else if num_bytes < (1024 * 1024) {
format!("TX: {:4}KB", num_bytes / 1024).to_string() format!("TX: {:4}KB", num_bytes / 1024).to_string()
} } else if num_bytes < (1024 * 1024 * 1024) {
else if num_bytes < (1024 * 1024 * 1024) {
format!("TX: {:4}MB", num_bytes / 1024 / 1024).to_string() format!("TX: {:4}MB", num_bytes / 1024 / 1024).to_string()
} } else {
else {
format!("TX: {:4}GB", num_bytes / 1024 / 1024 / 1024).to_string() format!("TX: {:4}GB", num_bytes / 1024 / 1024 / 1024).to_string()
} }
} } else {
else {
"0B".to_string() "0B".to_string()
}; };

View File

@ -1,5 +1,3 @@
#![feature(async_closure)]
#[macro_use] #[macro_use]
extern crate log; extern crate log;
#[macro_use] #[macro_use]
@ -7,14 +5,17 @@ extern crate clap;
#[macro_use] #[macro_use]
extern crate failure; extern crate failure;
use crossterm::{input, AlternateScreen, InputEvent, KeyEvent, MouseButton, MouseEvent}; use crossterm::{
input::{input, InputEvent, KeyEvent, MouseButton, MouseEvent},
screen::AlternateScreen,
};
use std::{ use std::{
io::stdout, io::stdout,
sync::mpsc, sync::mpsc,
thread, thread,
time::{Duration, Instant}, time::{Duration, Instant},
}; };
use tui_temp_fork::{backend::CrosstermBackend, Terminal}; use tui::{backend::CrosstermBackend, Terminal};
pub mod app; pub mod app;
mod utils { mod utils {
@ -70,8 +71,7 @@ fn main() -> error::Result<()> {
.value_of("RATE_MILLIS") .value_of("RATE_MILLIS")
.unwrap_or(&constants::DEFAULT_REFRESH_RATE_IN_MILLISECONDS.to_string()) .unwrap_or(&constants::DEFAULT_REFRESH_RATE_IN_MILLISECONDS.to_string())
.parse::<u128>()? .parse::<u128>()?
} } else {
else {
constants::DEFAULT_REFRESH_RATE_IN_MILLISECONDS constants::DEFAULT_REFRESH_RATE_IN_MILLISECONDS
}; };
@ -79,8 +79,7 @@ fn main() -> error::Result<()> {
return Err(RustopError::InvalidArg { return Err(RustopError::InvalidArg {
message: "Please set your update rate to be greater than 250 milliseconds.".to_string(), message: "Please set your update rate to be greater than 250 milliseconds.".to_string(),
}); });
} } else if update_rate_in_milliseconds > u128::from(std::u64::MAX) {
else if update_rate_in_milliseconds > u128::from(std::u64::MAX) {
return Err(RustopError::InvalidArg { return Err(RustopError::InvalidArg {
message: "Please set your update rate to be less than unsigned INT_MAX.".to_string(), message: "Please set your update rate to be less than unsigned INT_MAX.".to_string(),
}); });
@ -88,11 +87,9 @@ fn main() -> error::Result<()> {
let temperature_type = if matches.is_present("FAHRENHEIT") { let temperature_type = if matches.is_present("FAHRENHEIT") {
data_collection::temperature::TemperatureType::Fahrenheit data_collection::temperature::TemperatureType::Fahrenheit
} } else if matches.is_present("KELVIN") {
else if matches.is_present("KELVIN") {
data_collection::temperature::TemperatureType::Kelvin data_collection::temperature::TemperatureType::Kelvin
} } else {
else {
data_collection::temperature::TemperatureType::Celsius data_collection::temperature::TemperatureType::Celsius
}; };
let show_average_cpu = matches.is_present("AVG_CPU"); let show_average_cpu = matches.is_present("AVG_CPU");
@ -144,8 +141,7 @@ fn main() -> error::Result<()> {
_ => {} _ => {}
} }
} }
} } else {
else {
let mut reader = input.read_async(); let mut reader = input.read_async();
loop { loop {
if let Some(event) = reader.next() { if let Some(event) = reader.next() {
@ -200,8 +196,7 @@ fn main() -> error::Result<()> {
// Fix for if you set a really long time for update periods (and just gives a faster first value) // Fix for if you set a really long time for update periods (and just gives a faster first value)
thread::sleep(Duration::from_millis(250)); thread::sleep(Duration::from_millis(250));
first_run = false; first_run = false;
} } else {
else {
thread::sleep(Duration::from_millis(update_rate_in_milliseconds as u64)); thread::sleep(Duration::from_millis(update_rate_in_milliseconds as u64));
} }
} }

View File

@ -9,14 +9,12 @@ pub fn init_logger() -> Result<(), fern::InitError> {
message message
)) ))
}) })
.level(if cfg!(debug_assertions) { log::LevelFilter::Debug } else { log::LevelFilter::Info }) .level(if cfg!(debug_assertions) {
.chain(if cfg!(debug_assertions) { log::LevelFilter::Debug
//std::fs::OpenOptions::new().write(true).create(true).append(false).open("debug.log")? } else {
fern::log_file("debug.log")? log::LevelFilter::Info
}
else {
fern::log_file("logging.log")?
}) })
.chain(fern::log_file("debug.log")?)
.apply()?; .apply()?;
Ok(()) Ok(())