Skip to content

Commit

Permalink
feat(bar): add optional data refresh intervals to config
Browse files Browse the repository at this point in the history
  • Loading branch information
LGUG2Z committed Sep 10, 2024
1 parent 34d2431 commit 7907dfe
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 6 deletions.
6 changes: 5 additions & 1 deletion komorebi-bar/src/battery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use std::time::Instant;
pub struct BatteryConfig {
/// Enable the Battery widget
pub enable: bool,
/// Data refresh interval (default: 10 seconds)
pub data_refresh_interval: Option<u64>,
}

impl From<BatteryConfig> for Battery {
Expand All @@ -41,6 +43,7 @@ impl From<BatteryConfig> for Battery {
enable: value.enable,
manager,
last_state,
data_refresh_interval: value.data_refresh_interval.unwrap_or(10),
state: state.unwrap_or(BatteryState::Discharging),
last_updated: Instant::now(),
}
Expand All @@ -56,6 +59,7 @@ pub struct Battery {
pub enable: bool,
manager: Manager,
pub state: BatteryState,
data_refresh_interval: u64,
last_state: Vec<String>,
last_updated: Instant,
}
Expand All @@ -65,7 +69,7 @@ impl Battery {
let mut outputs = self.last_state.clone();

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();

if let Ok(mut batteries) = self.manager.batteries() {
Expand Down
6 changes: 5 additions & 1 deletion komorebi-bar/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use sysinfo::System;
pub struct MemoryConfig {
/// Enable the Memory widget
pub enable: bool,
/// Data refresh interval (default: 10 seconds)
pub data_refresh_interval: Option<u64>,
}

impl From<MemoryConfig> for Memory {
Expand All @@ -29,6 +31,7 @@ impl From<MemoryConfig> for Memory {
Self {
enable: value.enable,
system,
data_refresh_interval: value.data_refresh_interval.unwrap_or(10),
last_updated: Instant::now(),
}
}
Expand All @@ -37,13 +40,14 @@ impl From<MemoryConfig> for Memory {
pub struct Memory {
pub enable: bool,
system: System,
data_refresh_interval: u64,
last_updated: Instant,
}

impl Memory {
fn output(&mut self) -> Vec<String> {
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.last_updated = now;
}
Expand Down
6 changes: 5 additions & 1 deletion komorebi-bar/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ pub struct NetworkConfig {
pub enable: bool,
/// Show network transfer data
pub show_data: bool,
/// Data refresh interval (default: 10 seconds)
pub data_refresh_interval: Option<u64>,
}

impl From<NetworkConfig> for Network {
Expand Down Expand Up @@ -48,6 +50,7 @@ impl From<NetworkConfig> for Network {
enable: value.enable,
last_state,
networks,
data_refresh_interval: value.data_refresh_interval.unwrap_or(10),
show_data: value.show_data,
last_updated: Instant::now(),
}
Expand All @@ -58,6 +61,7 @@ pub struct Network {
pub enable: bool,
pub show_data: bool,
networks: Networks,
data_refresh_interval: u64,
last_state: Vec<String>,
last_updated: Instant,
}
Expand All @@ -67,7 +71,7 @@ impl Network {
let mut outputs = self.last_state.clone();

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();

if let Ok(interface) = netdev::get_default_interface() {
Expand Down
6 changes: 5 additions & 1 deletion komorebi-bar/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ use sysinfo::Disks;
pub struct StorageConfig {
/// Enable the Storage widget
pub enable: bool,
/// Data refresh interval (default: 10 seconds)
pub data_refresh_interval: Option<u64>,
}

impl From<StorageConfig> for Storage {
fn from(value: StorageConfig) -> Self {
Self {
enable: value.enable,
disks: Disks::new_with_refreshed_list(),
data_refresh_interval: value.data_refresh_interval.unwrap_or(10),
last_updated: Instant::now(),
}
}
Expand All @@ -31,13 +34,14 @@ impl From<StorageConfig> for Storage {
pub struct Storage {
pub enable: bool,
disks: Disks,
data_refresh_interval: u64,
last_updated: Instant,
}

impl Storage {
fn output(&mut self) -> Vec<String> {
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.last_updated = now;
}
Expand Down
51 changes: 49 additions & 2 deletions schema.bar.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"required": [
"left_widgets",
"monitor",
"right_widgets",
"theme"
"right_widgets"
],
"properties": {
"font_family": {
Expand Down Expand Up @@ -59,6 +58,12 @@
"enable"
],
"properties": {
"data_refresh_interval": {
"description": "Data refresh interval (default: 10 seconds)",
"type": "integer",
"format": "uint64",
"minimum": 0.0
},
"enable": {
"description": "Enable the Battery widget",
"type": "boolean"
Expand Down Expand Up @@ -237,6 +242,12 @@
"enable"
],
"properties": {
"data_refresh_interval": {
"description": "Data refresh interval (default: 10 seconds)",
"type": "integer",
"format": "uint64",
"minimum": 0.0
},
"enable": {
"description": "Enable the Memory widget",
"type": "boolean"
Expand All @@ -259,6 +270,12 @@
"show_data"
],
"properties": {
"data_refresh_interval": {
"description": "Data refresh interval (default: 10 seconds)",
"type": "integer",
"format": "uint64",
"minimum": 0.0
},
"enable": {
"description": "Enable the Network widget",
"type": "boolean"
Expand All @@ -284,6 +301,12 @@
"enable"
],
"properties": {
"data_refresh_interval": {
"description": "Data refresh interval (default: 10 seconds)",
"type": "integer",
"format": "uint64",
"minimum": 0.0
},
"enable": {
"description": "Enable the Storage widget",
"type": "boolean"
Expand Down Expand Up @@ -414,6 +437,12 @@
"enable"
],
"properties": {
"data_refresh_interval": {
"description": "Data refresh interval (default: 10 seconds)",
"type": "integer",
"format": "uint64",
"minimum": 0.0
},
"enable": {
"description": "Enable the Battery widget",
"type": "boolean"
Expand Down Expand Up @@ -592,6 +621,12 @@
"enable"
],
"properties": {
"data_refresh_interval": {
"description": "Data refresh interval (default: 10 seconds)",
"type": "integer",
"format": "uint64",
"minimum": 0.0
},
"enable": {
"description": "Enable the Memory widget",
"type": "boolean"
Expand All @@ -614,6 +649,12 @@
"show_data"
],
"properties": {
"data_refresh_interval": {
"description": "Data refresh interval (default: 10 seconds)",
"type": "integer",
"format": "uint64",
"minimum": 0.0
},
"enable": {
"description": "Enable the Network widget",
"type": "boolean"
Expand All @@ -639,6 +680,12 @@
"enable"
],
"properties": {
"data_refresh_interval": {
"description": "Data refresh interval (default: 10 seconds)",
"type": "integer",
"format": "uint64",
"minimum": 0.0
},
"enable": {
"description": "Enable the Storage widget",
"type": "boolean"
Expand Down

0 comments on commit 7907dfe

Please sign in to comment.