mirror of
https://github.com/typst/typst
synced 2025-05-14 17:15:28 +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')]
|
#[arg(long = "format", short = 'f')]
|
||||||
pub format: Option<OutputFormat>,
|
pub format: Option<OutputFormat>,
|
||||||
|
|
||||||
/// Opens the output file using the default viewer after compilation.
|
/// Opens the output file with the default viewer or a specific program after
|
||||||
/// Ignored if output is stdout
|
/// compilation
|
||||||
#[arg(long = "open")]
|
///
|
||||||
|
/// Ignored if output is stdout.
|
||||||
|
#[arg(long = "open", value_name = "VIEWER")]
|
||||||
pub open: Option<Option<String>>,
|
pub open: Option<Option<String>>,
|
||||||
|
|
||||||
/// The PPI (pixels per inch) to use for PNG export
|
/// 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:
|
/// Opens the given file using:
|
||||||
/// - The default file viewer if `open` is `None`.
|
/// - The default file viewer if `open` is `None`.
|
||||||
/// - The given viewer provided by `open` if it is `Some`.
|
/// - 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<()> {
|
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 {
|
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 {
|
} 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
|
/// Adds useful hints when the main source file couldn't be read
|
||||||
|
Loading…
x
Reference in New Issue
Block a user