mirror of
https://github.com/typst/typst
synced 2025-05-14 04:56:26 +08:00
Open with (detached) custom viewers and raise error on failure (#4430)
This commit is contained in:
parent
82f13d9a38
commit
2df138a507
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user