Skip to content

Commit

Permalink
Add duration format to uptime block
Browse files Browse the repository at this point in the history
  • Loading branch information
bim9262 committed Apr 2, 2024
1 parent 1583654 commit d49ef04
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/blocks/uptime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
//!
//! Key | Values | Default
//! -----------|----------------------------|--------
//! `format` | A string to customise the output of this block. See below for available placeholders | `" $icon $text "`
//! `format` | A string to customise the output of this block. See below for available placeholders | `" $icon $uptime "`
//! `interval` | Update interval in seconds | `60`
//!
//! Placeholder | Value | Type | Unit
//! --------------|-------------------------|--------|-----
//! `icon` | A static icon | Icon | -
//! `text` | Current uptime | Text | -
//! Placeholder | Value | Type | Unit
//! --------------------|-------------------------|----------|-----
//! `icon` | A static icon | Icon | -
//! `text` *DEPRECATED* | Current uptime | Text | -
//! `uptime` | Current uptime | Duration | -
//!
//! `text` has been deprecated in favor of `uptime`.
//!
//! # Example
//!
Expand All @@ -25,9 +28,6 @@
//!
//! # Used Icons
//! - `uptime`
//!
//! # TODO:
//! - Add `time` or `dur` formatter to `src/formatting/formatter.rs`

use super::prelude::*;
use tokio::fs::read_to_string;
Expand All @@ -41,7 +41,7 @@ pub struct Config {
}

pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
let format = config.format.with_default(" $icon $text ")?;
let format = config.format.with_default(" $icon $uptime ")?;

loop {
let uptime = read_to_string("/proc/uptime")
Expand All @@ -53,6 +53,8 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
.and_then(|u| u.parse().ok())
.error("/proc/uptime has invalid content")?;

let uptime = Duration::from_secs(seconds);

let weeks = seconds / 604_800;
seconds %= 604_800;
let days = seconds / 86_400;
Expand All @@ -75,7 +77,8 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
let mut widget = Widget::new().with_format(format.clone());
widget.set_values(map! {
"icon" => Value::icon("uptime"),
"text" => Value::text(text)
"text" => Value::text(text),
"uptime" => Value::duration(uptime)
});
api.set_widget(widget)?;

Expand Down

0 comments on commit d49ef04

Please sign in to comment.