bunflared 0.1.2

Share localhost on a public link, with an absurdly fun terminal show and an agent mode
use std::io::Write;
use std::process::{Command, Stdio};

#[cfg(windows)]
const COPIERS: &[&[&str]] = &[&["clip"]];
#[cfg(not(windows))]
const COPIERS: &[&[&str]] = &[
    &["pbcopy"],
    &["wl-copy"],
    &["xclip", "-selection", "clipboard"],
];
#[cfg(windows)]
const OPENERS: &[&[&str]] = &[&["cmd", "/C", "start", ""]];
#[cfg(not(windows))]
const OPENERS: &[&[&str]] = &[&["open"], &["xdg-open"]];

pub fn copy(text: &str) -> bool {
    COPIERS.iter().any(|argv| {
        let Ok(mut child) = Command::new(argv[0])
            .args(&argv[1..])
            .stdin(Stdio::piped())
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .spawn()
        else {
            return false;
        };
        let written = child
            .stdin
            .take()
            .is_some_and(|mut stdin| stdin.write_all(text.as_bytes()).is_ok());
        written && child.wait().is_ok_and(|status| status.success())
    })
}

pub fn open(url: &str) -> bool {
    OPENERS.iter().any(|argv| {
        Command::new(argv[0])
            .args(&argv[1..])
            .arg(url)
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status()
            .is_ok_and(|status| status.success())
    })
}