Skip to main content

very_normal/
lib.rs

1pub mod app;
2pub mod charts;
3pub mod coach;
4pub mod corpus;
5pub mod drill;
6pub mod gate;
7pub mod keymap;
8pub mod layout;
9pub mod optimise;
10pub mod oryx;
11pub mod plans;
12pub mod report;
13pub mod store;
14pub mod warmup;
15
16/// Opens a URL with `$VERY_NORMAL_OPEN` (a command, `{url}` marks where the
17/// address goes), or with `open` (`xdg-open` on Linux).
18pub fn open_url(url: &str) -> anyhow::Result<()> {
19    let default = if cfg!(target_os = "linux") {
20        "xdg-open {url}"
21    } else {
22        "open {url}"
23    };
24    let template = std::env::var("VERY_NORMAL_OPEN").unwrap_or_else(|_| default.to_string());
25    let mut parts: Vec<String> = template.split_whitespace().map(str::to_string).collect();
26    if !parts.iter().any(|p| p.contains("{url}")) {
27        parts.push("{url}".into());
28    }
29    let parts: Vec<String> = parts.into_iter().map(|p| p.replace("{url}", url)).collect();
30    let (cmd, args) = parts
31        .split_first()
32        .ok_or_else(|| anyhow::anyhow!("VERY_NORMAL_OPEN is empty"))?;
33    std::process::Command::new(cmd)
34        .args(args)
35        .stdout(std::process::Stdio::null())
36        .stderr(std::process::Stdio::null())
37        .spawn()?;
38    Ok(())
39}