From e88b16c5356bb95f828bf9d9df8c30c0617466a5 Mon Sep 17 00:00:00 2001 From: Nicola Guerrera Date: Tue, 28 Feb 2023 12:17:15 +0100 Subject: [PATCH] Desktop file parsing improvements --- Cargo.lock | 12 ++++++++++++ Cargo.toml | 3 ++- src/protostar.rs | 14 +++++++++----- src/xdg.rs | 14 +++++++++++++- 4 files changed, 36 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d5a9a42..7d11602 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -40,6 +40,15 @@ dependencies = [ "version_check", ] +[[package]] +name = "aho-corasick" +version = "0.7.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac" +dependencies = [ + "memchr", +] + [[package]] name = "aliasable" version = "0.1.3" @@ -1537,6 +1546,7 @@ dependencies = [ "manifest-dir-macros", "mint", "nix 0.26.1", + "regex", "resvg", "rustc-hash", "stardust-xr-fusion", @@ -1679,6 +1689,8 @@ version = "1.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "48aaa5748ba571fb95cd2c85c09f629215d3a6ece942baa100950af03a34f733" dependencies = [ + "aho-corasick", + "memchr", "regex-syntax", ] diff --git a/Cargo.toml b/Cargo.toml index 4abe98c..a1cd0f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ linicon = "2.3.0" manifest-dir-macros = "0.1.16" mint = "0.5.9" nix = "0.26.1" +regex = "1.7.1" resvg = "0.29.0" rustc-hash = "1.1.0" stardust-xr-fusion = "0.38.1" @@ -28,4 +29,4 @@ ustr = "0.9.0" walkdir = "2.3.2" [dev-dependencies] -tempdir = "0.3.7" \ No newline at end of file +tempdir = "0.3.7" diff --git a/src/protostar.rs b/src/protostar.rs index d4a5f3c..7d6fed8 100644 --- a/src/protostar.rs +++ b/src/protostar.rs @@ -3,6 +3,7 @@ use color_eyre::eyre::{eyre, Result}; use glam::Quat; use mint::Vector3; use nix::unistd::setsid; +use regex::Regex; use stardust_xr_fusion::{ client::{Client, FrameInfo, RootHandler}, core::values::Transform, @@ -174,7 +175,7 @@ impl RootHandler for ProtoStar { let scale = grabbabe_move.move_by(info.delta); self.grabbable .content_parent() - .set_position(None, [self.position.x*scale, self.position.y*scale, self.position.z*scale]) + .set_position(Some(self.client.get_root()), [self.position.x*scale, self.position.y*scale, self.position.z*scale]) .unwrap(); } else { if grabbabe_move.final_value() == 0.0001 { @@ -237,9 +238,9 @@ impl RootHandler for ProtoStar { .get_position_rotation_scale(self.client.get_root()) .unwrap(); - let executable = dbg!(self.execute_command.clone()); + let executable = self.execute_command.clone(); - //TODO: split the executable string for the args + //TODO: split the executable string for the args tokio::task::spawn(async move { let distance_vector = distance_future.await.ok().unwrap().0; let distance = @@ -248,9 +249,12 @@ impl RootHandler for ProtoStar { let future = startup_settings.generate_startup_token().unwrap(); std::env::set_var("STARDUST_STARTUP_TOKEN", future.await.unwrap()); - + let re = Regex::new(r"%[fFuUdDnNickvm]").unwrap(); + let exec = re.replace_all(&executable, ""); + let mut executable_array : Vec<&str> = dbg!(exec.split_whitespace().collect()); unsafe { - Command::new(executable) + Command::new(executable_array.remove(0)) + .args(executable_array) .stdin(Stdio::null()) .stdout(Stdio::null()) .stderr(Stdio::null()) diff --git a/src/xdg.rs b/src/xdg.rs index f9bd6af..f047082 100644 --- a/src/xdg.rs +++ b/src/xdg.rs @@ -12,7 +12,7 @@ use std::path::{Path, PathBuf}; use std::str::FromStr; use std::{env, fs}; use walkdir::WalkDir; - +use regex::Regex; fn get_data_dirs() -> Vec { let xdg_data_dirs_str = std::env::var("XDG_DATA_DIRS").unwrap_or_default(); @@ -88,9 +88,13 @@ pub fn parse_desktop_file(path: PathBuf) -> Result { let mut categories = Vec::new(); let mut icon = None; let mut no_display = false; + let mut desktop_entry_found = false; + + let re = Regex::new(r"^\[([^\]]*)\]$").unwrap(); // Loop through each line of the file for line in reader.lines() { + let line = match line { Ok(line) => line, Err(err) => return Err(format!("Failed to read line: {}", err)), @@ -101,6 +105,14 @@ pub fn parse_desktop_file(path: PathBuf) -> Result { continue; } + if let Some(captures) = re.captures(&line){ + let entry = captures.get(1).unwrap(); + desktop_entry_found = entry.as_str().contains("Desktop Entry"); + } + + if !desktop_entry_found { + continue + } // Split the line into a key-value pair by looking for the first "=" character let parts = line.split_once('='); let (key, value) = match parts {