Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update SketchyBar #130

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ on: [push]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
include:
- os: macos-latest
features: sketchybar

runs-on: ${{ matrix.os }}

steps:
- name: Checkout repository
uses: actions/checkout@v4
Expand All @@ -17,9 +25,14 @@ jobs:
- name: Setup Cache
uses: Swatinem/rust-cache@v2

- name: Build crate
- if: ${{ !matrix.features }}
name: Build crate
run: cargo build

- if: ${{ matrix.features }}
name: Build crate
run: cargo build --features ${{ matrix.features }}

- name: Run tests
run: cargo test

Expand Down
11 changes: 11 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ rustls = "0.21.9"
serde = { version = "1", features = ["derive"] }
thiserror = "1.0.50"
ureq = { version = "2.8.0", features = ["json"] }
sketchybar-rs = { version = "0.1.0", optional = true }
sqlx = { version = "0.7", features = ["macros", "runtime-tokio", "sqlite"] }
tokio = { version = "1.33", features = ["macros"] }

[dev-dependencies]
tempfile = "3.8.1"

[features]
sketchybar = ["sketchybar-rs"]
44 changes: 43 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,44 @@ sketchybar -m \
--set weather label="${temp}°F"
```

#### Direct Integration

Alternatively `conditions` can directly update the running instance of SketchyBar without making calls from a shell script using `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 \
update_freq=1800 \
icon.drawing=off \
script="$HOME/.cargo/bin/conditions current --icon="

sketchybar \
--add item weather_logo right \
icon.font="Hack Nerd Font:Regular:14.0" \
label.drawing=off \
```

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

```rust
let message = format!(
"--set weather_logo icon=\"{}\" --set weather label=\"{}°{}\"",
conditions.icon,
conditions.temp_f as i32,
self.config.unit.as_char().to_ascii_uppercase()
);
```

In my bar this results in:

![example SketchyBar](./assets/sketchybar-example.png)

## Tasks

Run tasks from this directory via: `xc [task-name]`
Expand Down
Binary file added assets/sketchybar-example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 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")]
self.notify_sketchybar(&conditions)?;

let output = self.to_output(conditions);

Ok(ureq::serde_json::to_string(&output)?)
Expand All @@ -53,6 +57,26 @@ impl Conditions {

Output { temp, icon }
}

#[cfg(feature = "sketchybar")]
fn notify_sketchybar(
&self,
conditions: &CurrentConditions,
) -> eyre::Result<String> {
#[allow(clippy::cast_possible_truncation)]
// TODO: this shouldn't be hard-code. Consider additional CLI args
let message = format!(
"--set weather_logo icon=\"{}\" --set weather label=\"{}°{}\"",
conditions.icon,
conditions.temp_f as i32,
self.config.unit.as_char().to_ascii_uppercase()
);

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

#[cfg(test)]
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