Skip to content

Commit

Permalink
temp two, remember to squash
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementTsang committed Jul 30, 2022
1 parent e2058f6 commit f2cb21a
Show file tree
Hide file tree
Showing 34 changed files with 1,046 additions and 1,784 deletions.
47 changes: 21 additions & 26 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use layout_manager::*;
pub use states::*;

use crate::{
components::old_text_table::SortState,
constants,
data_conversion::ConvertedData,
options::Config,
Expand Down Expand Up @@ -329,7 +328,7 @@ impl App {
}
}

pub fn toggle_sort(&mut self) {
pub fn toggle_sort_menu(&mut self) {
let widget_id = self.current_widget.widget_id
- match &self.current_widget.widget_type {
BottomWidgetType::Proc => 0,
Expand All @@ -343,10 +342,9 @@ impl App {

// If the sort is now open, move left. Otherwise, if the proc sort was selected, force move right.
if pws.is_sort_open {
if let SortState::Sortable(st) = &pws.table.sort_state {
pws.sort_table
.set_position(st.current_index, pws.num_enabled_columns());
}
pws.sort_table
.set_position(pws.table.sort_index(), pws.num_enabled_columns());

self.move_widget_selection(&WidgetDirection::Left);
} else if let BottomWidgetType::ProcSort = self.current_widget.widget_type {
self.move_widget_selection(&WidgetDirection::Right);
Expand All @@ -365,11 +363,9 @@ impl App {
_ => 0,
};

if let Some(proc_widget_state) = self.proc_state.get_mut_widget_state(widget_id) {
if let SortState::Sortable(state) = &mut proc_widget_state.table.sort_state {
state.toggle_order();
proc_widget_state.force_data_update();
}
if let Some(pws) = self.proc_state.get_mut_widget_state(widget_id) {
pws.table.toggle_order();
pws.force_data_update();
}
}
_ => {}
Expand Down Expand Up @@ -1140,7 +1136,7 @@ impl App {
.widget_states
.get(&self.current_widget.widget_id)
{
if let Some(table_row) = pws.table_data.data.get(pws.table.current_scroll_position) {
if let Some(table_row) = pws.table_data.data.get(pws.table.state.current_index) {
if let Some(col_value) = table_row.row().get(ProcWidget::PROC_NAME_OR_CMD) {
let val = col_value.main_text().to_string();
if pws.is_using_command() {
Expand Down Expand Up @@ -1397,7 +1393,7 @@ impl App {
'-' => self.on_minus(),
'=' => self.reset_zoom(),
'e' => self.toggle_expand_widget(),
's' => self.toggle_sort(),
's' => self.toggle_sort_menu(),
'I' => self.invert_sort(),
'%' => self.toggle_percentages(),
_ => {}
Expand Down Expand Up @@ -1924,8 +1920,7 @@ impl App {
.proc_state
.get_mut_widget_state(self.current_widget.widget_id)
{
proc_widget_state.table.current_scroll_position = 0;
proc_widget_state.table.scroll_direction = ScrollDirection::Up;
proc_widget_state.table.set_first();
}
}
BottomWidgetType::ProcSort => {
Expand Down Expand Up @@ -1979,9 +1974,9 @@ impl App {
.proc_state
.get_mut_widget_state(self.current_widget.widget_id)
{
proc_widget_state.table.current_scroll_position =
proc_widget_state.table_data.data.len().saturating_sub(1);
proc_widget_state.table.scroll_direction = ScrollDirection::Down;
proc_widget_state
.table
.set_last(proc_widget_state.table_data.data.len());
}
}
BottomWidgetType::ProcSort => {
Expand Down Expand Up @@ -2095,7 +2090,7 @@ impl App {
{
proc_widget_state
.table
.update_position(num_to_change_by, proc_widget_state.table_data.data.len())
.increment_position(num_to_change_by, proc_widget_state.table_data.data.len())
} else {
None
}
Expand Down Expand Up @@ -2584,7 +2579,7 @@ impl App {
.get_widget_state(self.current_widget.widget_id)
{
if let Some(visual_index) =
proc_widget_state.table.table_state.selected()
proc_widget_state.table.tui_selected()
{
// If in tree mode, also check to see if this click is on
// the same entry as the already selected one - if it is,
Expand All @@ -2596,7 +2591,7 @@ impl App {
);

let previous_scroll_position =
proc_widget_state.table.current_scroll_position;
proc_widget_state.table.state.current_index;

let new_position = self.change_process_position(
offset_clicked_entry as i64 - visual_index as i64,
Expand Down Expand Up @@ -2680,12 +2675,12 @@ impl App {
.proc_state
.get_mut_widget_state(self.current_widget.widget_id)
{
if let SortState::Sortable(st) =
&mut proc_widget_state.table.sort_state
if proc_widget_state
.table
.try_select_location(x, y)
.is_some()
{
if st.try_select_location(x, y).is_some() {
proc_widget_state.force_data_update();
}
proc_widget_state.force_data_update();
}
}
}
Expand Down
16 changes: 0 additions & 16 deletions src/app/data_farmer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,6 @@ impl ProcessData {

// Reverse as otherwise the pid mappings are in the wrong order.
list_of_processes.iter().rev().for_each(|process_harvest| {
if let Some(entry) = self.name_pid_map.get_mut(&process_harvest.name) {
entry.push(process_harvest.pid);
} else {
self.name_pid_map
.insert(process_harvest.name.to_string(), vec![process_harvest.pid]);
}

if let Some(entry) = self.cmd_pid_map.get_mut(&process_harvest.command) {
entry.push(process_harvest.pid);
} else {
self.cmd_pid_map.insert(
process_harvest.command.to_string(),
vec![process_harvest.pid],
);
}

if let Some(parent_pid) = process_harvest.parent_pid {
if let Some(entry) = self.process_parent_mapping.get_mut(&parent_pid) {
entry.push(process_harvest.pid);
Expand Down
80 changes: 52 additions & 28 deletions src/app/widgets/cpu_graph.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use std::time::Instant;
use std::{borrow::Cow, time::Instant};

use concat_string::concat_string;

use tui::{style::Style, widgets::Row};
use tui::{style::Style, text::Text, widgets::Row};

use crate::{
app::{data_harvester::cpu::CpuDataType, AppConfigFields},
canvas::canvas_colours::CanvasColours,
components::data_table::{Column, DataTable, DataTableProps, DataTableStyling, ToDataRow},
components::data_table::{
Column, ColumnHeader, DataTable, DataTableColumn, DataTableProps, DataTableStyling,
DataToCell,
},
data_conversion::{CpuWidgetData, CpuWidgetDataType},
utils::gen_util::truncate_text,
};
Expand Down Expand Up @@ -35,41 +38,62 @@ impl CpuWidgetStyling {
}
}

impl ToDataRow for CpuWidgetData {
fn to_data_row<'a>(&'a self, widths: &[u16], _columns: &[Column<&'static str>]) -> Row<'a> {
enum CpuWidgetColumn {
CPU,
Use,
}

impl ColumnHeader for CpuWidgetColumn {
fn text(&self) -> Cow<'static, str> {
match self {
CpuWidgetColumn::CPU => "CPU".into(),
CpuWidgetColumn::Use => "Use%".into(),
}
}
}

impl DataToCell<CpuWidgetColumn> for CpuWidgetData {
fn to_cell<'a>(&'a self, column: &CpuWidgetColumn, calculated_width: u16) -> Option<Text<'a>> {
// FIXME: Adjust based on column widths

match &self.data {
CpuWidgetDataType::All => Row::new(vec![truncate_text("All", widths[0].into())]),
CpuWidgetDataType::All => match column {
CpuWidgetColumn::CPU => Some(truncate_text("All", calculated_width)),
CpuWidgetColumn::Use => None,
},
CpuWidgetDataType::Entry {
data_type,
data: _,
data,
last_entry,
} => {
let entry_text = match data_type {
CpuDataType::Avg => truncate_text("AVG", widths[0].into()),
} => match column {
CpuWidgetColumn::CPU => match data_type {
CpuDataType::Avg => Some(truncate_text("AVG", calculated_width)),
CpuDataType::Cpu(index) => {
let index_str = index.to_string();
let width = widths[0].into();
let text = if width < 5 {
truncate_text(&index_str, width)
let text = if calculated_width < 5 {
truncate_text(&index_str, calculated_width)
} else {
truncate_text(&concat_string!("CPU", index_str), width)
truncate_text(&concat_string!("CPU", index_str), calculated_width)
};

text
Some(text)
}
};

Row::new(vec![
entry_text,
truncate_text(&format!("{:.0}%", last_entry.round()), widths[1].into()),
])
}
.style(self.style),
},
CpuWidgetColumn::Use => Some(truncate_text(
&format!("{:.0}%", last_entry.round()),
calculated_width,
)),
},
}
}

fn column_widths(_data: &[CpuWidgetData]) -> Vec<u16>
fn style_row<'a>(&self, row: Row<'a>) -> Row<'a> {
row.style(self.style)
}

fn column_widths<C: DataTableColumn<CpuWidgetColumn>>(
_data: &[Self], _columns: &[C],
) -> Vec<u16>
where
Self: Sized,
{
Expand All @@ -82,7 +106,7 @@ pub struct CpuWidgetState {
pub is_legend_hidden: bool,
pub show_avg: bool,
pub autohide_timer: Option<Instant>,
pub table: DataTable<CpuWidgetData>,
pub table: DataTable<CpuWidgetData, CpuWidgetColumn>,
pub styling: CpuWidgetStyling,
}

Expand All @@ -91,9 +115,9 @@ impl CpuWidgetState {
config: &AppConfigFields, current_display_time: u64, autohide_timer: Option<Instant>,
colours: &CanvasColours,
) -> Self {
const COLUMNS: [Column<&str>; 2] = [
Column::soft("CPU", Some(0.5)),
Column::soft("Use%", Some(0.5)),
const COLUMNS: [Column<CpuWidgetColumn>; 2] = [
Column::soft(CpuWidgetColumn::CPU, Some(0.5)),
Column::soft(CpuWidgetColumn::Use, Some(0.5)),
];

let props = DataTableProps {
Expand Down
82 changes: 58 additions & 24 deletions src/app/widgets/disk_table.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use std::cmp::max;
use std::{borrow::Cow, cmp::max};

use kstring::KString;
use tui::widgets::Row;
use tui::text::Text;

use crate::{
app::AppConfigFields,
canvas::canvas_colours::CanvasColours,
components::data_table::{Column, DataTable, DataTableProps, DataTableStyling, ToDataRow},
components::data_table::{
Column, ColumnHeader, DataTable, DataTableColumn, DataTableProps, DataTableStyling,
DataToCell,
},
utils::gen_util::{get_decimal_bytes, truncate_text},
};

Expand Down Expand Up @@ -52,20 +55,51 @@ impl DiskWidgetData {
}
}

impl ToDataRow for DiskWidgetData {
fn to_data_row<'a>(&'a self, widths: &[u16], _columns: &[Column<&'static str>]) -> Row<'a> {
Row::new(vec![
truncate_text(&self.name, widths[0].into()),
truncate_text(&self.mount_point, widths[1].into()),
truncate_text(&self.free_space(), widths[2].into()),
truncate_text(&self.total_space(), widths[3].into()),
truncate_text(&self.usage(), widths[4].into()),
truncate_text(&self.io_read, widths[5].into()),
truncate_text(&self.io_write, widths[6].into()),
])
enum DiskWidgetColumns {
Disk,
Mount,
Used,
Free,
Total,
IoRead,
IoWrite,
}

impl ColumnHeader for DiskWidgetColumns {
fn text(&self) -> Cow<'static, str> {
match self {
DiskWidgetColumns::Disk => "Disk",
DiskWidgetColumns::Mount => "Mount",
DiskWidgetColumns::Used => "Used",
DiskWidgetColumns::Free => "Free",
DiskWidgetColumns::Total => "Total",
DiskWidgetColumns::IoRead => "R/s",
DiskWidgetColumns::IoWrite => "W/s",
}
.into()
}
}

impl DataToCell<DiskWidgetColumns> for DiskWidgetData {
fn to_cell<'a>(
&'a self, column: &DiskWidgetColumns, calculated_width: u16,
) -> Option<Text<'a>> {
let text = match column {
DiskWidgetColumns::Disk => truncate_text(&self.name, calculated_width),
DiskWidgetColumns::Mount => truncate_text(&self.mount_point, calculated_width),
DiskWidgetColumns::Used => truncate_text(&self.usage(), calculated_width),
DiskWidgetColumns::Free => truncate_text(&self.free_space(), calculated_width),
DiskWidgetColumns::Total => truncate_text(&self.total_space(), calculated_width),
DiskWidgetColumns::IoRead => truncate_text(&self.io_read, calculated_width),
DiskWidgetColumns::IoWrite => truncate_text(&self.io_write, calculated_width),
};

Some(text)
}

fn column_widths(data: &[DiskWidgetData]) -> Vec<u16>
fn column_widths<C: DataTableColumn<DiskWidgetColumns>>(
data: &[Self], _columns: &[C],
) -> Vec<u16>
where
Self: Sized,
{
Expand All @@ -81,19 +115,19 @@ impl ToDataRow for DiskWidgetData {
}

pub struct DiskTableWidget {
pub table: DataTable<DiskWidgetData>,
pub table: DataTable<DiskWidgetData, DiskWidgetColumns>,
}

impl DiskTableWidget {
pub fn new(config: &AppConfigFields, colours: &CanvasColours) -> Self {
const COLUMNS: [Column<&str>; 7] = [
Column::soft("Disk", Some(0.2)),
Column::soft("Mount", Some(0.2)),
Column::hard("Used", 4),
Column::hard("Free", 6),
Column::hard("Total", 6),
Column::hard("R/s", 7),
Column::hard("W/s", 7),
const COLUMNS: [Column<DiskWidgetColumns>; 7] = [
Column::soft(DiskWidgetColumns::Disk, Some(0.2)),
Column::soft(DiskWidgetColumns::Mount, Some(0.2)),
Column::hard(DiskWidgetColumns::Used, 4),
Column::hard(DiskWidgetColumns::Free, 6),
Column::hard(DiskWidgetColumns::Total, 6),
Column::hard(DiskWidgetColumns::IoRead, 7),
Column::hard(DiskWidgetColumns::IoWrite, 7),
];

let props = DataTableProps {
Expand Down
Loading

0 comments on commit f2cb21a

Please sign in to comment.