Skip to content

Commit

Permalink
feat: add feature flags
Browse files Browse the repository at this point in the history
Flags allow you to disable certain functionality and compile with only select features to reduce build time.

Resolves #54.
  • Loading branch information
JakeStanger committed Feb 1, 2023
1 parent e83618b commit c347b6c
Show file tree
Hide file tree
Showing 16 changed files with 245 additions and 58 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ jobs:
- name: Check formatting
run: cargo fmt --check

- name: Clippy
- name: Clippy (base features)
uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --no-default-features --features config+json

- name: Clippy (all features)
uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

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

89 changes: 71 additions & 18 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,91 @@ edition = "2021"
license = "MIT"
description = "Customisable GTK Layer Shell wlroots/sway bar"

[features]
default = [
"http",
"config+all",
"clock",
"music+all",
"sys_info",
"tray",
"workspaces+all"
]

http = ["dep:reqwest"]

"config+all" = ["config+json", "config+yaml", "config+toml", "config+corn"]
"config+json" = ["serde_json"]
"config+yaml" = ["serde_yaml"]
"config+toml" = ["toml"]
"config+corn" = ["libcorn"]

clock = ["chrono"]

music = ["regex"]
"music+all" = ["music", "music+mpris", "music+mpd"]
"music+mpris" = ["music", "mpris"]
"music+mpd" = ["music", "mpd_client"]

sys_info = ["sysinfo", "regex"]

tray = ["stray"]

workspaces = ["futures-util"]
"workspaces+all" = ["workspaces", "workspaces+sway", "workspaces+hyprland"]
"workspaces+sway" = ["workspaces", "swayipc-async"]
"workspaces+hyprland" = ["workspaces", "hyprland"]

[dependencies]
# core
gtk = "0.16.0"
gtk-layer-shell = "0.5.0"
glib = "0.16.2"
tokio = { version = "1.21.2", features = ["macros", "rt-multi-thread", "time", "process"] }
tokio = { version = "1.21.2", features = ["macros", "rt-multi-thread", "time", "process", "sync", "io-util", "net"] }
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
tracing-error = "0.2.0"
tracing-appender = "0.2.2"
strip-ansi-escapes = "0.1.1"
color-eyre = "0.6.2"
serde = { version = "1.0.141", features = ["derive"] }
serde_json = "1.0.82"
serde_yaml = "0.9.4"
toml = "0.7.0"
libcorn = "0.6.1"
lazy_static = "1.4.0"
async_once = "0.2.6"
indexmap = "1.9.1"
futures-util = "0.3.21"
chrono = "0.4.19"
reqwest = {version = "0.11.14" }
regex = { version = "1.6.0", default-features = false, features = ["std"] }
stray = { version = "0.1.3" }
dirs = "4.0.0"
walkdir = "2.3.2"
notify = { version = "5.0.0", default-features = false }
mpd_client = "1.0.0"
mpris = "2.0.0"
swayipc-async = { version = "2.0.1" }
hyprland = "0.3.0"
sysinfo = "0.27.0"
wayland-client = "0.29.5"
wayland-protocols = { version = "0.29.5", features = ["unstable_protocols", "client"] }
smithay-client-toolkit = { version = "0.16.0", default-features = false, features = ["calloop"] }
smithay-client-toolkit = { version = "0.16.0", default-features = false, features = ["calloop"] }
lazy_static = "1.4.0"
async_once = "0.2.6"
cfg-if = "1.0.0"

# http
reqwest = { version = "0.11.14", optional = true }

# config
serde_json = { version = "1.0.82", optional = true }
serde_yaml = { version = "0.9.4", optional = true }
toml = { version = "0.7.0", optional = true }
libcorn = { version = "0.6.1", optional = true }

# clock
chrono = { version = "0.4.19", optional = true }

# music
mpd_client = { version = "1.0.0", optional = true }
mpris = { version = "2.0.0", optional = true }

# sys_info
sysinfo = { version = "0.27.0", optional = true }

# tray
stray = { version = "0.1.3", optional = true }

# workspaces
swayipc-async = { version = "2.0.1", optional = true }
hyprland = { version = "0.3.0", optional = true }
futures-util = { version = "0.3.21", optional = true }

# shared
regex = { version = "1.6.0", default-features = false, features = ["std"], optional = true } # music, sys_info
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ yay -S ironbar-git

### Nix Flake

A flake is included with the repo which can be used with home-manager.

#### Example
Here is an example nix flake that uses ironbar, this is just a
proof of concept, please adapt it to your config

Here is an example nix flake that uses Ironbar.

```nix
{
Expand Down Expand Up @@ -67,8 +69,9 @@ proof of concept, please adapt it to your config
```

#### Binary Caching
There is also a cachix cache at `https://app.cachix.org/cache/jakestanger`
incase you don't want to compile ironbar!

There is a Cachix cache available at `https://app.cachix.org/cache/jakestanger`
in case you don't want to compile Ironbar.

### Source

Expand All @@ -80,6 +83,9 @@ cargo build --release
install target/release/ironbar ~/.local/bin/ironbar
```

By default, all features are enabled.
See [here](https://github.com/JakeStanger/ironbar/wiki/compiling) for controlling which features are included.

[repo](https://github.com/jakestanger/ironbar)

## Running
Expand Down
51 changes: 51 additions & 0 deletions docs/Compiling.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
You can compile Ironbar from source using `cargo`.
Just clone the repo and build:

```sh
git clone https://github.com/jakestanger/ironbar.git
cd ironbar
cargo build --release
# change path to wherever you want to install
install target/release/ironbar ~/.local/bin/ironbar
```

## Features

By default, all features are enabled for convenience. This can result in a significant compile time.
If you know you are not going to need all the features, you can compile with only the features you need.

As of `v0.10.0`, compiling with no features is about 33% faster.
On a 3800X, it takes about 60 seconds for no features and 90 seconds for all.
This difference is expected to increase as the bar develops.

Features containing a `+` can be stacked, for example `config+json` and `config+yaml` could both be enabled.

To build using only specific features, disable default features and pass a comma separated list to `cargo build`:

```shell
cargo build --release --no-default-features \
--features http,config+json,clock
```

> ⚠ Make sure you enable at least one `config` feature otherwise you will not be able to start the bar!
| Feature | Description |
|---------------------|-----------------------------------------------------------------------------------|
| **Core** | |
| http | Enables HTTP features. Currently this includes the ability to load remote images. |
| config+all | Enables support for all configuration languages. |
| config+json | Enables configuration support for JSON. |
| config+yaml | Enables configuration support for YAML. |
| config+toml | Enables configuration support for TOML. |
| config+corn | Enables configuration support for [Corn](https://github.com/jakestanger.corn). |
| **Modules** | |
| clock | Enables the `clock` module. |
| music+all | Enables the `music` module with support for all player types. |
| music+mpris | Enables the `music` module with MPRIS support. |
| music+mpd | Enables the `music` module with MPD support |
| sys_info | Enables the `sys_info` module. |
| tray | Enables the `tray` module. |
| workspaces+all | Enables the `workspaces` module with support for all compositors. |
| workspaces+sway | Enables the `workspaces` module with support for Sway. |
| workspaces+hyprland | Enables the `workspaces` module with support for Hyprland. |

15 changes: 10 additions & 5 deletions src/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,20 @@ fn add_modules(content: &gtk::Box, modules: Vec<ModuleConfig>, info: &ModuleInfo

for (id, config) in modules.into_iter().enumerate() {
match config {
#[cfg(feature = "clock")]
ModuleConfig::Clock(mut module) => add_module!(module, id),
ModuleConfig::Custom(mut module) => add_module!(module, id),
ModuleConfig::Focused(mut module) => add_module!(module, id),
ModuleConfig::Launcher(mut module) => add_module!(module, id),
#[cfg(feature = "music")]
ModuleConfig::Music(mut module) => add_module!(module, id),
ModuleConfig::Script(mut module) => add_module!(module, id),
#[cfg(feature = "sys_info")]
ModuleConfig::SysInfo(mut module) => add_module!(module, id),
ModuleConfig::Focused(mut module) => add_module!(module, id),
ModuleConfig::Workspaces(mut module) => add_module!(module, id),
#[cfg(feature = "tray")]
ModuleConfig::Tray(mut module) => add_module!(module, id),
ModuleConfig::Music(mut module) => add_module!(module, id),
ModuleConfig::Launcher(mut module) => add_module!(module, id),
ModuleConfig::Custom(mut module) => add_module!(module, id),
#[cfg(feature = "workspaces")]
ModuleConfig::Workspaces(mut module) => add_module!(module, id),
}
}

Expand Down
19 changes: 17 additions & 2 deletions src/clients/compositor/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
use cfg_if::cfg_if;
use color_eyre::{Help, Report, Result};
use std::fmt::{Display, Formatter};
use tokio::sync::broadcast;
use tracing::debug;

#[cfg(feature = "workspaces+hyprland")]
pub mod hyprland;
#[cfg(feature = "workspaces+sway")]
pub mod sway;

pub enum Compositor {
#[cfg(feature = "workspaces+sway")]
Sway,
#[cfg(feature = "workspaces+hyprland")]
Hyprland,
Unsupported,
}
Expand All @@ -18,7 +23,9 @@ impl Display for Compositor {
f,
"{}",
match self {
#[cfg(feature = "workspaces+sway")]
Self::Sway => "Sway",
#[cfg(feature = "workspaces+hyprland")]
Self::Hyprland => "Hyprland",
Self::Unsupported => "Unsupported",
}
Expand All @@ -31,9 +38,15 @@ impl Compositor {
/// This is done by checking system env vars.
fn get_current() -> Self {
if std::env::var("SWAYSOCK").is_ok() {
Self::Sway
cfg_if! {
if #[cfg(feature = "workspaces+sway")] { Self::Sway }
else { tracing::error!("Not compiled with Sway support"); Self::Unsupported }
}
} else if std::env::var("HYPRLAND_INSTANCE_SIGNATURE").is_ok() {
Self::Hyprland
cfg_if! {
if #[cfg(feature = "workspaces+hyprland")] { Self::Hyprland}
else { tracing::error!("Not compiled with Hyprland support"); Self::Unsupported }
}
} else {
Self::Unsupported
}
Expand All @@ -44,7 +57,9 @@ impl Compositor {
let current = Self::get_current();
debug!("Getting workspace client for: {current}");
match current {
#[cfg(feature = "workspaces+sway")]
Self::Sway => Ok(sway::get_sub_client()),
#[cfg(feature = "workspaces+hyprland")]
Self::Hyprland => Ok(hyprland::get_client()),
Self::Unsupported => Err(Report::msg("Unsupported compositor")
.note("Currently workspaces are only supported by Sway and Hyprland")),
Expand Down
3 changes: 3 additions & 0 deletions src/clients/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#[cfg(feature = "workspaces")]
pub mod compositor;
#[cfg(feature = "music")]
pub mod music;
#[cfg(feature = "tray")]
pub mod system_tray;
pub mod wayland;
2 changes: 2 additions & 0 deletions src/clients/music/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use std::sync::Arc;
use std::time::Duration;
use tokio::sync::broadcast;

#[cfg(feature = "music+mpd")]
pub mod mpd;
#[cfg(feature = "music+mpris")]
pub mod mpris;

#[derive(Clone, Debug)]
Expand Down
7 changes: 6 additions & 1 deletion src/config/impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,16 @@ impl Config {
.unwrap_or_default();

match extension {
#[cfg(feature = "config+json")]
"json" => serde_json::from_str(str).wrap_err("Invalid JSON config"),
#[cfg(feature = "config+toml")]
"toml" => toml::from_str(str).wrap_err("Invalid TOML config"),
#[cfg(feature = "config+yaml")]
"yaml" | "yml" => serde_yaml::from_str(str).wrap_err("Invalid YAML config"),
#[cfg(feature = "config+corn")]
"corn" => libcorn::from_str(str).wrap_err("Invalid Corn config"),
_ => unreachable!(),
_ => Err(Report::msg(format!("Unsupported config type: {extension}"))
.note("You may need to recompile with support if available")),
}
}
}
20 changes: 15 additions & 5 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
mod r#impl;
mod truncate;

#[cfg(feature = "clock")]
use crate::modules::clock::ClockModule;
use crate::modules::custom::CustomModule;
use crate::modules::focused::FocusedModule;
use crate::modules::launcher::LauncherModule;
#[cfg(feature = "music")]
use crate::modules::music::MusicModule;
use crate::modules::script::ScriptModule;
#[cfg(feature = "sys_info")]
use crate::modules::sysinfo::SysInfoModule;
#[cfg(feature = "tray")]
use crate::modules::tray::TrayModule;
#[cfg(feature = "workspaces")]
use crate::modules::workspaces::WorkspacesModule;
use crate::script::ScriptInput;
use serde::Deserialize;
Expand All @@ -32,15 +37,20 @@ pub struct CommonConfig {
#[derive(Debug, Deserialize, Clone)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ModuleConfig {
#[cfg(feature = "clock")]
Clock(ClockModule),
Custom(CustomModule),
Focused(FocusedModule),
Launcher(LauncherModule),
#[cfg(feature = "music")]
Music(MusicModule),
Script(ScriptModule),
#[cfg(feature = "sys_info")]
SysInfo(SysInfoModule),
#[cfg(feature = "tray")]
Tray(TrayModule),
#[cfg(feature = "workspaces")]
Workspaces(WorkspacesModule),
SysInfo(SysInfoModule),
Launcher(LauncherModule),
Script(ScriptModule),
Focused(FocusedModule),
Custom(CustomModule),
}

#[derive(Debug, Clone)]
Expand Down
Loading

0 comments on commit c347b6c

Please sign in to comment.