Open with (detached) custom viewers and raise error on failure (#4430)

This commit is contained in:
Yip Coekjan 2024-07-06 17:54:12 +08:00 committed by GitHub
parent 82f13d9a38
commit 2df138a507
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 7 deletions

View File

@ -100,9 +100,11 @@ pub struct CompileCommand {
#[arg(long = "format", short = 'f')]
pub format: Option<OutputFormat>,
/// Opens the output file using the default viewer after compilation.
/// Ignored if output is stdout
#[arg(long = "open")]
/// Opens the output file with the default viewer or a specific program after
/// compilation
///
/// Ignored if output is stdout.
#[arg(long = "open", value_name = "VIEWER")]
pub open: Option<Option<String>>,
/// The PPI (pixels per inch) to use for PNG export

View File

@ -472,14 +472,30 @@ fn write_make_deps(world: &mut SystemWorld, command: &CompileCommand) -> StrResu
/// Opens the given file using:
/// - The default file viewer if `open` is `None`.
/// - The given viewer provided by `open` if it is `Some`.
///
/// If the file could not be opened, an error is returned.
fn open_file(open: Option<&str>, path: &Path) -> StrResult<()> {
// Some resource openers require the path to be canonicalized.
let path = path
.canonicalize()
.map_err(|err| eco_format!("failed to canonicalize path ({err})"))?;
if let Some(app) = open {
open::with_in_background(path, app);
open::with_detached(&path, app)
.map_err(|err| eco_format!("failed to open file with {} ({})", app, err))
} else {
open::that_in_background(path);
open::that_detached(&path).map_err(|err| {
let openers = open::commands(path)
.iter()
.map(|command| command.get_program().to_string_lossy())
.collect::<Vec<_>>()
.join(", ");
eco_format!(
"failed to open file with any of these resource openers: {} ({})",
openers,
err,
)
})
}
Ok(())
}
/// Adds useful hints when the main source file couldn't be read