Placeholder substitutions for clicks

main
Tassilo Horn 3 years ago
parent 59664d3b21
commit 2b9d4bf198
  1. 16
      Cargo.lock
  2. 5
      swayrbar/src/module.rs
  3. 79
      swayrbar/src/module/battery.rs
  4. 23
      swayrbar/src/module/date.rs
  5. 5
      swayrbar/src/module/sysinfo.rs

16
Cargo.lock generated

@ -279,9 +279,9 @@ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.121" version = "0.2.122"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "efaa7b300f3b5fe8eb6bf21ce3895e1751d9665086af2d64b42f19701015ff4f" checksum = "ec647867e2bf0772e28c8bcde4f0d19a9216916e890543b5a03ed8ef27b8f259"
[[package]] [[package]]
name = "log" name = "log"
@ -413,9 +413,9 @@ dependencies = [
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.36" version = "1.0.37"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" checksum = "ec757218438d5fda206afc041538b2f6d889286160d649a86a24d37e1235afd1"
dependencies = [ dependencies = [
"unicode-xid", "unicode-xid",
] ]
@ -652,9 +652,9 @@ dependencies = [
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.90" version = "1.0.91"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "704df27628939572cd88d33f171cd6f896f4eaca85252c6e0a72d8d8287ee86f" checksum = "b683b2b825c8eef438b77c36a06dc262294da3d5a5813fac20da149241dcd44d"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -663,9 +663,9 @@ dependencies = [
[[package]] [[package]]
name = "sysinfo" name = "sysinfo"
version = "0.23.6" version = "0.23.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f3c0db8e08e06cfd352a043bd0143498fb7d42783a6e941da83b55464eb27d2" checksum = "b3fb8adaa82317f1e8a040281807f411803c9111303cfe129b4abb4a14b2c223"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"core-foundation-sys 0.8.3", "core-foundation-sys 0.8.3",

@ -44,8 +44,5 @@ pub trait BarModuleFn: Sync + Send {
} }
} }
fn build(&self) -> s::Block; fn build(&self) -> s::Block;
fn subst_args<'a>(&'a self, _cmd: &'a [String]) -> Option<Vec<String>> { fn subst_args<'a>(&'a self, _cmd: &'a [String]) -> Option<Vec<String>>;
// None means, no substitution are to be made.
None
}
} }

@ -20,12 +20,20 @@ use crate::module::BarModuleFn;
use crate::shared::fmt::format_placeholders; use crate::shared::fmt::format_placeholders;
use battery as bat; use battery as bat;
use std::collections::HashSet; use std::collections::HashSet;
use std::sync::Mutex;
use swaybar_types as s; use swaybar_types as s;
const NAME: &str = "battery"; const NAME: &str = "battery";
struct State {
state_of_charge: f32,
state_of_health: f32,
state: String,
}
pub struct BarModuleBattery { pub struct BarModuleBattery {
config: config::ModuleConfig, config: config::ModuleConfig,
state: Mutex<State>,
} }
fn get_refreshed_batteries( fn get_refreshed_batteries(
@ -42,27 +50,25 @@ fn get_refreshed_batteries(
Ok(bats) Ok(bats)
} }
fn get_text(cfg: &config::ModuleConfig) -> String { fn set_state(state: &Mutex<State>) {
// FIXME: Creating the Manager on every refresh is bad but internally // FIXME: Creating the Manager on every refresh is bad but internally
// it uses an Rc so if I keep it as a field of BarModuleBattery, that // it uses an Rc so if I keep it as a field of BarModuleBattery, that
// cannot be Sync. // cannot be Sync.
let manager = battery::Manager::new().unwrap(); let manager = battery::Manager::new().unwrap();
match get_refreshed_batteries(&manager) { match get_refreshed_batteries(&manager) {
Ok(bats) => { Ok(bats) => {
if bats.is_empty() { let mut state = state.lock().expect("Could not lock state.");
return String::new(); state.state_of_charge =
} bats.iter().map(|b| b.state_of_charge().value).sum::<f32>()
format_placeholders!(&cfg.format, cfg.is_html_escape(), { / bats.len() as f32
"state_of_charge" => bats.iter() * 100_f32;
.map(|b| b.state_of_charge().value) state.state_of_health =
.sum::<f32>() bats.iter().map(|b| b.state_of_health().value).sum::<f32>()
/ bats.len() as f32 * 100_f32, / bats.len() as f32
"state_of_health" => bats.iter() * 100_f32;
.map(|b| b.state_of_health().value) state.state = {
.sum::<f32>() let states = bats
/ bats.len() as f32 * 100_f32, .iter()
"state" => {
let states = bats.iter()
.map(|b| format!("{:?}", b.state())) .map(|b| format!("{:?}", b.state()))
.collect::<HashSet<String>>(); .collect::<HashSet<String>>();
if states.len() == 1 { if states.len() == 1 {
@ -75,23 +81,39 @@ fn get_text(cfg: &config::ModuleConfig) -> String {
comma_sep_string = comma_sep_string + &state; comma_sep_string = comma_sep_string + &state;
first = false; first = false;
} else { } else {
comma_sep_string = comma_sep_string comma_sep_string = comma_sep_string + ", " + &state;
+ ", " + &state;
} }
} }
comma_sep_string += "]"; comma_sep_string += "]";
comma_sep_string comma_sep_string
} }
},
})
} }
Err(err) => format!("{}", err), }
Err(err) => {
log::error!("Could not update battery state: {}", err);
}
} }
} }
fn get_text(fmt: &str, html_escape: bool, state: &Mutex<State>) -> String {
let state = state.lock().expect("Could not lock state.");
format_placeholders!(fmt, html_escape, {
"state_of_charge" => state.state_of_charge,
"state_of_health" => state.state_of_health,
"state" => state.state.as_str(),
})
}
impl BarModuleFn for BarModuleBattery { impl BarModuleFn for BarModuleBattery {
fn create(config: config::ModuleConfig) -> Box<dyn BarModuleFn> { fn create(config: config::ModuleConfig) -> Box<dyn BarModuleFn> {
Box::new(BarModuleBattery { config }) Box::new(BarModuleBattery {
config,
state: Mutex::new(State {
state_of_charge: 0.0,
state_of_health: 0.0,
state: "Unknown".to_owned(),
}),
})
} }
fn default_config(instance: String) -> config::ModuleConfig { fn default_config(instance: String) -> config::ModuleConfig {
@ -109,7 +131,12 @@ impl BarModuleFn for BarModuleBattery {
} }
fn build(&self) -> s::Block { fn build(&self) -> s::Block {
let text = get_text(&self.config); set_state(&self.state);
let text = get_text(
&self.config.format,
self.config.is_html_escape(),
&self.state,
);
s::Block { s::Block {
name: Some(NAME.to_owned()), name: Some(NAME.to_owned()),
instance: Some(self.config.instance.clone()), instance: Some(self.config.instance.clone()),
@ -130,4 +157,12 @@ impl BarModuleFn for BarModuleBattery {
separator_block_width: None, separator_block_width: None,
} }
} }
fn subst_args<'a>(&'a self, cmd: &'a [String]) -> Option<Vec<String>> {
Some(
cmd.iter()
.map(|arg| get_text(arg, false, &self.state))
.collect(),
)
}
} }

@ -25,6 +25,10 @@ pub struct BarModuleDate {
config: config::ModuleConfig, config: config::ModuleConfig,
} }
fn chrono_format(s: &str) -> String {
chrono::Local::now().format(s).to_string()
}
impl BarModuleFn for BarModuleDate { impl BarModuleFn for BarModuleDate {
fn create(cfg: config::ModuleConfig) -> Box<dyn BarModuleFn> { fn create(cfg: config::ModuleConfig) -> Box<dyn BarModuleFn> {
Box::new(BarModuleDate { config: cfg }) Box::new(BarModuleDate { config: cfg })
@ -44,8 +48,21 @@ impl BarModuleFn for BarModuleDate {
&self.config &self.config
} }
fn get_on_click_map(
&self,
name: &str,
instance: &str,
) -> Option<&std::collections::HashMap<String, Vec<String>>> {
let cfg = self.get_config();
if name == cfg.name && instance == cfg.instance {
cfg.on_click.as_ref()
} else {
None
}
}
fn build(&self) -> s::Block { fn build(&self) -> s::Block {
let text = chrono::Local::now().format(&self.config.format).to_string(); let text = chrono_format(&self.config.format);
s::Block { s::Block {
name: Some(NAME.to_owned()), name: Some(NAME.to_owned()),
instance: Some(self.config.instance.clone()), instance: Some(self.config.instance.clone()),
@ -66,4 +83,8 @@ impl BarModuleFn for BarModuleDate {
separator_block_width: None, separator_block_width: None,
} }
} }
fn subst_args<'a>(&'a self, cmd: &'a [String]) -> Option<Vec<String>> {
Some(cmd.iter().map(|arg| chrono_format(arg)).collect())
}
} }

@ -145,4 +145,9 @@ impl BarModuleFn for BarModuleSysInfo {
separator_block_width: None, separator_block_width: None,
} }
} }
fn subst_args<'a>(&'a self, _cmd: &'a [String]) -> Option<Vec<String>> {
// TOOD: Set a State we can refer to here.
todo!()
}
} }

Loading…
Cancel
Save