From 340a1c27d63ccd80ad30ca3aee9a532e653fe168 Mon Sep 17 00:00:00 2001 From: wiiznokes <78230769+wiiznokes@users.noreply.github.com> Date: Sat, 13 Jul 2024 12:47:46 +0200 Subject: [PATCH] fmt --- bin/src/main.rs | 27 +++++++++++--------- plugins/src/cosmic_toplevel/mod.rs | 37 ++++++++++------------------ plugins/src/desktop_entries/mod.rs | 20 ++++----------- plugins/src/desktop_entries/utils.rs | 10 ++++++++ 4 files changed, 44 insertions(+), 50 deletions(-) diff --git a/bin/src/main.rs b/bin/src/main.rs index c7e9ce5..51926f3 100644 --- a/bin/src/main.rs +++ b/bin/src/main.rs @@ -5,6 +5,7 @@ use pop_launcher_toolkit::plugins; use pop_launcher_toolkit::service; use mimalloc::MiMalloc; +use tracing::info; #[global_allocator] static GLOBAL: MiMalloc = MiMalloc; @@ -17,6 +18,8 @@ async fn main() { init_logging(cmd); + info!("starting {}", cmd); + match cmd { "calc" => plugins::calc::main().await, "desktop-entries" => plugins::desktop_entries::main().await, @@ -38,7 +41,8 @@ async fn main() { } fn init_logging(cmd: &str) { - use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter}; + use tracing_subscriber::prelude::*; + use tracing_subscriber::{fmt, EnvFilter}; let logdir = match dirs::state_dir() { Some(dir) => dir.join("pop-launcher/"), @@ -61,24 +65,25 @@ fn init_logging(cmd: &str) { } } + let fmt_layer = tracing_subscriber::fmt::layer() + .with_target(false) + .with_timer(fmt::time::ChronoLocal::new("%T".into())) + .with_writer(file); + let filter_layer = EnvFilter::try_from_default_env() .or_else(|_| EnvFilter::try_new("warn")) .unwrap(); - let fmt_layer = fmt::layer().with_target(false).with_writer(file); + let registry = tracing_subscriber::registry() + .with(fmt_layer) + .with(filter_layer); // would be nice to implement this tracing issue // for journald https://github.com/tokio-rs/tracing/issues/2348 - if let Ok(journal_layer) = tracing_journald::layer() { - tracing_subscriber::registry() - .with(journal_layer) - .with(filter_layer) - .init(); + if let Ok(journald_layer) = tracing_journald::layer() { + registry.with(journald_layer).init(); } else { - tracing_subscriber::registry() - .with(fmt_layer) - .with(filter_layer) - .init(); + registry.init(); } } } diff --git a/plugins/src/cosmic_toplevel/mod.rs b/plugins/src/cosmic_toplevel/mod.rs index 2d41f34..84bc421 100644 --- a/plugins/src/cosmic_toplevel/mod.rs +++ b/plugins/src/cosmic_toplevel/mod.rs @@ -6,9 +6,9 @@ use cctk::{cosmic_protocols, sctk::reexports::calloop, toplevel_info::ToplevelIn use cosmic_protocols::toplevel_info::v1::client::zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1; use fde::DesktopEntry; use freedesktop_desktop_entry as fde; -use tracing::{debug, info}; +use tracing::{debug, error, info, warn}; -use crate::desktop_entries::utils::get_description; +use crate::desktop_entries::utils::{get_description, is_session_cosmic}; use crate::send; use futures::{ channel::mpsc, @@ -27,11 +27,9 @@ use tokio::io::{AsyncWrite, AsyncWriteExt}; use self::toplevel_handler::{toplevel_handler, ToplevelAction, ToplevelEvent}; pub async fn main() { - info!("starting cosmic-toplevel"); - let mut tx = async_stdout(); - if !session_is_cosmic() { + if !is_session_cosmic() { send(&mut tx, PluginResponse::Deactivate).await; return; } @@ -50,12 +48,12 @@ pub async fn main() { match request { Ok(request) => match request { Request::Activate(id) => { - tracing::info!("activating {id}"); + debug!("activating {id}"); app.activate(id); } Request::Quit(id) => app.quit(id), Request::Search(query) => { - tracing::info!("searching {query}"); + debug!("searching {query}"); app.search(&query).await; // clear the ids to ignore, as all just sent are valid app.ids_to_ignore.clear(); @@ -64,7 +62,7 @@ pub async fn main() { _ => (), }, Err(why) => { - tracing::error!("malformed JSON request: {}", why); + error!("malformed JSON request: {}", why); } }; } @@ -74,7 +72,7 @@ pub async fn main() { match event { ToplevelEvent::Add(handle, info) => { - tracing::info!("add {}", &info.app_id); + debug!("add {}", &info.app_id); app.toplevels.retain(|t| t.0 != handle); app.toplevels.push_front((handle, info)); } @@ -84,7 +82,7 @@ pub async fn main() { // ignore requests for this id until after the next search app.ids_to_ignore.push(handle.id().protocol_id()); } else { - tracing::warn!("ToplevelEvent::Remove, no toplevel found"); + warn!("ToplevelEvent::Remove, no toplevel found"); } } ToplevelEvent::Update(handle, info) => { @@ -93,14 +91,14 @@ pub async fn main() { if let Some(pos) = app.toplevels.iter().position(|t| t.0 == handle) { if info.state.contains(&State::Activated) { - tracing::debug!("Update {:?}: push front", &info.app_id); + debug!("Update {:?}: push front", &info.app_id); app.toplevels.remove(pos); app.toplevels.push_front((handle, info)); } else { app.toplevels[pos].1 = info; } } else { - tracing::warn!("ToplevelEvent::Update, no toplevel found"); + warn!("ToplevelEvent::Update, no toplevel found"); app.toplevels.push_front((handle, info)); } } @@ -115,7 +113,7 @@ struct App { locales: Vec, desktop_entries: Vec>, ids_to_ignore: Vec, - // XXX: use LinkedList? + // XXX: use LinkedList, and Box the tuple because it will be re ordered a lot? toplevels: VecDeque<(ZcosmicToplevelHandleV1, ToplevelInfo)>, calloop_tx: calloop::channel::Sender, tx: W, @@ -149,7 +147,7 @@ impl App { } fn activate(&mut self, id: u32) { - tracing::info!("requested to activate: {id}"); + info!("requested to activate: {id}"); if self.ids_to_ignore.contains(&id) { return; } @@ -160,7 +158,7 @@ impl App { None } }) { - tracing::info!("activating: {id}"); + info!("activating: {id}"); let _res = self.calloop_tx.send(ToplevelAction::Activate(handle)); } } @@ -240,12 +238,3 @@ impl App { let _ = self.tx.flush().await; } } - -#[must_use] -fn session_is_cosmic() -> bool { - if let Ok(var) = std::env::var("XDG_CURRENT_DESKTOP") { - return var.contains("COSMIC"); - } - - false -} diff --git a/plugins/src/desktop_entries/mod.rs b/plugins/src/desktop_entries/mod.rs index 8f06f6c..0215c4e 100644 --- a/plugins/src/desktop_entries/mod.rs +++ b/plugins/src/desktop_entries/mod.rs @@ -9,13 +9,11 @@ use futures::StreamExt; use pop_launcher::*; use std::borrow::Cow; use tokio::io::AsyncWrite; -use tracing::info; -use utils::get_description; +use utils::{get_description, is_session_cosmic}; pub(crate) mod utils; pub async fn main() { - info!("starting desktop entries"); let mut app = App::new(async_stdout()); app.reload().await; @@ -53,13 +51,9 @@ struct App { impl App { fn new(tx: W) -> Self { - let current_desktop = fde::current_desktop(); Self { current_desktop: fde::current_desktop(), - is_desktop_cosmic: current_desktop - .unwrap_or_default() - .iter() - .any(|e| e == "cosmic"), + is_desktop_cosmic: is_session_cosmic(), desktop_entries: Vec::new(), locales: fde::get_languages_from_env(), tx, @@ -82,9 +76,7 @@ impl App { return None; } - if de.name(&self.locales).is_none() { - return None; - } + de.name(&self.locales)?; match de.exec() { Some(exec) => match exec.split_ascii_whitespace().next() { @@ -130,10 +122,8 @@ impl App { return None; } } - } else { - if de.no_display() { - return None; - } + } else if de.no_display() { + return None; } deduplicator.insert(de.appid.to_string()); Some(de) diff --git a/plugins/src/desktop_entries/utils.rs b/plugins/src/desktop_entries/utils.rs index ba9a252..864aced 100644 --- a/plugins/src/desktop_entries/utils.rs +++ b/plugins/src/desktop_entries/utils.rs @@ -36,3 +36,13 @@ pub fn get_description<'a>(de: &'a DesktopEntry<'a>, locales: &[String]) -> Stri None => desc_source, } } + +// todo: cache +#[must_use] +pub fn is_session_cosmic() -> bool { + if let Ok(var) = std::env::var("XDG_CURRENT_DESKTOP") { + return var.contains("COSMIC"); + } + + false +}