Implementation of modules in separate structs

This commit is contained in:
2025-05-04 22:13:41 -07:00
parent a20e1d8336
commit e1f2518239
6 changed files with 186 additions and 115 deletions

View File

@@ -0,0 +1,44 @@
use std::process::{Stdio, Command};
use crate::common;
use crate::bar_modules;
#[derive(Clone)]
pub struct UnibarModuleMusic {
pub opts: common::AppOptions,
}
impl bar_modules::BarModuleActions for UnibarModuleMusic {
// --------------------
fn get(&self) -> String {
let mut parts = Vec::new();
// MPD format options here:
// https://manpages.ubuntu.com/manpages/plucky/man1/mpc.1.html
let current_output = Command::new("mpc")
.arg("--format")
.arg("[%artist% - ][%title%]")
.arg("current")
.stdout(Stdio::piped())
.output()
.unwrap();
let mut current_stdout = String::from_utf8(current_output.stdout).unwrap();
current_stdout.pop();
parts.push(format!("{}", current_stdout));
if self.opts.music_progress {
let progress_output = Command::new("mpc")
.arg("status")
.arg("%percenttime% %totaltime%")
.stdout(Stdio::piped())
.output()
.unwrap();
let mut progress_stdout = String::from_utf8(progress_output.stdout).unwrap();
progress_stdout.pop();
parts.push(format!("[{}]", progress_stdout));
}
return format!("{}", parts.join(" "));
}
}

View File

@@ -0,0 +1,71 @@
use std::str;
use curl::easy::{Easy, List};
use serde_json::Value;
use crate::common;
use crate::bar_modules;
#[derive(Clone)]
pub struct UnibarModuleWeather {
pub opts: common::AppOptions,
}
impl UnibarModuleWeather {
// --------------------
fn get_current_temperature(&self, v: Value) -> f32 {
let deg_c :f32 = v["features"][0]["properties"]["temperature"]["value"]
.to_string().parse().unwrap();
match self.opts.weather_units {
common::TemperatureUnits::Metric => return deg_c,
common::TemperatureUnits::Imperial => return (deg_c * 9.0 / 5.0) + 32.0,
}
}
// --------------------
fn get_temperature_unit(&self) -> String{
return if self.opts.weather_units == common::TemperatureUnits::Metric { "°C".to_string() }
else { "°F".to_string() };
}
}
impl bar_modules::BarModuleActions for UnibarModuleWeather {
// --------------------
fn get(&self) -> String {
// Print a web page onto stdout
let mut curl = Easy::new();
let mut url_string = Vec::new();
let mut curl_ret = Vec::new();
url_string.push("https://api.weather.gov/stations/".to_owned());
url_string.push(self.opts.weather_station.to_owned());
url_string.push("/observations?limit=1".to_owned());
curl.url(url_string.concat().as_str()).unwrap();
{
let mut list = List::new();
list.append("User-Agent: Bar Weather (mahesh@heshapps.com)").unwrap();
curl.http_headers(list).unwrap();
let mut transfer = curl.transfer();
transfer.write_function(|data| {
curl_ret.extend_from_slice(data);
Ok(data.len())
}).unwrap();
transfer.perform().unwrap();
}
if self.opts.debug_json {
println!("-----> curl_data - [{}]", std::str::from_utf8(&curl_ret).unwrap());
}
let v: Value = serde_json::from_str(str::from_utf8(&curl_ret).unwrap()).unwrap();
let temperature_value :f32 = self.get_current_temperature(v);
let temperature_unit :String = self.get_temperature_unit();
return format!("{:.2}{}", temperature_value, temperature_unit);
}
}

8
src/bar_modules/mod.rs Normal file
View File

@@ -0,0 +1,8 @@
// --------------------
// All Bar modules must implement the actions
pub trait BarModuleActions {
fn get(&self) -> String;
}
pub mod bar_module_weather;
pub mod bar_module_music;