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

add "material-nf" icon set, add icons_format config option #1095

Merged
merged 10 commits into from
Feb 27, 2021
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ There are some top-level configuration variables:

Key | Description | Required | Default
----|-------------|----------|--------
`icons` | The icon set that should be used. Possible values are `none`, `awesome`, `awesome5` and `material`. Check [themes.md](https://github.com/greshake/i3status-rust/blob/master/themes.md) for more information | No | `none`
`icons` | The icon set that should be used. Possible values are `none`, `awesome`, `awesome5`, `material` and `material-nf`. Check [themes.md](https://github.com/greshake/i3status-rust/blob/master/themes.md) for more information | No | `none`
`icons_format` | A string to customise the appearance of each icon. Can be used to edit icons' spacing or specify a font that will be applied only to icons via pango markup. For example, set it to `" <span font_family='NotoSans Nerd Font'>{icon}</span> "` to set font of the icons to be 'NotoSans Nerd Font' | No | `" {icon} "`
`theme` | The predefined theme that should be used. You can also add your own overrides. Check [themes.md](https://github.com/greshake/i3status-rust/blob/master/themes.md) for all available themes. | No | `plain`
`scrolling` | The direction of scrolling, either `natural` or `reverse` | No | `reverse`
`block` | All blocks that will exist in your i3bar. Check [blocks.md](https://github.com/greshake/i3status-rust/blob/master/blocks.md) for all blocks and their parameters. | No | none
Expand Down
3 changes: 3 additions & 0 deletions src/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ macro_rules! block {
if let Some(ref overrides) = common_config.theme_overrides {
$shared_config.theme_override(overrides)?;
}
if let Some(overrides) = common_config.icons_format {
$shared_config.icons_format_override(overrides);
}

// Extract block-specific config
let block_config = <$block_type as ConfigBlock>::Config::deserialize($block_config)
Expand Down
25 changes: 18 additions & 7 deletions src/blocks/backlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,24 @@ impl Block for Backlight {
if self.invert_icons {
brightness = 100 - brightness;
}
match brightness {
0..=19 => self.output.set_icon("backlight_empty"),
20..=39 => self.output.set_icon("backlight_partial1"),
40..=59 => self.output.set_icon("backlight_partial2"),
60..=79 => self.output.set_icon("backlight_partial3"),
_ => self.output.set_icon("backlight_full"),
}
self.output.set_icon(match brightness {
0..=6 => "backlight_empty",
7..=13 => "backlight_1",
14..=20 => "backlight_2",
21..=26 => "backlight_3",
27..=33 => "backlight_4",
34..=40 => "backlight_5",
41..=46 => "backlight_6",
47..=53 => "backlight_7",
54..=60 => "backlight_8",
61..=67 => "backlight_9",
68..=73 => "backlight_10",
74..=80 => "backlight_11",
81..=87 => "backlight_12",
88..=93 => "backlight_13",
_ => "backlight_full",
});

Ok(None)
}

Expand Down
3 changes: 2 additions & 1 deletion src/blocks/base_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ pub(super) struct BaseBlockConfig {
pub on_click: Option<String>,

pub theme_overrides: Option<HashMap<String, String>>,
pub icons_format: Option<String>,
}

impl BaseBlockConfig {
const FIELDS: &'static [&'static str] = &["on_click", "theme_overrides"];
const FIELDS: &'static [&'static str] = &["on_click", "theme_overrides", "icons_format"];

// FIXME: this function is to paper over https://github.com/serde-rs/serde/issues/1957
pub(super) fn extract(config: &mut Value) -> Value {
Expand Down
30 changes: 24 additions & 6 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ use crate::icons;
use crate::input::MouseButton;
use crate::themes::Theme;

////////////////

#[derive(Debug)]
pub struct SharedConfig {
pub theme: Rc<Theme>,
pub icons: Rc<HashMap<String, String>>,
icons: Rc<HashMap<String, String>>,
icons_format: String,
pub scrolling: Scrolling,
}

Expand All @@ -28,10 +27,15 @@ impl SharedConfig {
Self {
theme: Rc::new(config.theme.clone()),
icons: Rc::new(config.icons.clone()),
icons_format: config.icons_format.clone(),
scrolling: config.scrolling,
}
}

pub fn icons_format_override(&mut self, icons_format: String) {
self.icons_format = icons_format;
}

pub fn theme_override(&mut self, overrides: &HashMap<String, String>) -> errors::Result<()> {
let mut theme = self.theme.as_ref().clone();
for entry in overrides {
Expand Down Expand Up @@ -59,7 +63,11 @@ impl SharedConfig {
}

pub fn get_icon(&self, icon: &str) -> Option<String> {
self.icons.get(icon).map(|s| s.to_string())
Some(
self.icons_format
.clone()
.replace("{icon}", self.icons.get(icon)?),
)
}
}

Expand All @@ -68,6 +76,7 @@ impl Default for SharedConfig {
Self {
theme: Rc::new(Theme::default()),
icons: Rc::new(icons::default()),
icons_format: " {icon} ".to_string(),
scrolling: Scrolling::default(),
}
}
Expand All @@ -78,13 +87,12 @@ impl Clone for SharedConfig {
Self {
theme: Rc::clone(&self.theme),
icons: Rc::clone(&self.icons),
icons_format: self.icons_format.clone(),
scrolling: self.scrolling,
}
}
}

///////////////////

#[derive(Deserialize, Debug, Clone)]
pub struct Config {
#[serde(default = "icons::default", deserialize_with = "deserialize_icons")]
Expand All @@ -93,6 +101,9 @@ pub struct Config {
#[serde(default = "Theme::default")]
pub theme: Theme,

#[serde(default = "Config::default_icons_format")]
pub icons_format: String,

#[serde(default = "Scrolling::default")]
pub scrolling: Scrolling,
/// Direction of scrolling, "natural" or "reverse".
Expand All @@ -104,11 +115,18 @@ pub struct Config {
pub blocks: Vec<(String, value::Value)>,
}

impl Config {
fn default_icons_format() -> String {
" {icon} ".to_string()
}
}

impl Default for Config {
fn default() -> Self {
Config {
icons: icons::default(),
theme: Theme::default(),
icons_format: Config::default_icons_format(),
scrolling: Scrolling::default(),
blocks: Vec::new(),
}
Expand Down
Loading