Skip to content

Commit

Permalink
add weather module
Browse files Browse the repository at this point in the history
  • Loading branch information
doums committed Aug 19, 2024
1 parent d3db2b8 commit 8e0e1ac
Show file tree
Hide file tree
Showing 9 changed files with 1,257 additions and 5 deletions.
951 changes: 949 additions & 2 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ tracing-appender = "0.2"
once_cell = "1.19.0"
chrono = "0.4"
regex = "1"
reqwest = { version = "0.12.6", features = ["blocking"] }

[build-dependencies]
cmake = "0.1"
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Like [xmobar](https://codeberg.org/xmobar/xmobar),
* brightness
* cpu usage, frequency and temperature
* memory (percent or used/total in gigabyte/gibibyte)
* weather (wttr.in)
* dynamic and customizable labels
* customizable format output
* configuration in YAML
Expand Down Expand Up @@ -137,8 +138,8 @@ baru -l file

### implementation details

Baru gathers the information from `/sys` and `/proc` filesystems\
(filled by the kernel). Except audio and network modules which use C libraries.\
Baru gathers the information from `/sys` and `/proc` filesystems (filled by the kernel).\
Except audio and network modules which use C libraries.\
All modules are threaded and loaded on-demand.\
Thanks to this modular design (as well Rust and C), baru is lightweight and
efficient.\
Expand Down
71 changes: 71 additions & 0 deletions baru.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
# f => cpu frequency
# m => memory
# i => mic
# r => weather
# s => sound
# t => temperature
# w => wireless
Expand Down Expand Up @@ -403,6 +404,76 @@ mic:
format: '%l_%v'


# # # # # # # # # #
# Weather module #
# # # # # # # # # #

weather:
# The weather data is fetched from https://wttr.in/ API.
# Takes the following options:

# tick: u32, default: 5
#
# The refresh rate in **minutes** of the weather data.
#
tick: 5

# location: String, default: None
#
# If not provided, it defaults to the current location based on the IP address.
# see https://wttr.in/:help
#
location: 'Monte+Cinto'

# wttr_format: String, default: %C+%t
#
# wttr.in `format` URL parameter
# see https://github.com/chubin/wttr.in?tab=readme-ov-file#one-line-output
#
wttr_format: '%C+%t'

# unit: enum Unit { Metric, MetricMs, Uscs }, default: Metric
#
# wttr.in unit URL parameters
# - Metric: Celsius for temperature, km/h for wind speed
# - MetricMs: Celsius for temperature, m/s for wind speed
# - Uscs: Fahrenheit for temperature, mph for wind speed
# see https://wttr.in/:help
#
unit: 'Metric'

# lang: String, default: None
#
# wttr.in `lang` URL parameter (two-letter language code)
# see https://wttr.in/:help
#
lang: 'en'

# compact_temp: Bool, default: false
#
# Trim temperature output to only the value° (and minus sign) without the unit.
#
compact_temp: false

# placeholder: String, default: -
#
# Value to display when there is no data available yet.
#
placeholder: '-'

# label: String, default: wtr
#
# The module label.
#
label: wtr

# format: String, default: %l:%v
#
# The module format.
#
format: '%l:%v'


# # # # # # # # #
# Sound module #
# # # # # # # # #
Expand Down
15 changes: 15 additions & 0 deletions src/http.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use std::time::Duration;

use once_cell::sync::Lazy;
use reqwest::blocking::Client;
use tracing::error;

pub static HTTP_CLIENT: Lazy<Client> = Lazy::new(|| {
Client::builder()
.timeout(Duration::from_secs(10))
.build()
.inspect_err(|e| {
error!("Failed to create HTTP client: {:?}", e);
})
.unwrap()
});
9 changes: 8 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ mod error;
mod module;
mod modules;
mod netlink;
pub mod pulse;
mod pulse;
pub mod trace;
pub mod util;
mod http;

use anyhow::{anyhow, Result};
use error::Error;
Expand All @@ -23,6 +24,7 @@ use modules::memory::Config as MemoryConfig;
use modules::mic::Config as MicConfig;
use modules::sound::Config as SoundConfig;
use modules::temperature::Config as TemperatureConfig;
use modules::weather::Config as WeatherConfig;
use modules::wired::Config as WiredConfig;
use modules::wireless::Config as WirelessConfig;
use serde::{Deserialize, Serialize};
Expand All @@ -31,6 +33,10 @@ use std::thread;
use tracing::{error, info, instrument};

#[derive(Debug)]
/// Message sent by modules.
/// `0`: module key,
/// `1`: value,
/// `2`: label
pub struct ModuleMsg(char, Option<String>, Option<String>);

#[derive(Debug, Serialize, Deserialize, Clone)]
Expand All @@ -47,6 +53,7 @@ pub struct Config {
mic: Option<MicConfig>,
sound: Option<SoundConfig>,
temperature: Option<TemperatureConfig>,
weather: Option<WeatherConfig>,
wired: Option<WiredConfig>,
wireless: Option<WirelessConfig>,
}
Expand Down
7 changes: 7 additions & 0 deletions src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::modules::memory::Memory;
use crate::modules::mic::Mic;
use crate::modules::sound::Sound;
use crate::modules::temperature::Temperature;
use crate::modules::weather::Weather;
use crate::modules::wired::Wired;
use crate::modules::wireless::Wireless;
use crate::Config;
Expand Down Expand Up @@ -41,6 +42,7 @@ pub enum Module<'a> {
Sound(Sound<'a>),
Temperature(Temperature<'a>),
Wireless(Wireless<'a>),
Weather(Weather<'a>),
}

impl<'a> TryFrom<(char, &'a Config)> for Module<'a> {
Expand All @@ -56,6 +58,7 @@ impl<'a> TryFrom<(char, &'a Config)> for Module<'a> {
'f' => Ok(Module::CpuFreq(CpuFreq::with_config(config))),
'i' => Ok(Module::Mic(Mic::with_config(config))),
'm' => Ok(Module::Memory(Memory::with_config(config))),
'r' => Ok(Module::Weather(Weather::with_config(config))),
's' => Ok(Module::Sound(Sound::with_config(config))),
't' => Ok(Module::Temperature(Temperature::with_config(config))),
'w' => Ok(Module::Wireless(Wireless::with_config(config))),
Expand All @@ -77,6 +80,7 @@ impl<'a> Bar for Module<'a> {
Module::Wired(m) => m.name(),
Module::Sound(m) => m.name(),
Module::Temperature(m) => m.name(),
Module::Weather(m) => m.name(),
Module::Wireless(m) => m.name(),
}
}
Expand All @@ -92,6 +96,7 @@ impl<'a> Bar for Module<'a> {
Module::Mic(m) => m.run_fn(),
Module::Sound(m) => m.run_fn(),
Module::Temperature(m) => m.run_fn(),
Module::Weather(m) => m.run_fn(),
Module::Wired(m) => m.run_fn(),
Module::Wireless(m) => m.run_fn(),
}
Expand All @@ -109,6 +114,7 @@ impl<'a> Bar for Module<'a> {
Module::Mic(m) => m.placeholder(),
Module::Sound(m) => m.placeholder(),
Module::Temperature(m) => m.placeholder(),
Module::Weather(m) => m.placeholder(),
Module::Wireless(m) => m.placeholder(),
}
}
Expand All @@ -125,6 +131,7 @@ impl<'a> Bar for Module<'a> {
Module::Mic(m) => m.format(),
Module::Sound(m) => m.format(),
Module::Temperature(m) => m.format(),
Module::Weather(m) => m.format(),
Module::Wireless(m) => m.format(),
}
}
Expand Down
1 change: 1 addition & 0 deletions src/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ pub mod memory;
pub mod mic;
pub mod sound;
pub mod temperature;
pub mod weather;
pub mod wired;
pub mod wireless;
Loading

0 comments on commit 8e0e1ac

Please sign in to comment.