summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoraxtlos <axtlos@disroot.org>2024-04-19 23:10:16 +0200
committeraxtlos <axtlos@disroot.org>2024-04-19 23:10:16 +0200
commit73815e97a91cd51db9d3b96d69891f6f6c1f58d0 (patch)
tree7e7e8c96770c856d38d55dfaf4601fee73028db2 /src
downloaddeskwhich-73815e97a91cd51db9d3b96d69891f6f6c1f58d0.tar.gz
deskwhich-73815e97a91cd51db9d3b96d69891f6f6c1f58d0.tar.bz2
Add program
Diffstat (limited to 'src')
-rw-r--r--src/args.rs27
-rw-r--r--src/main.rs51
2 files changed, 78 insertions, 0 deletions
diff --git a/src/args.rs b/src/args.rs
new file mode 100644
index 0000000..d5efe2e
--- /dev/null
+++ b/src/args.rs
@@ -0,0 +1,27 @@
+use clap::Parser;
+
+#[derive(Debug, Parser)]
+#[clap(name="deskwhich", version=env!("CARGO_PKG_VERSION"),about=env!("CARGO_PKG_DESCRIPTION"), author=env!("CARGO_PKG_AUTHORS"))]
+pub struct Cli {
+
+ /// The desktop file to search for
+ pub search: String,
+
+ /// Skip directories in XDG_DATA_DIRS that start with a dot
+ #[arg(long, default_value_t=false)]
+ pub skip_dot: bool,
+
+ /// Skip directories in XDG_DATA_DIRS that start with the home directory
+ #[arg(long, default_value_t=false)]
+ pub skip_home: bool,
+
+ /// Print all matches in XDG_DATA_DIRS, not just the first
+ #[arg(short, long, default_value_t=false)]
+ pub all: bool,
+
+ //show_dot: bool,
+
+ /// Output a tilde for HOME directory
+ #[arg(long, default_value_t=false)]
+ pub show_tilde: bool,
+}
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..18ee8e9
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,51 @@
+mod args;
+use std::fs;
+use std::env;
+use std::process::exit;
+use clap::Parser;
+use crate::args::Cli;
+
+fn main() {
+ let cli = Cli::parse();
+
+ let args: Vec<String> = env::args().collect();
+ let xdg_data_dirs = std::env::var("XDG_DATA_DIRS").unwrap();
+ let data_dirs = xdg_data_dirs.split(":");
+
+ let mut found = false;
+ let home = match env::var("HOME") {
+ Ok(var) => var,
+ Err(_) => exit(1),
+ };
+ for dirs in data_dirs {
+ if (dirs.starts_with(&home) && cli.skip_home) || (dirs.starts_with(".") && cli.skip_dot) {
+ continue
+ }
+
+ let files = match fs::read_dir(format!("{}/applications",dirs)) {
+ Ok(file) => file,
+ Err(_) => continue,
+ };
+ for file in files {
+ let desktop_file = format!("{}", file.as_ref().unwrap().file_name().into_string().unwrap());
+ if desktop_file.contains(&cli.search) {
+ let path = file.as_ref().unwrap().path();
+ let out = if cli.show_tilde {
+ format!("{}", path.display().to_string().replace(&home, "~"))
+ } else {
+ path.display().to_string()
+ };
+ println!("{}", out);
+ found = true;
+ if !cli.all {
+ return
+ }
+ }
+ }
+ }
+
+ if !found {
+ eprintln!("No {} in ({})", args[1], xdg_data_dirs);
+ exit(1);
+ }
+}