| 1 | use std::{borrow::Cow, num::NonZeroU16, time::Instant}; |
| 2 | |
| 3 | use concat_string::concat_string; |
| 4 | use ratatui::widgets::Row; |
| 5 | |
| 6 | use crate::{ |
| 7 | app::AppConfigFields, |
| 8 | canvas::{ |
| 9 | Painter, |
| 10 | components::data_table::{ |
| 11 | Column, ColumnHeader, DataTable, DataTableColumn, DataTableProps, DataTableStyling, |
| 12 | DataToCell, |
| 13 | }, |
| 14 | }, |
| 15 | collection::cpu::{CpuData, CpuDataType}, |
| 16 | components::time_series::{PercentTimeGraph, TimeseriesConfig}, |
| 17 | options::config::{cpu::CpuDefault, style::Styles}, |
| 18 | }; |
| 19 | |
| 20 | pub enum CpuWidgetColumn { |
| 21 | Cpu, |
| 22 | Use { show_decimal: bool }, |
| 23 | } |
| 24 | |
| 25 | impl ColumnHeader for CpuWidgetColumn { |
| 26 | fn text(&self) -> Cow<'static, str> { |
| 27 | match self { |
| 28 | CpuWidgetColumn::Cpu => "CPU".into(), |
| 29 | CpuWidgetColumn::Use { .. } => "Use".into(), |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | pub enum CpuWidgetTableData { |
| 35 | All, |
| 36 | Entry { data_type: CpuDataType, usage: f32 }, |
| 37 | } |
| 38 | |
| 39 | impl CpuWidgetTableData { |
| 40 | pub fn from_cpu_data(data: &CpuData) -> CpuWidgetTableData { |
| 41 | CpuWidgetTableData::Entry { |
| 42 | data_type: data.data_type, |
| 43 | usage: data.usage, |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | impl DataToCell<CpuWidgetColumn> for CpuWidgetTableData { |
| 49 | fn to_cell_text( |
| 50 | &self, column: &CpuWidgetColumn, calculated_width: NonZeroU16, |
| 51 | ) -> Option<Cow<'static, str>> { |
| 52 | const CPU_TRUNCATE_BREAKPOINT: u16 = 5; |
| 53 | |
| 54 | let calculated_width = calculated_width.get(); |
| 55 | |
| 56 | // This is a bit of a hack, but apparently we can avoid having to do any fancy |
| 57 | // checks of showing the "All" on a specific column if the other is |
| 58 | // hidden by just always showing it on the CPU (first) column - if there |
| 59 | // isn't room for it, it will just collapse down. |
| 60 | // |
| 61 | // This is the same for the use percentages - we just *always* show them, and |
| 62 | // *always* hide the CPU column if it is too small. |
| 63 | match &self { |
| 64 | CpuWidgetTableData::All => match column { |
| 65 | CpuWidgetColumn::Cpu => Some("All".into()), |
| 66 | CpuWidgetColumn::Use { .. } => None, |
| 67 | }, |
| 68 | CpuWidgetTableData::Entry { |
| 69 | data_type, |
| 70 | usage: last_entry, |
| 71 | } => { |
| 72 | if calculated_width == 0 { |
| 73 | None |
| 74 | } else { |
| 75 | match column { |
| 76 | CpuWidgetColumn::Cpu => match data_type { |
| 77 | CpuDataType::Avg => Some("AVG".into()), |
| 78 | CpuDataType::Cpu(index) => { |
| 79 | let index_str = index.to_string(); |
| 80 | let text = if calculated_width < CPU_TRUNCATE_BREAKPOINT { |
| 81 | index_str.into() |
| 82 | } else { |
| 83 | concat_string!("CPU", index_str).into() |
| 84 | }; |
| 85 | |
| 86 | Some(text) |
| 87 | } |
| 88 | }, |
| 89 | CpuWidgetColumn::Use { show_decimal } => Some(if *show_decimal { |
| 90 | format!("{last_entry:.1}%").into() |
| 91 | } else { |
| 92 | format!("{last_entry:.0}%").into() |
| 93 | }), |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | #[inline(always)] |
| 101 | fn style_row<'a>(&self, row: Row<'a>, painter: &Painter) -> Row<'a> { |
| 102 | let style = match self { |
| 103 | CpuWidgetTableData::All => painter.styles.all_cpu_colour, |
| 104 | CpuWidgetTableData::Entry { |
| 105 | data_type, |
| 106 | usage: _, |
| 107 | } => match data_type { |
| 108 | CpuDataType::Avg => painter.styles.avg_cpu_colour, |
| 109 | CpuDataType::Cpu(index) => { |
| 110 | painter.styles.cpu_colour_styles[index % painter.styles.cpu_colour_styles.len()] |
| 111 | } |
| 112 | }, |
| 113 | }; |
| 114 | |
| 115 | row.style(style) |
| 116 | } |
| 117 | |
| 118 | fn column_widths<C: DataTableColumn<CpuWidgetColumn>>( |
| 119 | _data: &[Self], _columns: &[C], |
| 120 | ) -> Vec<u16> |
| 121 | where |
| 122 | Self: Sized, |
| 123 | { |
| 124 | vec![1, 3] |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | pub struct CpuWidgetState { |
| 129 | pub graph: PercentTimeGraph, |
| 130 | pub is_legend_hidden: bool, |
| 131 | pub table: DataTable<CpuWidgetTableData, CpuWidgetColumn>, |
| 132 | pub force_update_data: bool, |
| 133 | } |
| 134 | |
| 135 | impl CpuWidgetState { |
| 136 | pub(crate) fn new( |
| 137 | config: &AppConfigFields, default_selection: CpuDefault, autohide_timer: Option<Instant>, |
| 138 | colours: &Styles, |
| 139 | ) -> Self { |
| 140 | let ts_config = TimeseriesConfig { |
| 141 | time_interval: config.time_interval, |
| 142 | retention_ms: config.retention_ms, |
| 143 | autohide_time: config.autohide_time, |
| 144 | default_time_value: config.default_time_value, |
| 145 | }; |
| 146 | let columns = [ |
| 147 | Column::soft(CpuWidgetColumn::Cpu, Some(0.5)), |
| 148 | Column::soft( |
| 149 | CpuWidgetColumn::Use { |
| 150 | show_decimal: config.show_cpu_decimal, |
| 151 | }, |
| 152 | Some(0.5), |
| 153 | ), |
| 154 | ]; |
| 155 | |
| 156 | let props = DataTableProps { |
| 157 | title: None, |
| 158 | table_gap: config.table_gap, |
| 159 | left_to_right: false, |
| 160 | is_basic: false, |
| 161 | show_table_scroll_position: false, // TODO: Should this be possible? |
| 162 | show_table_scroll_bar: config.show_table_scroll_bar, |
| 163 | show_current_entry_when_unfocused: true, |
| 164 | }; |
| 165 | |
| 166 | let styling = DataTableStyling::from_palette(colours); |
| 167 | let mut table = DataTable::new(columns, props, styling); |
| 168 | match default_selection { |
| 169 | CpuDefault::All => {} |
| 170 | CpuDefault::Average if !config.show_average_cpu => {} |
| 171 | CpuDefault::Average => { |
| 172 | table = table.first_draw_index(1); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | CpuWidgetState { |
| 177 | graph: PercentTimeGraph::new(ts_config, autohide_timer), |
| 178 | is_legend_hidden: false, |
| 179 | table, |
| 180 | force_update_data: false, |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /// Forces an update of the data stored. |
| 185 | #[inline] |
| 186 | pub fn force_data_update(&mut self) { |
| 187 | self.force_update_data = true; |
| 188 | } |
| 189 | |
| 190 | pub fn set_legend_data(&mut self, data: &[CpuData]) { |
| 191 | self.table.set_data( |
| 192 | std::iter::once(CpuWidgetTableData::All) |
| 193 | .chain(data.iter().map(CpuWidgetTableData::from_cpu_data)) |
| 194 | .collect(), |
| 195 | ); |
| 196 | self.force_update_data = false; |
| 197 | } |
| 198 | } |