Improve argument handling

This commit is contained in:
Jeremy Soller 2024-10-14 07:08:35 -06:00
parent 5d44d331dd
commit d8ee67cc7f
No known key found for this signature in database
GPG key ID: D02FD439211AF56F

View file

@ -23,6 +23,7 @@ use iced_video_player::{
use std::{ use std::{
any::TypeId, any::TypeId,
collections::HashMap, collections::HashMap,
fs,
time::{Duration, Instant}, time::{Duration, Instant},
}; };
@ -69,14 +70,30 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
settings = settings.theme(config.app_theme.theme()); settings = settings.theme(config.app_theme.theme());
settings = settings.size_limits(Limits::NONE.min_width(360.0).min_height(180.0)); settings = settings.size_limits(Limits::NONE.min_width(360.0).min_height(180.0));
let url = url::Url::from_file_path( let url_opt = match std::env::args().nth(1) {
std::env::args().nth(1).unwrap() Some(arg) => match url::Url::parse(&arg) {
) Ok(url) => Some(url),
.unwrap(); Err(_) => match fs::canonicalize(&arg) {
Ok(path) => match url::Url::from_file_path(&path) {
Ok(url) => Some(url),
Err(()) => {
log::warn!("failed to parse argument {:?}", arg);
None
}
},
Err(_) => {
log::warn!("failed to parse argument {:?}", arg);
None
}
},
},
None => None,
};
let flags = Flags { let flags = Flags {
config_handler, config_handler,
config, config,
url, url_opt,
}; };
cosmic::app::run::<App>(settings, flags)?; cosmic::app::run::<App>(settings, flags)?;
@ -106,7 +123,7 @@ impl Action {
pub struct Flags { pub struct Flags {
config_handler: Option<cosmic_config::Config>, config_handler: Option<cosmic_config::Config>,
config: Config, config: Config,
url: url::Url, url_opt: Option<url::Url>,
} }
/// Messages that are used specifically by our [`App`]. /// Messages that are used specifically by our [`App`].
@ -162,10 +179,15 @@ impl App {
fn load(&mut self) -> Command<Message> { fn load(&mut self) -> Command<Message> {
self.close(); self.close();
let video = match Video::new(&self.flags.url) { let url = match &self.flags.url_opt {
Some(some) => some,
None => return Command::none(),
};
let video = match Video::new(&url) {
Ok(ok) => ok, Ok(ok) => ok,
Err(err) => { Err(err) => {
log::warn!("failed to open {:?}: {err}", self.flags.url); log::warn!("failed to open {:?}: {err}", url);
return Command::none(); return Command::none();
} }
}; };