Skip to content

Commit

Permalink
add an optional label for the light item
Browse files Browse the repository at this point in the history
  • Loading branch information
acheronfail committed May 16, 2024
1 parent dc83b7a commit fae1ca7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
2 changes: 2 additions & 0 deletions sample_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ left_click = { modifiers = ["Control"], command = "i3-msg exec pavucontrol" }
type = "light"
# Optionally provide a path to a specific backlight:
# path = "/sys/class/backlight/intel_backlight"
# Optionally provide a label that will be appended:
# label = " screen"
# Optionally specify how much percentage to increment the light by when scrolling (default is 5):
# increment = 10

Expand Down
15 changes: 12 additions & 3 deletions src/bar_items/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl LightFile {
}
}

pub async fn format(&self) -> Result<I3Item> {
pub async fn format(&self, label: Option<&str>) -> Result<I3Item> {
let pct = self.get().await?;
let icon = match pct {
0..=29 => "󰃜",
Expand All @@ -113,12 +113,20 @@ impl LightFile {
90..=u8::MAX => "󰃠",
};

Ok(I3Item::new(format!("{} {:>3}%", icon, pct)))
Ok(I3Item::new(format!(
"{} {:>3}%{}",
icon,
pct,
label.unwrap_or("")
)))
}
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct Light {
/// Optional label for this light
#[serde(default)]
label: Option<String>,
/// Optional path to a specific light.
path: Option<PathBuf>,
/// How much to increment the light when scrolling up or down.
Expand All @@ -136,7 +144,8 @@ impl BarItem for Light {

let increment = self.increment.unwrap_or(5) as i8;
loop {
ctx.update_item(light.format().await?).await?;
ctx.update_item(light.format(self.label.as_deref()).await?)
.await?;
match ctx.wait_for_event(None).await {
// mouse events
Some(BarEvent::Click(click)) => match click.button {
Expand Down

0 comments on commit fae1ca7

Please sign in to comment.