feat(bar): add optional data refresh intervals to config

This commit is contained in:
LGUG2Z
2024-09-09 19:16:53 -07:00
parent 34d2431947
commit 7907dfeb79
5 changed files with 69 additions and 6 deletions

View File

@@ -17,6 +17,8 @@ use std::time::Instant;
pub struct BatteryConfig { pub struct BatteryConfig {
/// Enable the Battery widget /// Enable the Battery widget
pub enable: bool, pub enable: bool,
/// Data refresh interval (default: 10 seconds)
pub data_refresh_interval: Option<u64>,
} }
impl From<BatteryConfig> for Battery { impl From<BatteryConfig> for Battery {
@@ -41,6 +43,7 @@ impl From<BatteryConfig> for Battery {
enable: value.enable, enable: value.enable,
manager, manager,
last_state, last_state,
data_refresh_interval: value.data_refresh_interval.unwrap_or(10),
state: state.unwrap_or(BatteryState::Discharging), state: state.unwrap_or(BatteryState::Discharging),
last_updated: Instant::now(), last_updated: Instant::now(),
} }
@@ -56,6 +59,7 @@ pub struct Battery {
pub enable: bool, pub enable: bool,
manager: Manager, manager: Manager,
pub state: BatteryState, pub state: BatteryState,
data_refresh_interval: u64,
last_state: Vec<String>, last_state: Vec<String>,
last_updated: Instant, last_updated: Instant,
} }
@@ -65,7 +69,7 @@ impl Battery {
let mut outputs = self.last_state.clone(); let mut outputs = self.last_state.clone();
let now = Instant::now(); let now = Instant::now();
if now.duration_since(self.last_updated) > Duration::from_secs(10) { if now.duration_since(self.last_updated) > Duration::from_secs(self.data_refresh_interval) {
outputs.clear(); outputs.clear();
if let Ok(mut batteries) = self.manager.batteries() { if let Ok(mut batteries) = self.manager.batteries() {

View File

@@ -17,6 +17,8 @@ use sysinfo::System;
pub struct MemoryConfig { pub struct MemoryConfig {
/// Enable the Memory widget /// Enable the Memory widget
pub enable: bool, pub enable: bool,
/// Data refresh interval (default: 10 seconds)
pub data_refresh_interval: Option<u64>,
} }
impl From<MemoryConfig> for Memory { impl From<MemoryConfig> for Memory {
@@ -29,6 +31,7 @@ impl From<MemoryConfig> for Memory {
Self { Self {
enable: value.enable, enable: value.enable,
system, system,
data_refresh_interval: value.data_refresh_interval.unwrap_or(10),
last_updated: Instant::now(), last_updated: Instant::now(),
} }
} }
@@ -37,13 +40,14 @@ impl From<MemoryConfig> for Memory {
pub struct Memory { pub struct Memory {
pub enable: bool, pub enable: bool,
system: System, system: System,
data_refresh_interval: u64,
last_updated: Instant, last_updated: Instant,
} }
impl Memory { impl Memory {
fn output(&mut self) -> Vec<String> { fn output(&mut self) -> Vec<String> {
let now = Instant::now(); let now = Instant::now();
if now.duration_since(self.last_updated) > Duration::from_secs(10) { if now.duration_since(self.last_updated) > Duration::from_secs(self.data_refresh_interval) {
self.system.refresh_memory(); self.system.refresh_memory();
self.last_updated = now; self.last_updated = now;
} }

View File

@@ -18,6 +18,8 @@ pub struct NetworkConfig {
pub enable: bool, pub enable: bool,
/// Show network transfer data /// Show network transfer data
pub show_data: bool, pub show_data: bool,
/// Data refresh interval (default: 10 seconds)
pub data_refresh_interval: Option<u64>,
} }
impl From<NetworkConfig> for Network { impl From<NetworkConfig> for Network {
@@ -48,6 +50,7 @@ impl From<NetworkConfig> for Network {
enable: value.enable, enable: value.enable,
last_state, last_state,
networks, networks,
data_refresh_interval: value.data_refresh_interval.unwrap_or(10),
show_data: value.show_data, show_data: value.show_data,
last_updated: Instant::now(), last_updated: Instant::now(),
} }
@@ -58,6 +61,7 @@ pub struct Network {
pub enable: bool, pub enable: bool,
pub show_data: bool, pub show_data: bool,
networks: Networks, networks: Networks,
data_refresh_interval: u64,
last_state: Vec<String>, last_state: Vec<String>,
last_updated: Instant, last_updated: Instant,
} }
@@ -67,7 +71,7 @@ impl Network {
let mut outputs = self.last_state.clone(); let mut outputs = self.last_state.clone();
let now = Instant::now(); let now = Instant::now();
if now.duration_since(self.last_updated) > Duration::from_secs(10) { if now.duration_since(self.last_updated) > Duration::from_secs(self.data_refresh_interval) {
outputs.clear(); outputs.clear();
if let Ok(interface) = netdev::get_default_interface() { if let Ok(interface) = netdev::get_default_interface() {

View File

@@ -16,6 +16,8 @@ use sysinfo::Disks;
pub struct StorageConfig { pub struct StorageConfig {
/// Enable the Storage widget /// Enable the Storage widget
pub enable: bool, pub enable: bool,
/// Data refresh interval (default: 10 seconds)
pub data_refresh_interval: Option<u64>,
} }
impl From<StorageConfig> for Storage { impl From<StorageConfig> for Storage {
@@ -23,6 +25,7 @@ impl From<StorageConfig> for Storage {
Self { Self {
enable: value.enable, enable: value.enable,
disks: Disks::new_with_refreshed_list(), disks: Disks::new_with_refreshed_list(),
data_refresh_interval: value.data_refresh_interval.unwrap_or(10),
last_updated: Instant::now(), last_updated: Instant::now(),
} }
} }
@@ -31,13 +34,14 @@ impl From<StorageConfig> for Storage {
pub struct Storage { pub struct Storage {
pub enable: bool, pub enable: bool,
disks: Disks, disks: Disks,
data_refresh_interval: u64,
last_updated: Instant, last_updated: Instant,
} }
impl Storage { impl Storage {
fn output(&mut self) -> Vec<String> { fn output(&mut self) -> Vec<String> {
let now = Instant::now(); let now = Instant::now();
if now.duration_since(self.last_updated) > Duration::from_secs(10) { if now.duration_since(self.last_updated) > Duration::from_secs(self.data_refresh_interval) {
self.disks.refresh(); self.disks.refresh();
self.last_updated = now; self.last_updated = now;
} }

View File

@@ -5,8 +5,7 @@
"required": [ "required": [
"left_widgets", "left_widgets",
"monitor", "monitor",
"right_widgets", "right_widgets"
"theme"
], ],
"properties": { "properties": {
"font_family": { "font_family": {
@@ -59,6 +58,12 @@
"enable" "enable"
], ],
"properties": { "properties": {
"data_refresh_interval": {
"description": "Data refresh interval (default: 10 seconds)",
"type": "integer",
"format": "uint64",
"minimum": 0.0
},
"enable": { "enable": {
"description": "Enable the Battery widget", "description": "Enable the Battery widget",
"type": "boolean" "type": "boolean"
@@ -237,6 +242,12 @@
"enable" "enable"
], ],
"properties": { "properties": {
"data_refresh_interval": {
"description": "Data refresh interval (default: 10 seconds)",
"type": "integer",
"format": "uint64",
"minimum": 0.0
},
"enable": { "enable": {
"description": "Enable the Memory widget", "description": "Enable the Memory widget",
"type": "boolean" "type": "boolean"
@@ -259,6 +270,12 @@
"show_data" "show_data"
], ],
"properties": { "properties": {
"data_refresh_interval": {
"description": "Data refresh interval (default: 10 seconds)",
"type": "integer",
"format": "uint64",
"minimum": 0.0
},
"enable": { "enable": {
"description": "Enable the Network widget", "description": "Enable the Network widget",
"type": "boolean" "type": "boolean"
@@ -284,6 +301,12 @@
"enable" "enable"
], ],
"properties": { "properties": {
"data_refresh_interval": {
"description": "Data refresh interval (default: 10 seconds)",
"type": "integer",
"format": "uint64",
"minimum": 0.0
},
"enable": { "enable": {
"description": "Enable the Storage widget", "description": "Enable the Storage widget",
"type": "boolean" "type": "boolean"
@@ -414,6 +437,12 @@
"enable" "enable"
], ],
"properties": { "properties": {
"data_refresh_interval": {
"description": "Data refresh interval (default: 10 seconds)",
"type": "integer",
"format": "uint64",
"minimum": 0.0
},
"enable": { "enable": {
"description": "Enable the Battery widget", "description": "Enable the Battery widget",
"type": "boolean" "type": "boolean"
@@ -592,6 +621,12 @@
"enable" "enable"
], ],
"properties": { "properties": {
"data_refresh_interval": {
"description": "Data refresh interval (default: 10 seconds)",
"type": "integer",
"format": "uint64",
"minimum": 0.0
},
"enable": { "enable": {
"description": "Enable the Memory widget", "description": "Enable the Memory widget",
"type": "boolean" "type": "boolean"
@@ -614,6 +649,12 @@
"show_data" "show_data"
], ],
"properties": { "properties": {
"data_refresh_interval": {
"description": "Data refresh interval (default: 10 seconds)",
"type": "integer",
"format": "uint64",
"minimum": 0.0
},
"enable": { "enable": {
"description": "Enable the Network widget", "description": "Enable the Network widget",
"type": "boolean" "type": "boolean"
@@ -639,6 +680,12 @@
"enable" "enable"
], ],
"properties": { "properties": {
"data_refresh_interval": {
"description": "Data refresh interval (default: 10 seconds)",
"type": "integer",
"format": "uint64",
"minimum": 0.0
},
"enable": { "enable": {
"description": "Enable the Storage widget", "description": "Enable the Storage widget",
"type": "boolean" "type": "boolean"