mirror of
https://github.com/ClementTsang/bottom.git
synced 2026-07-08 05:30:47 +00:00
refactor: wrap timeseries state access (#2051)
Wrap the common code around timeseries state access as a helper. Should help with code deduplication.
This commit is contained in:
+41
-148
@@ -19,7 +19,8 @@ use crate::{
|
||||
options::config::flags::TableGap,
|
||||
utils::data_units::DataUnit,
|
||||
widgets::{
|
||||
DiskWidgetColumn, ProcWidgetColumn, ProcWidgetMode, TempWidgetColumn, TreeCollapsed,
|
||||
DiskWidgetColumn, ProcWidgetColumn, ProcWidgetMode, TempWidgetColumn, TimeseriesState,
|
||||
TreeCollapsed,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1986,178 +1987,70 @@ impl App {
|
||||
}
|
||||
}
|
||||
|
||||
fn zoom_out(&mut self) {
|
||||
#[inline]
|
||||
fn current_ts_state(&mut self) -> Option<&mut TimeseriesState> {
|
||||
match self.current_widget.widget_type {
|
||||
BottomWidgetType::Cpu => {
|
||||
BottomWidgetType::Cpu
|
||||
if let Some(widget_state) = self
|
||||
.states
|
||||
.cpu_state
|
||||
.widget_states
|
||||
.get_mut(&self.current_widget.widget_id)
|
||||
{
|
||||
widget_state.time_series_state.zoom_out(
|
||||
self.app_config_fields.time_interval,
|
||||
self.app_config_fields.retention_ms,
|
||||
self.app_config_fields.autohide_time,
|
||||
);
|
||||
}
|
||||
.get_mut_widget_state(self.current_widget.widget_id) =>
|
||||
{
|
||||
Some(&mut widget_state.time_series_state)
|
||||
}
|
||||
BottomWidgetType::Mem => {
|
||||
BottomWidgetType::Mem
|
||||
if let Some(widget_state) = self
|
||||
.states
|
||||
.mem_state
|
||||
.widget_states
|
||||
.get_mut(&self.current_widget.widget_id)
|
||||
{
|
||||
widget_state.time_series_state.zoom_out(
|
||||
self.app_config_fields.time_interval,
|
||||
self.app_config_fields.retention_ms,
|
||||
self.app_config_fields.autohide_time,
|
||||
);
|
||||
}
|
||||
.get_mut_widget_state(self.current_widget.widget_id) =>
|
||||
{
|
||||
Some(&mut widget_state.time_series_state)
|
||||
}
|
||||
BottomWidgetType::Net => {
|
||||
BottomWidgetType::Net
|
||||
if let Some(widget_state) = self
|
||||
.states
|
||||
.net_state
|
||||
.widget_states
|
||||
.get_mut(&self.current_widget.widget_id)
|
||||
{
|
||||
widget_state.time_series_state.zoom_out(
|
||||
self.app_config_fields.time_interval,
|
||||
self.app_config_fields.retention_ms,
|
||||
self.app_config_fields.autohide_time,
|
||||
);
|
||||
}
|
||||
.get_mut_widget_state(self.current_widget.widget_id) =>
|
||||
{
|
||||
Some(&mut widget_state.time_series_state)
|
||||
}
|
||||
BottomWidgetType::TempGraph => {
|
||||
BottomWidgetType::TempGraph
|
||||
if let Some(widget_state) = self
|
||||
.states
|
||||
.temp_graph_state
|
||||
.get_mut_widget_state(self.current_widget.widget_id)
|
||||
{
|
||||
widget_state.time_series_state.zoom_out(
|
||||
self.app_config_fields.time_interval,
|
||||
self.app_config_fields.retention_ms,
|
||||
self.app_config_fields.autohide_time,
|
||||
);
|
||||
}
|
||||
.get_mut_widget_state(self.current_widget.widget_id) =>
|
||||
{
|
||||
Some(&mut widget_state.time_series_state)
|
||||
}
|
||||
_ => {}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn zoom_out(&mut self) {
|
||||
let time_interval = self.app_config_fields.time_interval;
|
||||
let retention_ms = self.app_config_fields.retention_ms;
|
||||
let autohide_time = self.app_config_fields.autohide_time;
|
||||
|
||||
if let Some(ts_state) = self.current_ts_state() {
|
||||
ts_state.zoom_out(time_interval, retention_ms, autohide_time);
|
||||
}
|
||||
}
|
||||
|
||||
fn zoom_in(&mut self) {
|
||||
match self.current_widget.widget_type {
|
||||
BottomWidgetType::Cpu => {
|
||||
if let Some(widget_state) = self
|
||||
.states
|
||||
.cpu_state
|
||||
.widget_states
|
||||
.get_mut(&self.current_widget.widget_id)
|
||||
{
|
||||
widget_state.time_series_state.zoom_in(
|
||||
self.app_config_fields.time_interval,
|
||||
self.app_config_fields.autohide_time,
|
||||
);
|
||||
}
|
||||
}
|
||||
BottomWidgetType::Mem => {
|
||||
if let Some(widget_state) = self
|
||||
.states
|
||||
.mem_state
|
||||
.widget_states
|
||||
.get_mut(&self.current_widget.widget_id)
|
||||
{
|
||||
widget_state.time_series_state.zoom_in(
|
||||
self.app_config_fields.time_interval,
|
||||
self.app_config_fields.autohide_time,
|
||||
);
|
||||
}
|
||||
}
|
||||
BottomWidgetType::Net => {
|
||||
if let Some(widget_state) = self
|
||||
.states
|
||||
.net_state
|
||||
.widget_states
|
||||
.get_mut(&self.current_widget.widget_id)
|
||||
{
|
||||
widget_state.time_series_state.zoom_in(
|
||||
self.app_config_fields.time_interval,
|
||||
self.app_config_fields.autohide_time,
|
||||
);
|
||||
}
|
||||
}
|
||||
BottomWidgetType::TempGraph => {
|
||||
if let Some(widget_state) = self
|
||||
.states
|
||||
.temp_graph_state
|
||||
.get_mut_widget_state(self.current_widget.widget_id)
|
||||
{
|
||||
widget_state.time_series_state.zoom_in(
|
||||
self.app_config_fields.time_interval,
|
||||
self.app_config_fields.autohide_time,
|
||||
);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
let time_interval = self.app_config_fields.time_interval;
|
||||
let autohide_time = self.app_config_fields.autohide_time;
|
||||
|
||||
if let Some(ts_state) = self.current_ts_state() {
|
||||
ts_state.zoom_in(time_interval, autohide_time);
|
||||
}
|
||||
}
|
||||
|
||||
fn reset_zoom(&mut self) {
|
||||
match self.current_widget.widget_type {
|
||||
BottomWidgetType::Cpu => {
|
||||
if let Some(widget_state) = self
|
||||
.states
|
||||
.cpu_state
|
||||
.widget_states
|
||||
.get_mut(&self.current_widget.widget_id)
|
||||
{
|
||||
widget_state.time_series_state.reset_zoom(
|
||||
self.app_config_fields.default_time_value,
|
||||
self.app_config_fields.autohide_time,
|
||||
);
|
||||
}
|
||||
}
|
||||
BottomWidgetType::Mem => {
|
||||
if let Some(widget_state) = self
|
||||
.states
|
||||
.mem_state
|
||||
.widget_states
|
||||
.get_mut(&self.current_widget.widget_id)
|
||||
{
|
||||
widget_state.time_series_state.reset_zoom(
|
||||
self.app_config_fields.default_time_value,
|
||||
self.app_config_fields.autohide_time,
|
||||
);
|
||||
}
|
||||
}
|
||||
BottomWidgetType::Net => {
|
||||
if let Some(widget_state) = self
|
||||
.states
|
||||
.net_state
|
||||
.widget_states
|
||||
.get_mut(&self.current_widget.widget_id)
|
||||
{
|
||||
widget_state.time_series_state.reset_zoom(
|
||||
self.app_config_fields.default_time_value,
|
||||
self.app_config_fields.autohide_time,
|
||||
);
|
||||
}
|
||||
}
|
||||
BottomWidgetType::TempGraph => {
|
||||
if let Some(widget_state) = self
|
||||
.states
|
||||
.temp_graph_state
|
||||
.get_mut_widget_state(self.current_widget.widget_id)
|
||||
{
|
||||
widget_state.time_series_state.reset_zoom(
|
||||
self.app_config_fields.default_time_value,
|
||||
self.app_config_fields.autohide_time,
|
||||
);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
let default_time_value = self.app_config_fields.default_time_value;
|
||||
let autohide_time = self.app_config_fields.autohide_time;
|
||||
|
||||
if let Some(ts_state) = self.current_ts_state() {
|
||||
ts_state.reset_zoom(default_time_value, autohide_time);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+8
-4
@@ -101,6 +101,10 @@ impl NetState {
|
||||
pub fn init(widget_states: HashMap<u64, NetWidgetState>) -> Self {
|
||||
NetState { widget_states }
|
||||
}
|
||||
|
||||
pub fn get_mut_widget_state(&mut self, widget_id: u64) -> Option<&mut NetWidgetState> {
|
||||
self.widget_states.get_mut(&widget_id)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CpuState {
|
||||
@@ -129,6 +133,10 @@ impl MemState {
|
||||
pub fn init(widget_states: HashMap<u64, MemWidgetState>) -> Self {
|
||||
MemState { widget_states }
|
||||
}
|
||||
|
||||
pub fn get_mut_widget_state(&mut self, widget_id: u64) -> Option<&mut MemWidgetState> {
|
||||
self.widget_states.get_mut(&widget_id)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TempState {
|
||||
@@ -161,10 +169,6 @@ impl TempGraphStates {
|
||||
pub fn get_mut_widget_state(&mut self, widget_id: u64) -> Option<&mut TempGraphWidgetState> {
|
||||
self.widget_states.get_mut(&widget_id)
|
||||
}
|
||||
|
||||
pub fn get_widget_state(&self, widget_id: u64) -> Option<&TempGraphWidgetState> {
|
||||
self.widget_states.get(&widget_id)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DiskState {
|
||||
|
||||
Reference in New Issue
Block a user