further refactor

main
Tassilo Horn 3 years ago
parent 55521a9c97
commit f484eb0420
  1. 4
      Cargo.lock
  2. 30
      swayrbar/src/bar.rs
  3. 17
      swayrbar/src/module.rs
  4. 4
      swayrbar/src/module/battery.rs
  5. 4
      swayrbar/src/module/date.rs
  6. 4
      swayrbar/src/module/pactl.rs
  7. 4
      swayrbar/src/module/sysinfo.rs
  8. 4
      swayrbar/src/module/window.rs

4
Cargo.lock generated

@ -570,9 +570,9 @@ dependencies = [
[[package]] [[package]]
name = "serde_json" name = "serde_json"
version = "1.0.80" version = "1.0.81"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f972498cf015f7c0746cac89ebe1d6ef10c293b94175a243a2d9442c163d9944" checksum = "9b7ce2b32a1aed03c558dc61a5cd328f15aff2dbc17daad8fb8af04d2100e15c"
dependencies = [ dependencies = [
"itoa", "itoa",
"ryu", "ryu",

@ -17,7 +17,7 @@
use crate::config; use crate::config;
use crate::module; use crate::module;
use crate::module::{BarModuleFn, NameAndInstance}; use crate::module::{BarModuleFn, NameInstanceAndReason, RefreshReason};
use env_logger::Env; use env_logger::Env;
use serde_json; use serde_json;
use std::io; use std::io;
@ -30,14 +30,6 @@ use std::{sync::Arc, thread};
use swaybar_types as sbt; use swaybar_types as sbt;
use swayipc as si; use swayipc as si;
#[derive(Debug)]
enum RefreshReason {
ClickEvent,
SwayEvent,
}
type NameInstanceAndReason = (String, String, RefreshReason);
pub fn start() { pub fn start() {
env_logger::Builder::from_env(Env::default().default_filter_or("warn")) env_logger::Builder::from_env(Env::default().default_filter_or("warn"))
.init(); .init();
@ -56,7 +48,7 @@ pub fn start() {
let sender_for_input = sender.clone(); let sender_for_input = sender.clone();
thread::spawn(move || handle_input(mods_for_input, sender_for_input)); thread::spawn(move || handle_input(mods_for_input, sender_for_input));
let window_mods: Vec<NameAndInstance> = mods let window_mods: Vec<(String, String)> = mods
.iter() .iter()
.filter(|m| m.get_config().name == "window") .filter(|m| m.get_config().name == "window")
.map(|m| (m.get_config().name.clone(), m.get_config().instance.clone())) .map(|m| (m.get_config().name.clone(), m.get_config().instance.clone()))
@ -134,8 +126,8 @@ fn handle_input(
} }
}; };
log::debug!("Received click: {:?}", click); log::debug!("Received click: {:?}", click);
if let Some((name, instance)) = handle_click(click, mods.clone()) { let event = handle_click(click, mods.clone());
let event = Some((name, instance, RefreshReason::ClickEvent)); if event.is_some() {
send_refresh_event(&sender, event); send_refresh_event(&sender, event);
} }
} }
@ -156,7 +148,7 @@ fn send_refresh_event(
fn handle_click( fn handle_click(
click: sbt::Click, click: sbt::Click,
mods: Arc<Vec<Box<dyn BarModuleFn>>>, mods: Arc<Vec<Box<dyn BarModuleFn>>>,
) -> Option<NameAndInstance> { ) -> Option<NameInstanceAndReason> {
let name = click.name?; let name = click.name?;
let instance = click.instance?; let instance = click.instance?;
let button_str = format!("{:?}", click.button); let button_str = format!("{:?}", click.button);
@ -176,7 +168,11 @@ fn handle_click(
if cfg.name == module::window::NAME { if cfg.name == module::window::NAME {
return None; return None;
} }
return Some((cfg.name.clone(), cfg.instance.clone())); return Some((
cfg.name.clone(),
cfg.instance.clone(),
RefreshReason::ClickEvent,
));
} }
} }
} }
@ -212,7 +208,7 @@ fn sway_subscribe() -> si::Fallible<si::EventStream> {
} }
fn handle_sway_events( fn handle_sway_events(
window_mods: Vec<NameAndInstance>, window_mods: Vec<(String, String)>,
sender: SyncSender<Option<NameInstanceAndReason>>, sender: SyncSender<Option<NameInstanceAndReason>>,
) { ) {
let mut resets = 0; let mut resets = 0;
@ -275,7 +271,7 @@ fn handle_sway_events(
fn generate_status_1( fn generate_status_1(
mods: &[Box<dyn BarModuleFn>], mods: &[Box<dyn BarModuleFn>],
name_and_instance: &Option<NameAndInstance>, name_and_instance: &Option<NameInstanceAndReason>,
) { ) {
let mut blocks = vec![]; let mut blocks = vec![];
for m in mods { for m in mods {
@ -296,6 +292,6 @@ fn generate_status(
println!("["); println!("[");
for ev in receiver.iter() { for ev in receiver.iter() {
generate_status_1(mods, &ev.map(|x| (x.0, x.1))) generate_status_1(mods, &ev)
} }
} }

@ -24,13 +24,22 @@ pub mod pactl;
pub mod sysinfo; pub mod sysinfo;
pub mod window; pub mod window;
pub type NameAndInstance = (String, String); #[derive(Debug)]
pub enum RefreshReason {
ClickEvent,
SwayEvent,
}
pub type NameInstanceAndReason = (String, String, RefreshReason);
fn should_refresh(m: &dyn BarModuleFn, nai: &Option<NameAndInstance>) -> bool { fn should_refresh(
m: &dyn BarModuleFn,
nai: &Option<NameInstanceAndReason>,
) -> bool {
let cfg = m.get_config(); let cfg = m.get_config();
match nai { match nai {
None => true, None => true,
Some((n, i)) => n == &cfg.name && i == &cfg.instance, Some((n, i, _)) => n == &cfg.name && i == &cfg.instance,
} }
} }
@ -54,6 +63,6 @@ pub trait BarModuleFn: Sync + Send {
None None
} }
} }
fn build(&self, nai: &Option<NameAndInstance>) -> s::Block; fn build(&self, nai: &Option<NameInstanceAndReason>) -> 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>>;
} }

@ -16,7 +16,7 @@
//! The date `swayrbar` module. //! The date `swayrbar` module.
use crate::config; use crate::config;
use crate::module::{should_refresh, BarModuleFn, NameAndInstance}; use crate::module::{should_refresh, BarModuleFn, NameInstanceAndReason};
use crate::shared::fmt::subst_placeholders; use crate::shared::fmt::subst_placeholders;
use battery as bat; use battery as bat;
use std::collections::HashSet; use std::collections::HashSet;
@ -128,7 +128,7 @@ impl BarModuleFn for BarModuleBattery {
&self.config &self.config
} }
fn build(&self, nai: &Option<NameAndInstance>) -> s::Block { fn build(&self, nai: &Option<NameInstanceAndReason>) -> s::Block {
let mut state = self.state.lock().expect("Could not lock state."); let mut state = self.state.lock().expect("Could not lock state.");
if should_refresh(self, nai) { if should_refresh(self, nai) {

@ -16,7 +16,7 @@
//! The date `swayrbar` module. //! The date `swayrbar` module.
use crate::module::config; use crate::module::config;
use crate::module::{BarModuleFn, NameAndInstance}; use crate::module::{BarModuleFn, NameInstanceAndReason};
use swaybar_types as s; use swaybar_types as s;
const NAME: &str = "date"; const NAME: &str = "date";
@ -61,7 +61,7 @@ impl BarModuleFn for BarModuleDate {
} }
} }
fn build(&self, _nai: &Option<NameAndInstance>) -> s::Block { fn build(&self, _nai: &Option<NameInstanceAndReason>) -> s::Block {
let text = chrono_format(&self.config.format); let text = chrono_format(&self.config.format);
s::Block { s::Block {
name: Some(NAME.to_owned()), name: Some(NAME.to_owned()),

@ -16,7 +16,7 @@
//! The pactl `swayrbar` module. //! The pactl `swayrbar` module.
use crate::config; use crate::config;
use crate::module::{should_refresh, BarModuleFn, NameAndInstance}; use crate::module::{should_refresh, BarModuleFn, NameInstanceAndReason};
use crate::shared::fmt::subst_placeholders; use crate::shared::fmt::subst_placeholders;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use regex::Regex; use regex::Regex;
@ -142,7 +142,7 @@ impl BarModuleFn for BarModulePactl {
&self.config &self.config
} }
fn build(&self, nai: &Option<NameAndInstance>) -> s::Block { fn build(&self, nai: &Option<NameInstanceAndReason>) -> s::Block {
let mut state = self.state.lock().expect("Could not lock state."); let mut state = self.state.lock().expect("Could not lock state.");
if should_refresh(self, nai) { if should_refresh(self, nai) {

@ -16,7 +16,7 @@
//! The date `swayrbar` module. //! The date `swayrbar` module.
use crate::config; use crate::config;
use crate::module::{should_refresh, BarModuleFn, NameAndInstance}; use crate::module::{should_refresh, BarModuleFn, NameInstanceAndReason};
use crate::shared::fmt::subst_placeholders; use crate::shared::fmt::subst_placeholders;
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Mutex; use std::sync::Mutex;
@ -145,7 +145,7 @@ impl BarModuleFn for BarModuleSysInfo {
&self.config &self.config
} }
fn build(&self, nai: &Option<NameAndInstance>) -> s::Block { fn build(&self, nai: &Option<NameInstanceAndReason>) -> s::Block {
let mut sys = self.system.lock().expect("Could not lock state."); let mut sys = self.system.lock().expect("Could not lock state.");
let mut state = self.state.lock().expect("Could not lock state."); let mut state = self.state.lock().expect("Could not lock state.");

@ -19,7 +19,7 @@ use std::collections::HashMap;
use std::sync::Mutex; use std::sync::Mutex;
use crate::config; use crate::config;
use crate::module::{should_refresh, BarModuleFn, NameAndInstance}; use crate::module::{should_refresh, BarModuleFn, NameInstanceAndReason};
use crate::shared::fmt::subst_placeholders; use crate::shared::fmt::subst_placeholders;
use crate::shared::ipc; use crate::shared::ipc;
use crate::shared::ipc::NodeMethods; use crate::shared::ipc::NodeMethods;
@ -112,7 +112,7 @@ impl BarModuleFn for BarModuleWindow {
&self.config &self.config
} }
fn build(&self, nai: &Option<NameAndInstance>) -> s::Block { fn build(&self, nai: &Option<NameInstanceAndReason>) -> s::Block {
let mut state = self.state.lock().expect("Could not lock state."); let mut state = self.state.lock().expect("Could not lock state.");
// In contrast to other modules, this one should only refresh its state // In contrast to other modules, this one should only refresh its state

Loading…
Cancel
Save