diff --git a/cosmic-panel-button/src/main.rs b/cosmic-panel-button/src/main.rs index 24016af0..0273a131 100644 --- a/cosmic-panel-button/src/main.rs +++ b/cosmic-panel-button/src/main.rs @@ -60,18 +60,60 @@ impl cosmic::Application for Button { } pub fn main() -> iced::Result { - let id = env::args() - .nth(1) - .expect("Requires desktop file id as argument."); - - let filename = format!("{id}.desktop"); let mut desktop = None; - for mut path in freedesktop_desktop_entry::default_paths() { - path.push(&filename); - if let Ok(bytes) = fs::read_to_string(&path) { - if let Ok(entry) = DesktopEntry::decode(&path, &bytes) { - desktop = - Some(Desktop { + if env::args().len() > 2 + || env::args() + .nth(1) + .expect("Requires a desktop id or --help for an argument.") + == "--help" + { + let args: Vec = env::args().collect(); + let mut exec = None; + let mut name = None; + let mut icon = None; + + for i in 1..args.len() { + match args[i].as_str() { + "-e" | "--exec" => { + exec = args.get(i + 1).map(|s| s.to_owned()); + } + "-n" | "--name" => { + name = args.get(i + 1).map(|s| s.to_owned()); + } + "-i" | "--icon" => { + icon = args.get(i + 1).map(|s| s.to_owned()); + } + "-h" | "--help" => { + println!("cosmic-panel-button is a cosmic applet which creates a button to either run .desktop files or execute a command. \n"); + println!("--exec and --name are required arguments. \n"); + println!("-e, --exec Command to execute"); + println!("-n, --name Name of the applet"); + println!("-i, --icon Name of the icon for the applet \n"); + println!("Example line .desktop file:"); + println!("Exec=sh -c \"cosmic-panel-button --exec 'notify-send cosmic-panel-button_pressed' --name 'send-notification' --icon bell\""); + std::process::exit(1) + } + _ => {} + } + } + + desktop = Some(Desktop { + name: name.unwrap_or_else(|| panic!("Name is a required argument")), + icon: icon, + exec: exec.unwrap_or_else(|| panic!("Exec is a required argument")), + }); + } else { + let arg = env::args() + .nth(1) + .expect("Requires a desktop id or --help for an argument."); + + let filename = format!("{arg}.desktop"); + + for mut path in freedesktop_desktop_entry::default_paths() { + path.push(&filename); + if let Ok(bytes) = fs::read_to_string(&path) { + if let Ok(entry) = DesktopEntry::decode(&path, &bytes) { + desktop = Some(Desktop { name: entry.name(None).map(|x| x.to_string()).unwrap_or_else(|| { panic!("Desktop file '{filename}' doesn't have `Name`") }), @@ -80,12 +122,13 @@ pub fn main() -> iced::Result { panic!("Desktop file '{filename}' doesn't have `Exec`") }), }); - break; + break; + } } } + desktop = Some(desktop.unwrap_or_else(|| { + panic!("Failed to find valid desktop file '{filename}' in search paths") + })); } - let desktop = desktop.unwrap_or_else(|| { - panic!("Failed to find valid desktop file '{filename}' in search paths") - }); - cosmic::applet::run::