Skip to content

Commit

Permalink
pulse onceselled
Browse files Browse the repository at this point in the history
  • Loading branch information
doums committed Aug 18, 2024
1 parent a92dcfc commit d2d6825
Show file tree
Hide file tree
Showing 19 changed files with 75 additions and 114 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ build = "build.rs"

[dependencies]
anyhow = "1.0.86"
thiserror = "1.0"
clap = { version = "4.5", features = ["derive"] }
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.9"
Expand All @@ -18,6 +17,7 @@ tracing-subscriber = { version = "0.3.1", features = [
"env-filter",
] }
tracing-appender = "0.2"
once_cell = "1.19.0"
chrono = "0.4"
regex = "1"

Expand Down
33 changes: 22 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod pulse;
pub mod trace;
pub mod util;

use anyhow::{anyhow, Result};
use error::Error;
use module::{Bar, ModuleData};
use modules::battery::Config as BatteryConfig;
Expand All @@ -24,11 +25,10 @@ use modules::sound::Config as SoundConfig;
use modules::temperature::Config as TemperatureConfig;
use modules::wired::Config as WiredConfig;
use modules::wireless::Config as WirelessConfig;
use pulse::Pulse;
use serde::{Deserialize, Serialize};
use std::sync::mpsc::{self, Receiver, Sender};
use std::sync::{Arc, Mutex};
use std::thread;
use tracing::{error, info, instrument};

#[derive(Debug)]
pub struct ModuleMsg(char, Option<String>, Option<String>);
Expand All @@ -55,7 +55,6 @@ pub struct Baru<'a> {
config: &'a Config,
modules: Vec<ModuleData<'a>>,
format: &'a str,
pulse: &'a Arc<Mutex<Pulse>>,
markup_matches: Vec<MarkupMatch>,
channel: (Sender<ModuleMsg>, Receiver<ModuleMsg>),
}
Expand All @@ -64,48 +63,58 @@ pub struct Baru<'a> {
struct MarkupMatch(char, usize);

impl<'a> Baru<'a> {
pub fn with_config(config: &'a Config, pulse: &'a Arc<Mutex<Pulse>>) -> Result<Self, Error> {
#[instrument(skip_all)]
pub fn with_config(config: &'a Config) -> Result<Self> {
let mut modules = vec![];
let markup_matches = parse_format(&config.format);
for markup in &markup_matches {
modules.push(ModuleData::new(markup.0, config)?);
}
Ok(Baru {
config,
pulse,
channel: mpsc::channel(),
modules,
format: &config.format,
markup_matches,
})
}

pub fn start(&self) -> Result<(), Error> {
#[instrument(skip_all)]
pub fn start(&self) -> Result<()> {
// check if any module needs pulse, i.e. sound or mic modules
let need_pulse = self.modules.iter().any(|m| m.key == 's' || m.key == 'i');
if need_pulse {
pulse::init(self.config);
}
for data in &self.modules {
let builder = thread::Builder::new().name(format!("mod_{}", data.module.name()));
let cloned_m_conf = self.config.clone();
let tx1 = mpsc::Sender::clone(&self.channel.0);
let pulse = Arc::clone(self.pulse);
let run = data.module.run_fn();
let key = data.key;
let c_name = data.module.name().to_string();
builder.spawn(move || -> Result<(), Error> {
run(key, cloned_m_conf, pulse, tx1)?;
info!("[{}] module starting", c_name);
run(key, cloned_m_conf, tx1)
.inspect_err(|e| error!("[{}] module failed: {}", c_name, e))?;
Ok(())
})?;
}
Ok(())
}

fn module_output(&self, key: char) -> Result<&str, Error> {
#[instrument(skip(self))]
fn module_output(&self, key: char) -> Result<&str> {
let module = self
.modules
.iter()
.find(|data| data.key == key)
.ok_or(format!("module for key \"{}\" not found", key))?;
.ok_or(anyhow!("module for key \"{}\" not found", key))?;
Ok(module.output())
}

pub fn update(&mut self) -> Result<(), Error> {
#[instrument(skip(self))]
pub fn update(&mut self) -> Result<()> {
let messages: Vec<ModuleMsg> = self.channel.1.try_iter().collect();
for module in &mut self.modules {
let mut iter = messages.iter().rev();
Expand All @@ -123,11 +132,13 @@ impl<'a> Baru<'a> {
Ok(())
}

#[instrument(skip(self))]
pub fn modules(&self) -> Vec<&str> {
self.modules.iter().map(|m| m.module.name()).collect()
}
}

#[instrument]
fn parse_format(format: &str) -> Vec<MarkupMatch> {
let mut matches = vec![];
let mut iter = format.char_indices().peekable();
Expand Down
7 changes: 1 addition & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@

use anyhow::{Context, Result};
use baru::cli::Cli;
use baru::pulse::Pulse;
use baru::{trace, util, Baru, Config};
use clap::Parser;
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::{Duration, Instant};
use tracing::{debug, error, info};
Expand Down Expand Up @@ -43,10 +41,7 @@ fn main() -> Result<()> {
Some(ms) => Duration::from_millis(ms as u64),
None => TICK_RATE,
};
let pulse = Arc::new(Mutex::new(Pulse::new(&config).inspect_err(|e| {
error!("baru: error while creating pulse module, {}", e);
})?));
let mut baru = Baru::with_config(&config, &pulse)
let mut baru = Baru::with_config(&config)
.inspect_err(|e| error!("failed to create baru instance {}", e))?;
info!("baru instance initialized");

Expand Down
4 changes: 1 addition & 3 deletions src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ use crate::modules::wired::Wired;
use crate::modules::wireless::Wireless;
use crate::Config;
use crate::ModuleMsg;
use crate::Pulse;
use std::convert::TryFrom;
use std::sync::mpsc::Sender;
use std::sync::{Arc, Mutex};

pub type RunPtr = fn(char, Config, Arc<Mutex<Pulse>>, Sender<ModuleMsg>) -> Result<(), Error>;
pub type RunPtr = fn(char, Config, Sender<ModuleMsg>) -> Result<(), Error>;

pub trait Bar {
fn name(&self) -> &str;
Expand Down
9 changes: 1 addition & 8 deletions src/modules/battery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@

use crate::error::Error;
use crate::module::{Bar, RunPtr};
use crate::pulse::Pulse;
use crate::{Config as MainConfig, ModuleMsg};
use serde::{Deserialize, Serialize};
use std::convert::TryFrom;
use std::fs::{self, File};
use std::io::{self, prelude::*, BufReader};
use std::sync::mpsc::Sender;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::{Duration, Instant};
use tracing::{debug, instrument};
Expand Down Expand Up @@ -175,12 +173,7 @@ impl<'a> Bar for Battery<'a> {
}

#[instrument(skip_all)]
pub fn run(
key: char,
main_config: MainConfig,
_: Arc<Mutex<Pulse>>,
tx: Sender<ModuleMsg>,
) -> Result<(), Error> {
pub fn run(key: char, main_config: MainConfig, tx: Sender<ModuleMsg>) -> Result<(), Error> {
let config = InternalConfig::try_from(&main_config)?;
debug!("{:#?}", config);
let mut iteration_start: Instant;
Expand Down
9 changes: 1 addition & 8 deletions src/modules/brightness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
use crate::error::Error;
use crate::module::{Bar, RunPtr};
use crate::util::read_and_parse;
use crate::Pulse;
use crate::{Config as MainConfig, ModuleMsg};
use serde::{Deserialize, Serialize};
use std::sync::mpsc::Sender;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::{Duration, Instant};
use tracing::{debug, instrument};
Expand Down Expand Up @@ -104,12 +102,7 @@ impl<'a> Bar for Brightness<'a> {
}

#[instrument(skip_all)]
pub fn run(
key: char,
main_config: MainConfig,
_: Arc<Mutex<Pulse>>,
tx: Sender<ModuleMsg>,
) -> Result<(), Error> {
pub fn run(key: char, main_config: MainConfig, tx: Sender<ModuleMsg>) -> Result<(), Error> {
let config = InternalConfig::from(&main_config);
debug!("{:#?}", config);
let mut iteration_start: Instant;
Expand Down
9 changes: 1 addition & 8 deletions src/modules/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@

use crate::error::Error;
use crate::module::{Bar, RunPtr};
use crate::Pulse;
use crate::{Config as MainConfig, ModuleMsg};
use serde::{Deserialize, Serialize};
use std::fs::File;
use std::io::prelude::*;
use std::io::BufReader;
use std::sync::mpsc::Sender;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::{Duration, Instant};
use tracing::{debug, instrument};
Expand Down Expand Up @@ -117,12 +115,7 @@ impl<'a> Bar for Cpu<'a> {
}

#[instrument(skip_all)]
pub fn run(
key: char,
main_config: MainConfig,
_: Arc<Mutex<Pulse>>,
tx: Sender<ModuleMsg>,
) -> Result<(), Error> {
pub fn run(key: char, main_config: MainConfig, tx: Sender<ModuleMsg>) -> Result<(), Error> {
let config = InternalConfig::from(&main_config);
debug!("{:#?}", config);
let mut prev_idle = 0;
Expand Down
9 changes: 1 addition & 8 deletions src/modules/cpu_freq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
use crate::error::Error;
use crate::module::{Bar, RunPtr};
use crate::util::read_and_parse;
use crate::Pulse;
use crate::{Config as MainConfig, ModuleMsg};
use serde::{Deserialize, Serialize};
use std::fs::{read_dir, DirEntry};
use std::sync::mpsc::Sender;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::{Duration, Instant};
use std::{convert::TryFrom, path::Path};
Expand Down Expand Up @@ -179,12 +177,7 @@ impl<'a> Bar for CpuFreq<'a> {
}

#[instrument(skip_all)]
pub fn run(
key: char,
main_config: MainConfig,
_: Arc<Mutex<Pulse>>,
tx: Sender<ModuleMsg>,
) -> Result<(), Error> {
pub fn run(key: char, main_config: MainConfig, tx: Sender<ModuleMsg>) -> Result<(), Error> {
let config = InternalConfig::try_from(&main_config)?;
debug!("{:#?}", config);
let mut iteration_start: Instant;
Expand Down
9 changes: 1 addition & 8 deletions src/modules/date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

use crate::error::Error;
use crate::module::{Bar, RunPtr};
use crate::Pulse;
use crate::{Config as MainConfig, ModuleMsg};
use chrono::prelude::*;
use serde::{Deserialize, Serialize};
use std::sync::mpsc::Sender;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::{Duration, Instant};
use tracing::{debug, instrument};
Expand Down Expand Up @@ -101,12 +99,7 @@ impl<'a> Bar for DateTime<'a> {
}

#[instrument(skip_all)]
pub fn run(
key: char,
main_config: MainConfig,
_: Arc<Mutex<Pulse>>,
tx: Sender<ModuleMsg>,
) -> Result<(), Error> {
pub fn run(key: char, main_config: MainConfig, tx: Sender<ModuleMsg>) -> Result<(), Error> {
let config = InternalConfig::from(&main_config);
debug!("{:#?}", config);
let mut iteration_start: Instant;
Expand Down
9 changes: 1 addition & 8 deletions src/modules/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@

use crate::error::Error;
use crate::module::{Bar, RunPtr};
use crate::pulse::Pulse;
use crate::util::read_and_trim;
use crate::{Config as MainConfig, ModuleMsg};
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::sync::mpsc::Sender;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::{Duration, Instant};
use tracing::{debug, instrument};
Expand Down Expand Up @@ -152,12 +150,7 @@ impl MemRegex {
}

#[instrument(skip_all)]
pub fn run(
key: char,
main_config: MainConfig,
_: Arc<Mutex<Pulse>>,
tx: Sender<ModuleMsg>,
) -> Result<(), Error> {
pub fn run(key: char, main_config: MainConfig, tx: Sender<ModuleMsg>) -> Result<(), Error> {
let config = InternalConfig::from(&main_config);
debug!("{:#?}", config);
let mem_regex = MemRegex::new();
Expand Down
13 changes: 4 additions & 9 deletions src/modules/mic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

use crate::error::Error;
use crate::module::{Bar, RunPtr};
use crate::pulse::Pulse;
use crate::pulse::PULSE;
use crate::{Config as MainConfig, ModuleMsg};
use serde::{Deserialize, Serialize};
use std::sync::mpsc::Sender;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::{Duration, Instant};
use tracing::{debug, instrument};
use tracing::{debug, error, instrument};

const PLACEHOLDER: &str = "-";
const TICK_RATE: Duration = Duration::from_millis(50);
Expand Down Expand Up @@ -104,16 +103,12 @@ impl<'a> Bar for Mic<'a> {
}

#[instrument(skip_all)]
pub fn run(
key: char,
main_config: MainConfig,
pulse: Arc<Mutex<Pulse>>,
tx: Sender<ModuleMsg>,
) -> Result<(), Error> {
pub fn run(key: char, main_config: MainConfig, tx: Sender<ModuleMsg>) -> Result<(), Error> {
let config = InternalConfig::from(&main_config);
debug!("{:#?}", config);
let mut iteration_start: Instant;
let mut iteration_end: Duration;
let pulse = PULSE.get().ok_or("pulse module not initialized")?;
loop {
iteration_start = Instant::now();
if let Some(data) = pulse.lock().unwrap().source_data() {
Expand Down
4 changes: 4 additions & 0 deletions src/modules/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

pub mod battery;
pub mod brightness;
pub mod cpu;
Expand Down
Loading

0 comments on commit d2d6825

Please sign in to comment.