Skip to content
Merged
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
93 changes: 92 additions & 1 deletion Cargo.lock

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

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "capybar"
version = "0.1.0"
version = "0.1.1"
edition = "2021"
license = "MIT"

Expand All @@ -14,6 +14,10 @@ wayland-protocols = "0.32.8"
thiserror = "2.0.12"
anyhow = "1.0.98"

#Config
toml = "0.8.23"
serde = {version = "1.0.219", features = [ "derive" ] }

### Widget dependencies
#Fonts
fontconfig = "0.9.0"
Expand Down
22 changes: 4 additions & 18 deletions examples/basic/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

// Fonts can be replaces by your liking. The first font added will be used for normal text, the
// second for emoji
capybar.add_font_by_name("mono")?;
//capybar.add_font_by_name("mono")?;
capybar.add_font_by_name("jetbrainsmononerdfont")?;
capybar.add_font_by_name("jetbrainsmononerdfont")?;

let mut bar = Bar::new(
Expand All @@ -59,13 +60,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
bar.create_child_left(
CPU::new,
CPUSettings {
text: TextSettings {
font_color: catpuccin_mocha.font,
size: 25.0,

..TextSettings::default()
},
icon: TextSettings {
text_settings: TextSettings {
font_color: catpuccin_mocha.font,
size: 25.0,

Expand All @@ -79,7 +74,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
},
)?;


//Center widgets
bar.create_child_center(
Clock::new,
Expand All @@ -91,18 +85,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
},
)?;


// Right widgets
bar.create_child_right(
Battery::new,
BatterySettings {
text: TextSettings {
font_color: catpuccin_mocha.font,
size: 25.0,

..TextSettings::default()
},
icon: TextSettings {
text_settings: TextSettings {
font_color: catpuccin_mocha.font,
size: 25.0,

Expand All @@ -116,7 +103,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
},
)?;


capybar.add_widget(bar)?;

capybar.init(&mut event_queue)?.run(&mut event_queue)?;
Expand Down
Binary file added examples/toml_config/bar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions examples/toml_config/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#TOML does not support variables interpolation, so this is just a place to copy values from
[palete]
font = 0xf5e0dcff
background = 0x1e1e2eff
border = 0x74c7ecff

[preloaded_fonts]
list = ["mono", "jetbrainsmononerdfont"]

[bar.settings]
width = 1920
background = 0x1e1e2eff
border = [1, 0x74c7ecff]

[[bar.left]]
widget = "cpu"
[bar.left.settings]
size = 24
font_color = 0xf5e0dcff
margin = [10,0,0,0]
update_rate = 1000

[[bar.center]]
widget = "clock"
[bar.center.settings]
size = 24
font_color = 0xf5e0dcff

[[bar.right]]
widget = "battery"
[bar.right.settings]
size = 24
font_color = 0xf5e0dcff
margin = [0,10,0,0]




17 changes: 17 additions & 0 deletions examples/toml_config/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use anyhow::Result;
use capybar::{config::Config, Root};
use wayland_client::{globals::registry_queue_init, Connection};

fn main() -> Result<()> {
let config = Config::parse_toml("./examples/toml_config/config.toml".into())?;

let conn = Connection::connect_to_env()?;
let (globals, mut event_queue) = registry_queue_init(&conn)?;

let mut capybar = Root::new(&globals, &mut event_queue)?;
capybar.apply_config(config)?;

capybar.init(&mut event_queue)?.run(&mut event_queue)?;

Ok(())
}
32 changes: 32 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
pub mod util;
pub mod widgets;

use anyhow::Result;
use serde::Deserialize;
use std::path::PathBuf;

use util::font::PreloadedFonts;
use widgets::bar::Bar;

#[derive(Deserialize, Debug)]
pub struct Config {
pub preloaded_fonts: PreloadedFonts,

pub bar: Bar,
}

impl Config {
pub const fn default() -> Self {
Self {
preloaded_fonts: PreloadedFonts::default(),
bar: Bar::default(),
}
}

pub fn parse_toml(file: PathBuf) -> Result<Self> {
let content = std::fs::read_to_string(file)?;
let t: Config = toml::from_str(&content)?;

Ok(t)
}
}
84 changes: 84 additions & 0 deletions src/config/util/font.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use serde::{de::Visitor, Deserialize};

use crate::util::{fonts, Color};

#[derive(Default, Deserialize, Debug)]
pub struct PreloadedFonts {
pub list: Vec<Font>,
}

impl PreloadedFonts {
pub const fn default() -> Self {
Self { list: Vec::new() }
}
}

#[derive(Debug, Clone)]
pub struct Font {
pub name: String,
}

impl<'de> Deserialize<'de> for Font {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct FontVisitor;

impl<'de> Visitor<'de> for FontVisitor {
type Value = Font;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("Expected font name that can be found using fontconfig")
}

fn visit_str<E>(self, name: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
match fonts::add_font_by_name(name) {
Ok(_) => Ok(Font {
name: name.to_string(),
}),
Err(e) => Err(E::custom(e.to_string())),
}
}
}
deserializer.deserialize_str(FontVisitor)
}
}

#[derive(Deserialize, Debug, Clone, Default)]
pub struct FontStyle {
pub name: String,
#[serde(default = "FontStyle::default_text_size")]
pub size: usize,
#[serde(default = "FontStyle::default_text_color")]
pub color: Color,
}

impl Font {
pub const fn default() -> Self {
Self {
name: String::new(),
}
}
}

impl FontStyle {
pub const fn default() -> Self {
Self {
name: String::new(),
size: 0,
color: Color::NONE,
}
}

pub const fn default_text_color() -> Color {
Color::BLACK
}

pub const fn default_text_size() -> usize {
12
}
}
3 changes: 3 additions & 0 deletions src/config/util/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod style;

pub mod font;
Loading
Loading