Skip to main content

mermaid_cli/utils/
open.rs

1use std::path::Path;
2use std::process::{Command, Stdio};
3
4/// Open a file with the platform's default application.
5/// Silently spawns the opener in the background (does not block).
6pub fn open_file(path: impl AsRef<Path>) {
7    let path = path.as_ref();
8
9    #[cfg(target_os = "linux")]
10    {
11        let _ = Command::new("xdg-open")
12            .arg(path)
13            .stdin(Stdio::null())
14            .stdout(Stdio::null())
15            .stderr(Stdio::null())
16            .spawn();
17    }
18
19    #[cfg(target_os = "macos")]
20    {
21        let _ = Command::new("open")
22            .arg(path)
23            .stdin(Stdio::null())
24            .stdout(Stdio::null())
25            .stderr(Stdio::null())
26            .spawn();
27    }
28
29    #[cfg(target_os = "windows")]
30    {
31        let _ = Command::new("cmd")
32            .args(["/C", "start", "", &path.display().to_string()])
33            .stdin(Stdio::null())
34            .stdout(Stdio::null())
35            .stderr(Stdio::null())
36            .spawn();
37    }
38}