Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
wiiznokes committed Sep 18, 2024
1 parent 01a5449 commit 340a1c2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 50 deletions.
27 changes: 16 additions & 11 deletions bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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/"),
Expand All @@ -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();
}
}
}
37 changes: 13 additions & 24 deletions plugins/src/cosmic_toplevel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
}
Expand All @@ -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();
Expand All @@ -64,7 +62,7 @@ pub async fn main() {
_ => (),
},
Err(why) => {
tracing::error!("malformed JSON request: {}", why);
error!("malformed JSON request: {}", why);
}
};
}
Expand All @@ -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));
}
Expand All @@ -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) => {
Expand All @@ -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));
}
}
Expand All @@ -115,7 +113,7 @@ struct App<W> {
locales: Vec<String>,
desktop_entries: Vec<DesktopEntry<'static>>,
ids_to_ignore: Vec<u32>,
// 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<ToplevelAction>,
tx: W,
Expand Down Expand Up @@ -149,7 +147,7 @@ impl<W: AsyncWrite + Unpin> App<W> {
}

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;
}
Expand All @@ -160,7 +158,7 @@ impl<W: AsyncWrite + Unpin> App<W> {
None
}
}) {
tracing::info!("activating: {id}");
info!("activating: {id}");
let _res = self.calloop_tx.send(ToplevelAction::Activate(handle));
}
}
Expand Down Expand Up @@ -240,12 +238,3 @@ impl<W: AsyncWrite + Unpin> App<W> {
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
}
20 changes: 5 additions & 15 deletions plugins/src/desktop_entries/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -53,13 +51,9 @@ struct App<W> {

impl<W: AsyncWrite + Unpin> App<W> {
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,
Expand All @@ -82,9 +76,7 @@ impl<W: AsyncWrite + Unpin> App<W> {
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() {
Expand Down Expand Up @@ -130,10 +122,8 @@ impl<W: AsyncWrite + Unpin> App<W> {
return None;
}
}
} else {
if de.no_display() {
return None;
}
} else if de.no_display() {
return None;
}
deduplicator.insert(de.appid.to_string());
Some(de)
Expand Down
10 changes: 10 additions & 0 deletions plugins/src/desktop_entries/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 340a1c2

Please sign in to comment.