Improvements to the display procedures
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
use std::ffi::{CString, NulError};
|
||||
use std::{collections::HashMap, ffi::{CString, NulError}};
|
||||
|
||||
use x11::xlib;
|
||||
|
||||
use crate::{config::Config, plugman::PolledText};
|
||||
|
||||
|
||||
|
||||
pub struct Display {
|
||||
@@ -41,4 +43,63 @@ impl Display {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn write_with_vec(&mut self, cfg: &Config, v: HashMap<String, PolledText>) -> Result<(), NulError> {
|
||||
let mut buf = Vec::new();
|
||||
|
||||
let mut v = if cfg.plugins.as_whitelist {
|
||||
let mut vv = HashMap::new();
|
||||
for (n, pt) in v {
|
||||
if cfg.plugins.blacklist.contains(&n) {
|
||||
vv.insert(n, pt);
|
||||
}
|
||||
}
|
||||
vv
|
||||
} else {
|
||||
let mut vv = HashMap::new();
|
||||
for (n, pt) in v {
|
||||
if !cfg.plugins.blacklist.contains(&n) {
|
||||
vv.insert(n, pt);
|
||||
}
|
||||
}
|
||||
vv
|
||||
};
|
||||
|
||||
let mut used_values = Vec::new();
|
||||
for tv in &cfg.plugins.template {
|
||||
let mut found = false;
|
||||
for (pname, pt) in &v {
|
||||
if *pname == *tv {
|
||||
if !pt.disabled() {
|
||||
buf.push(pt.text().clone());
|
||||
}
|
||||
|
||||
used_values.push(pname.clone());
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
log::warn!("Plugin '{}' was not found even though it was included in the template", tv);
|
||||
}
|
||||
}
|
||||
|
||||
for val in used_values {
|
||||
v.remove(&val);
|
||||
}
|
||||
|
||||
let mut kv: Vec<(&String, &PolledText)> = v.iter().collect();
|
||||
kv.sort_by(|a, b| a.0.cmp(b.0));
|
||||
|
||||
for (_, pt) in &kv {
|
||||
buf.push(pt.text().clone());
|
||||
}
|
||||
|
||||
let text = buf.join(&cfg.seperator);
|
||||
|
||||
self.write_display_name(&text)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
36
src/main.rs
36
src/main.rs
@@ -1,19 +1,33 @@
|
||||
#![allow(internal_features)]
|
||||
#![feature(core_intrinsics)]
|
||||
use std::{path::PathBuf, process::ExitCode, time::Duration};
|
||||
|
||||
use std::{process::ExitCode, time::Duration};
|
||||
|
||||
use clap::Parser;
|
||||
|
||||
mod display;
|
||||
mod plugman;
|
||||
mod config;
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
struct CliArgs {
|
||||
#[arg(long, short, default_value="./plugins")]
|
||||
plugin_dir: camino::Utf8PathBuf,
|
||||
pub plugin_dir: camino::Utf8PathBuf,
|
||||
#[arg(long, short, default_value="./config")]
|
||||
pub config_dir: camino::Utf8PathBuf,
|
||||
#[arg(long, short)]
|
||||
pub debug: bool
|
||||
}
|
||||
|
||||
|
||||
// TODO: Clean up main function
|
||||
// TODO: Make ffi safe abstraction for logger
|
||||
// TODO: Set up ipc with unix sockets
|
||||
// TODO: Allow sending messages command -> running DIM instance -> plugin with ipc
|
||||
// TODO: Run code through clippy
|
||||
fn main() -> ExitCode {
|
||||
// let ca = CliArgs::parse();
|
||||
let ca = CliArgs::parse();
|
||||
|
||||
if ca.debug {
|
||||
env_logger::builder()
|
||||
.filter(None, log::LevelFilter::Debug)
|
||||
@@ -22,6 +36,8 @@ fn main() -> ExitCode {
|
||||
env_logger::builder()
|
||||
.filter(None, log::LevelFilter::Info)
|
||||
.init();
|
||||
}
|
||||
|
||||
let config;
|
||||
match config::Config::new(ca.config_dir.as_std_path()) {
|
||||
Ok(v) => config = v,
|
||||
@@ -36,22 +52,18 @@ fn main() -> ExitCode {
|
||||
|
||||
|
||||
let mut pm = plugman::PlugMan::new(1024); // idk tbh
|
||||
if let Err(e) = pm.load(PathBuf::from("./plugins")
|
||||
//ca.plugin_dir.as_std_path().to_path_buf()
|
||||
){
|
||||
if let Err(e) = pm.load(ca.plugin_dir.as_std_path().to_path_buf()){
|
||||
log::error!("Failed to load plugins: {e:?}");
|
||||
return ExitCode::from(3);
|
||||
}
|
||||
|
||||
pm.init_plugins();
|
||||
|
||||
// TODO: Sort them from a config or something
|
||||
|
||||
loop {
|
||||
let vals: Vec<String> = pm.poll_plugins().into_iter().map(|f| f.1).collect();
|
||||
|
||||
|
||||
let _ = disp.write_display_name(&vals.join(" | "));
|
||||
let vals = pm.poll_plugins();
|
||||
if let Err(e) = disp.write_with_vec(&config, vals) {
|
||||
log::warn!("Failed to write too bar: {e}");
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(250));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user