byob-types 0.3.12

Shared serde types: messages, steps, attachments, settings (L0)
Documentation
//! Starting other programs without a window of their own.
//!
//! byob is a windowed program on Windows (no console). A console program it starts would get
//! a new console window, flashing up for every plugin, shell command or model CLI. So every
//! [`Command`] byob runs goes through [`NoWindow::no_window`]: on Windows it sets
//! `CREATE_NO_WINDOW` (the child gets a console with no window, which its own children share);
//! elsewhere it does nothing.

use std::process::Command;

/// `CREATE_NO_WINDOW` (`winbase.h`).
pub const CREATE_NO_WINDOW: u32 = 0x0800_0000;

pub trait NoWindow {
    /// Start without a console window (Windows); a no-op elsewhere.
    fn no_window(&mut self) -> &mut Self;
}

impl NoWindow for Command {
    #[cfg(windows)]
    fn no_window(&mut self) -> &mut Self {
        use std::os::windows::process::CommandExt as _;
        self.creation_flags(CREATE_NO_WINDOW)
    }

    #[cfg(not(windows))]
    fn no_window(&mut self) -> &mut Self {
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn no_window_keeps_the_command() {
        let mut c = Command::new("prog");
        c.arg("a").no_window().arg("b");
        assert_eq!(c.get_args().collect::<Vec<_>>(), ["a", "b"]);
    }
}