Compare commits

...

10 commits

10 changed files with 666 additions and 232 deletions

1
.envrc Normal file
View file

@ -0,0 +1 @@
use flake

2
.gitignore vendored
View file

@ -1 +1,3 @@
/result
/target
/.direnv

701
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -16,12 +16,12 @@ futures = "0.3"
futures-async-stream = "0.2"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
smart-default = "0.6"
smart-default = "0.7"
structopt = "0.3"
tokio = { version = "1", features = ["fs", "io-std", "io-util", "time", "rt", "macros", "rt-multi-thread"] }
tokio-stream = { version = "0.1", features = ["time"]}
toml = "0.5"
uuid = { version = "0.8", features = [ "v4" ] }
toml = "0.8"
uuid = { version = "1.4", features = [ "v4" ] }
[profile.release]
incremental = false

61
flake.lock Normal file
View file

@ -0,0 +1,61 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1695132891,
"narHash": "sha256-cJR9AFHmt816cW/C9necLJyOg/gsnkvEeFAfxgeM1hc=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "8b5ab8341e33322e5b66fb46ce23d724050f6606",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs",
"utils": "utils"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
},
"utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1694529238,
"narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "ff7b65b44d01cf9ba6a71320833626af21126384",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

53
flake.nix Normal file
View file

@ -0,0 +1,53 @@
{
description = "sway/i3bar command in Rust";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, utils }:
let
supportedSystems = [ "x86_64-linux" "aarch64-linux" ];
in
(utils.lib.eachSystem supportedSystems (system:
let pkgs = import nixpkgs { inherit system; };
in
rec {
packages.rustybar = with pkgs; rustPlatform.buildRustPackage rec {
name = "rustybar";
version = "0.1";
src = ./.;
cargoLock.lockFile = ./Cargo.lock;
RUSTC_BOOTSTRAP = 1;
doCheck = false;
nativeBuildInputs = [ pkg-config ];
buildInputs = [ dbus ];
meta = with lib; {
homepage = "https://github.com/mildlyfunctionalgays/rustybar";
description = "swaybar/i3bar command in Rust";
maintainers = with maintainers; [ artemist ];
license = with licenses; [ mit ];
platforms = supportedSystems;
};
};
defaultPackage = packages.rustybar;
apps.rustybar = utils.lib.mkApp { drv = packages.rustybar; };
defaultApp = apps.rustybar;
overlay = final: prev: {
inherit (packages) rustybar;
};
devShells.rustybar = with pkgs; mkShell {
packages = [ pkg-config dbus rustup ];
};
devShell = devShells.rustybar;
})) // {
overlay = final: prev: {
inherit (self.packages."${prev.system}") rustybar;
};
};
}

View file

@ -1,8 +0,0 @@
{ pkgs ? import <nixpkgs> {} }:
with pkgs; stdenv.mkDerivation {
name = "rustybar-env";
nativeBuildInputs = [ pkgconfig ];
buildInputs = [ dbus rustup ];
}

View file

@ -54,7 +54,7 @@ pub struct TileConfig {
#[derive(Deserialize, Clone, Debug)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum TileConfigType {
Battery,
Battery(BatteryConfig),
Memory,
Load,
Hostname,
@ -62,6 +62,13 @@ pub enum TileConfigType {
Iwd(IwdConfig),
}
#[derive(SmartDefault, Deserialize, Clone, Debug)]
#[serde(default)]
pub struct BatteryConfig {
#[default("BAT0")]
pub battery: Box<str>,
}
#[derive(SmartDefault, Deserialize, Clone, Debug)]
#[serde(default)]
pub struct IwdConfig {
@ -118,16 +125,19 @@ pub async fn read_config() -> Result<Config, Box<dyn std::error::Error>> {
.await?
.read_to_end(&mut config_contents)
.await?;
Ok(toml::from_slice(&config_contents)?)
Ok(toml::from_str(std::str::from_utf8(&config_contents)?)?)
}
pub fn process_tile<'a>(
pub fn process_tile(
tile: &TileConfig,
connection: &'a Arc<SyncConnection>,
connection: &Arc<SyncConnection>,
) -> BoxStream<'static, TileResult> {
let five_secs = Duration::from_secs(5);
match &tile.config_type {
TileConfigType::Battery => wrap(tiles::battery_stream(), tile.update.or(Some(five_secs))),
TileConfigType::Battery(c) => wrap(
tiles::battery_stream(c.clone()),
tile.update.or(Some(five_secs)),
),
TileConfigType::Hostname => wrap(tiles::hostname_stream(connection.as_ref()), tile.update),
TileConfigType::Load => wrap(tiles::load_stream(), tile.update.or(Some(five_secs))),
TileConfigType::Memory => wrap(tiles::memory_stream(), tile.update.or(Some(five_secs))),

View file

@ -18,16 +18,21 @@ where
let mut blocks = Vec::new();
blocks.resize_with(num_tiles, Default::default);
loop {
let message = receiver.next().await.unwrap();
let message = message.unwrap();
if message.sender_id < num_tiles {
blocks[message.sender_id] = Some(message.block);
} else {
eprintln!("Invalid message with sender id {}", message.sender_id);
continue;
match receiver.next().await.unwrap() {
Ok(message) => {
if message.sender_id < num_tiles {
blocks[message.sender_id] = Some(message.block);
} else {
eprintln!("Invalid message with sender id {}", message.sender_id);
continue;
}
let mut serialized = serde_json::to_vec(&blocks).unwrap();
serialized.extend_from_slice(b",\n");
stdout.write_all(&serialized).await?;
}
Err(err) => {
eprintln!("Error in tile: {:?}", err);
}
}
let mut serialized = serde_json::to_vec(&blocks).unwrap();
serialized.extend_from_slice(b",\n");
stdout.write_all(&serialized).await?;
}
}

View file

@ -1,16 +1,27 @@
use crate::config::BatteryConfig;
use crate::tile::Block;
use futures::future::try_join3;
use futures_async_stream::try_stream;
use std::error::Error;
use tokio::fs::File;
use std::path::Path;
use tokio::fs::{try_exists, File};
use tokio::io::AsyncReadExt;
#[try_stream(ok = Block, error = Box<dyn Error + Send + Sync>)]
pub async fn battery_stream() {
pub async fn battery_stream(config: BatteryConfig) {
let base_dir = Path::new("/sys/class/power_supply").join(&*config.battery);
let file_prefix = if try_exists(base_dir.join("energy_now")).await? {
"energy_"
} else {
"charge_"
};
let now_path = base_dir.join(file_prefix.to_owned() + "now");
let full_path = base_dir.join(file_prefix.to_owned() + "full");
loop {
let charge_now = async {
let mut raw = String::new();
File::open("/sys/class/power_supply/BAT0/charge_now")
File::open(&now_path)
.await?
.read_to_string(&mut raw)
.await?;
@ -19,7 +30,7 @@ pub async fn battery_stream() {
};
let charge_total = async {
let mut raw = String::new();
File::open("/sys/class/power_supply/BAT0/charge_full")
File::open(&full_path)
.await?
.read_to_string(&mut raw)
.await?;
@ -28,7 +39,7 @@ pub async fn battery_stream() {
};
let status = async {
let mut raw = String::new();
File::open("/sys/class/power_supply/BAT0/status")
File::open(base_dir.join("status"))
.await?
.read_to_string(&mut raw)
.await?;