Skip to content

Commit

Permalink
feat: update SketchyBar
Browse files Browse the repository at this point in the history
  • Loading branch information
johnallen3d committed Oct 13, 2023
1 parent 62995c4 commit 32c6b59
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 5 deletions.
18 changes: 16 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ lazy_static = "1.4.0"
log = "0.4.20"
rustls = "0.21.7"
serde = { version = "1", features = ["derive"] }
thiserror = "1.0.49"
ureq = { version = "2.8.0", features = ["json"] }
sketchybar-rs = { version = "0.1.0", optional = true }
sqlx = { version = "0.7", features = ["macros", "runtime-tokio", "sqlite"] }
thiserror = "1.0.49"
tokio = { version = "1.33", features = ["macros"] }
ureq = { version = "2.8.0", features = ["json"] }

[dev-dependencies]
tempfile = "3.8.0"

[features]
sketchybar = ["sketchybar-rs"]
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ conditions location set "10001, usa"

### SketchyBar

Here's how I'm using this with SketchyBar.
[SketchyBar](https://github.com/FelixKratz/SketchyBar) is a very customizable bar app for macOS that is deliberately light on resource usage (written in C vs more common WebView approach). Widgets in SketchyBar are refreshed (if need be) via "plugins" (typically shell scripts).

#### Manual

Here's how I'm using `conditions` with SketchyBar in the more traditional plugin approach.

```bash
#!/bin/bash
Expand All @@ -74,6 +78,27 @@ sketchybar -m \
--set weather label="${temp}°F"
```

#### Direct Integration

Alternatively `conditions` can directly update the running instance of SketchyBar without making calls from eg. Bash via `sketchybar -m`. In order to use this experimental version the `sketchybar` feature must be enabled on install.

```bash
cargo install conditions --features sketchybar
```

Then when setting up a weather widget in `sketchybarrc`, `conditions` can be invoked directly.

```bash
sketchybar \
--add item weather right \
--set weather \
update_freq=1800 \
icon.drawing=off \
script="$HOME/.cargo/bin/conditions current"
```

Note, at this time `conditions` is hard-coded with widgets named `weather` and `weather_logo`

## Tasks

Run tasks from this directory via: `xc [task-name]`
Expand Down
18 changes: 18 additions & 0 deletions src/conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ impl Conditions {
};

let conditions = CurrentConditions::get(&self.config, &location)?;

#[cfg(feature = "sketchybar")]
notify_sketchybar(&conditions)?;

let output = self.to_output(conditions);

Ok(ureq::serde_json::to_string(&output)?)
Expand All @@ -55,6 +59,20 @@ impl Conditions {
}
}

#[cfg(feature = "sketchybar")]
fn notify_sketchybar(conditions: &CurrentConditions) -> eyre::Result<String> {
#[allow(clippy::cast_possible_truncation)]
let message = format!(
"--set weather_logo icon=\"{}\" --set weather label=\"{}°F\"",
conditions.icon, conditions.temp_f as i32
);

match sketchybar_rs::message(&message) {
Ok(_) => Ok(String::new()),
Err(err) => Err(eyre::eyre!(err.to_string())),
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#![deny(clippy::pedantic)]

#[cfg(feature = "sketchybar")]
extern crate sketchybar_rs;

use clap::Parser;

use args::{
Expand Down

0 comments on commit 32c6b59

Please sign in to comment.