cabytcini/src/lib.rs

91 lines
2.0 KiB
Rust

#[macro_use]
extern crate log;
use chrono::prelude::*;
use serde::Serialize;
use std::{
fmt,
sync::{Arc, Mutex},
};
use xsetroot::XSetRoot;
pub mod config;
pub mod front;
pub mod weather;
pub mod xsetroot;
pub static APP_USER_AGENT: &str = concat!(
env!("CARGO_PKG_NAME"),
"/",
env!("CARGO_PKG_VERSION"),
" +https://tulpa.dev/cadey/cabytcini",
);
pub type MTState = Arc<Mutex<State>>;
pub struct State {
pub msg: String,
pub front: String,
pub weather: Option<weather::Root>,
}
#[derive(Serialize, Default)]
pub struct Block {
pub full_text: String,
pub color: Option<String>,
}
impl fmt::Display for State {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.msg)
}
}
impl State {
pub fn init() -> State {
State {
msg: "".to_string(),
front: "".to_string(),
weather: None,
}
}
pub fn to_string(&self) -> String {
let now = Local::now().format("%H:%M M%m %-d %a");
let mut msg = String::new();
if self.msg != "" {
msg.push_str(format!("{} | ", self.msg).as_str());
}
if self.front != "" {
msg.push_str(format!("{} | ", self.front).as_str());
}
match &self.weather {
Some(datni) => msg.push_str(
format!(
"{} {} / {} {} | ",
datni.currently.temperature,
datni.currently.summary,
datni.daily.data[0].temperature_high,
datni.daily.data[0].temperature_low
)
.as_str(),
),
None => {}
};
msg.push_str(format!("{}", now).as_str());
msg
}
pub fn xsetroot(&self) {
let xsr = XSetRoot::init().expect("xsetroot to initialize");
if let Err(why) = xsr.render(self.to_string()) {
error!("error setting root window title: {:?}", why);
}
}
}