-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Open torrents with
imdl create --open ...
Invokes an OS-dependent opener to open the `.torrent` file after creation. type: added
- Loading branch information
Showing
7 changed files
with
167 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use crate::common::*; | ||
|
||
pub(crate) struct Platform; | ||
|
||
#[cfg(target_os = "windows")] | ||
impl PlatformInterface for Platform { | ||
fn opener() -> Result<Vec<OsString>, Error> { | ||
let exe = if cfg!(test) { "open.bat" } else { "cmd" }; | ||
Ok(vec![ | ||
OsString::from(exe), | ||
OsString::from("/C"), | ||
OsString::from("start"), | ||
]) | ||
} | ||
} | ||
|
||
#[cfg(target_os = "macos")] | ||
impl PlatformInterface for Platform { | ||
fn opener() -> Result<Vec<OsString>, Error> { | ||
Ok(vec![OsString::from("open")]) | ||
} | ||
} | ||
|
||
#[cfg(not(any(target_os = "windows", target_os = "macos")))] | ||
impl PlatformInterface for Platform { | ||
fn opener() -> Result<Vec<OsString>, Error> { | ||
const OPENERS: &[&str] = &["xdg-open", "gnome-open", "kde-open"]; | ||
|
||
for opener in OPENERS { | ||
if let Ok(output) = Command::new(opener).arg("--version").output() { | ||
if output.status.success() { | ||
return Ok(vec![OsString::from(opener)]); | ||
} | ||
} | ||
} | ||
|
||
Err(Error::OpenerMissing { tried: OPENERS }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use crate::common::*; | ||
|
||
pub(crate) trait PlatformInterface { | ||
fn open(path: &Path) -> Result<(), Error> { | ||
let mut command = Self::opener()?; | ||
command.push(OsString::from(path)); | ||
|
||
let command_string = || { | ||
command | ||
.iter() | ||
.map(|arg| arg.to_string_lossy().into_owned()) | ||
.collect::<Vec<String>>() | ||
.join(",") | ||
}; | ||
|
||
let status = Command::new(&command[0]) | ||
.args(&command[1..]) | ||
.status() | ||
.map_err(|source| Error::CommandInvoke { | ||
source, | ||
command: command_string(), | ||
})?; | ||
|
||
if status.success() { | ||
Ok(()) | ||
} else { | ||
Err(Error::CommandStatus { | ||
command: command_string(), | ||
status, | ||
}) | ||
} | ||
} | ||
|
||
fn opener() -> Result<Vec<OsString>, Error>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters