Make name optional, clean config_path, skip nones when serializing

This commit is contained in:
Skye 2023-12-16 15:20:59 -05:00
parent 184b854747
commit ad1d0bde35
12 changed files with 31 additions and 18 deletions

View file

@ -180,21 +180,19 @@ struct Args {
pub async fn read_config() -> eyre::Result<Config> {
let args = Args::from_args();
let Some(config_path) = args.config.clone().or_else(|| {
Some(if let Ok(rusty_conf) = var("RUSTYBAR_CONFIG") {
let config_path = args.config.map(Ok).unwrap_or_else(|| {
Ok(if let Ok(rusty_conf) = var("RUSTYBAR_CONFIG") {
rusty_conf.into()
} else if let Ok(config) = var("XDG_CONFIG_HOME") {
PathBuf::from_iter([&config, "rustybar/config.toml"])
} else if let Ok(home) = var("HOME") {
PathBuf::from_iter([&home, ".config/rustybar/config.toml"])
} else {
return None;
})
}) else {
eyre::bail!(
"Could not find RUSTYBAR_CONFIG, XDG_CONFIG_HOME, or HOME environment variables"
)
};
})
})?;
let string = tokio::fs::read_to_string(config_path)
.await

View file

@ -1,5 +1,6 @@
use crate::tile::{Block, TileData};
use eyre::OptionExt;
use serde::{Serialize, ser::SerializeSeq};
use std::sync::Arc;
use tokio::{
io::{self, AsyncWriteExt},
@ -55,3 +56,25 @@ pub async fn run(num_tiles: usize, mut receiver: mpsc::Receiver<TileData>) -> ey
stdout.write_all(&serialized).await?;
}
}
struct NoneSkipper<'b, I, T>(&'b I)
where
&'b I: IntoIterator<Item = &'b Option<T>>,
T: Serialize + 'b;
impl<'b, I, T> Serialize for NoneSkipper<'b, I, T>
where
&'b I: IntoIterator<Item = &'b Option<T>>,
T: Serialize,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut seq = serializer.serialize_seq(None)?;
for e in self.0.into_iter().filter_map(Option::as_ref) {
seq.serialize_element(e)?;
}
seq.end()
}
}

View file

@ -74,7 +74,8 @@ pub struct Block {
pub min_width: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub align: Option<Alignment>,
pub name: &'static str,
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<&'static str>,
#[serde(serialize_with = "arc_default")]
#[default = ""]
pub instance: Arc<str>,

View file

@ -35,7 +35,6 @@ pub async fn battery(config: BatteryConfig, output: OutputChannel) -> eyre::Resu
.send(Block {
full_text: format!("{}% {}", percentage, status).into(),
short_text: format!("{}%", percentage).into_boxed_str().into(),
name: "battery",
..Default::default()
})
.await?;

View file

@ -27,7 +27,6 @@ pub async fn hostname(
output
.send(Block {
full_text: hostname.into(),
name: "hostname",
..Default::default()
})
.await?;

View file

@ -30,7 +30,6 @@ pub async fn internet_time(
)
.into_boxed_str(),
short_text: Some(format!("@{:03.0}", internet_time).into_boxed_str()),
name: "internet_time",
..Default::default()
})
.await?;

View file

@ -63,7 +63,6 @@ pub async fn iwd(
output
.send(Block {
full_text: text.into(),
name: config.interface,
..Default::default()
})
.await?;

View file

@ -16,7 +16,6 @@ pub async fn load(config: LoadConfig, output: OutputChannel) -> eyre::Result<Inf
output
.send(Block {
full_text: load.into(),
name: "load",
..Default::default()
})
.await?;

View file

@ -102,7 +102,6 @@ pub async fn local_time(
.send(Block {
full_text: local_now.format(&config.format).to_string().into(),
short_text: Some(local_now.format(&config.short_format).to_string().into()),
name: "local_time",
..Default::default()
})
.await?;

View file

@ -57,7 +57,6 @@ pub async fn memory(config: MemoryConfig, output: OutputChannel) -> eyre::Result
.send(Block {
full_text,
short_text: Some(short_text),
name: "memory",
..Default::default()
})
.await?;

View file

@ -16,7 +16,6 @@ pub async fn system_time(
.send(Block {
full_text: now.format(&config.format).to_string().into(),
short_text: Some(now.format(&config.short_format).to_string().into()),
name: "system_time",
..Default::default()
})
.await?;

View file

@ -13,7 +13,6 @@ pub async fn utc_time(config: UtcTimeConfig, output: OutputChannel) -> eyre::Res
.send(Block {
full_text: now.format(&config.format).to_string().into(),
short_text: Some(now.format(&config.short_format).to_string().into()),
name: "utc_time",
..Default::default()
})
.await?;