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 a2e0bc6
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 7 deletions.
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
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"]
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 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 \
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

0 comments on commit a2e0bc6

Please sign in to comment.