-
Notifications
You must be signed in to change notification settings - Fork 267
/
Copy pathtemperature_table.rs
132 lines (114 loc) · 3.83 KB
/
temperature_table.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use std::{borrow::Cow, cmp::max, num::NonZeroU16};
use crate::{
app::{AppConfigFields, data::TypedTemperature},
canvas::components::data_table::{
ColumnHeader, DataTableColumn, DataTableProps, DataTableStyling, DataToCell, SortColumn,
SortDataTable, SortDataTableProps, SortOrder, SortsRow,
},
options::config::style::Styles,
utils::general::sort_partial_fn,
};
#[derive(Clone, Debug)]
pub struct TempWidgetData {
pub sensor: String,
pub temperature: Option<TypedTemperature>,
}
pub enum TempWidgetColumn {
Sensor,
Temp,
}
impl ColumnHeader for TempWidgetColumn {
fn text(&self) -> Cow<'static, str> {
match self {
TempWidgetColumn::Sensor => "Sensor(s)".into(),
TempWidgetColumn::Temp => "Temp(t)".into(),
}
}
}
impl TempWidgetData {
pub fn temperature(&self) -> Cow<'static, str> {
match &self.temperature {
Some(temp) => temp.to_string().into(),
None => "N/A".into(),
}
}
}
impl DataToCell<TempWidgetColumn> for TempWidgetData {
fn to_cell(
&self, column: &TempWidgetColumn, _calculated_width: NonZeroU16,
) -> Option<Cow<'static, str>> {
Some(match column {
TempWidgetColumn::Sensor => self.sensor.clone().into(),
TempWidgetColumn::Temp => self.temperature(),
})
}
fn column_widths<C: DataTableColumn<TempWidgetColumn>>(
data: &[TempWidgetData], _columns: &[C],
) -> Vec<u16>
where
Self: Sized,
{
let mut widths = vec![0; 2];
data.iter().for_each(|row| {
widths[0] = max(widths[0], row.sensor.len() as u16);
widths[1] = max(widths[1], row.temperature().len() as u16);
});
widths
}
}
impl SortsRow for TempWidgetColumn {
type DataType = TempWidgetData;
fn sort_data(&self, data: &mut [Self::DataType], descending: bool) {
match self {
TempWidgetColumn::Sensor => {
data.sort_by(move |a, b| sort_partial_fn(descending)(&a.sensor, &b.sensor));
}
TempWidgetColumn::Temp => {
data.sort_by(|a, b| sort_partial_fn(descending)(&a.temperature, &b.temperature));
}
}
}
}
pub struct TempWidgetState {
pub table: SortDataTable<TempWidgetData, TempWidgetColumn>,
pub force_update_data: bool,
}
impl TempWidgetState {
pub(crate) fn new(config: &AppConfigFields, palette: &Styles) -> Self {
let columns = [
SortColumn::soft(TempWidgetColumn::Sensor, Some(0.8)),
SortColumn::soft(TempWidgetColumn::Temp, None).default_descending(),
];
let props = SortDataTableProps {
inner: DataTableProps {
title: Some(" Temperatures ".into()),
table_gap: config.table_gap,
left_to_right: false,
is_basic: config.use_basic_mode,
show_table_scroll_position: config.show_table_scroll_position,
show_current_entry_when_unfocused: false,
},
sort_index: 0,
order: SortOrder::Ascending,
};
let styling = DataTableStyling::from_palette(palette);
Self {
table: SortDataTable::new_sortable(columns, props, styling),
force_update_data: false,
}
}
/// Forces an update of the data stored.
#[inline]
pub fn force_data_update(&mut self) {
self.force_update_data = true;
}
/// Update the current table data.
pub fn set_table_data(&mut self, data: &[TempWidgetData]) {
let mut data = data.to_vec();
if let Some(column) = self.table.columns.get(self.table.sort_index()) {
column.sort_by(&mut data, self.table.order());
}
self.table.set_data(data);
self.force_update_data = false;
}
}