rightkit_child_process/no_window.rs
1//! Hide the console window of a child you build yourself (`std::process::Command`, and
2//! `tokio::process::Command` with the `tokio` feature), without going through
3//! [`OwnedCommand`](crate::OwnedCommand).
4
5/// The Windows `CREATE_NO_WINDOW` creation flag, exposed on every platform so a caller that
6/// composes its own flags can OR it in.
7pub const CREATE_NO_WINDOW: u32 = 0x0800_0000;
8
9/// The Windows `CREATE_BREAKAWAY_FROM_JOB` creation flag, exposed on every platform so a caller
10/// that composes its own flags (for example a deliberately detached replacement process) can OR
11/// it in. It asks the new process to leave the creator's job object; the launch fails with
12/// access denied when that job forbids breakaway.
13pub const CREATE_BREAKAWAY_FROM_JOB: u32 = 0x0100_0000;
14
15/// A `std::process::Command` for `program` that already has the no-window flag set on Windows,
16/// returned by value so it chains: `command_std("git").args(["status"]).output()`. Same effect as
17/// `Command::new(program)` followed by [`NoWindow::no_window`]. The async variant is
18/// `tokio_support::command` (feature `tokio`).
19pub fn command_std(program: impl AsRef<std::ffi::OsStr>) -> std::process::Command {
20 let mut command = std::process::Command::new(program);
21 command.no_window();
22 command
23}
24
25/// Suppresses the child's console window on Windows. A no-op elsewhere.
26///
27/// On Windows this calls `creation_flags(CREATE_NO_WINDOW)`, which *replaces* any creation
28/// flags set earlier on the same command (that is how `creation_flags` works for both std and
29/// tokio). Call it first and OR any further flags yourself with [`CREATE_NO_WINDOW`], or call
30/// `creation_flags(CREATE_NO_WINDOW | other)` directly.
31pub trait NoWindow {
32 /// Sets the no-window creation flag. Returns `self` for chaining.
33 fn no_window(&mut self) -> &mut Self;
34}
35
36impl NoWindow for std::process::Command {
37 fn no_window(&mut self) -> &mut Self {
38 #[cfg(windows)]
39 {
40 use std::os::windows::process::CommandExt;
41 self.creation_flags(CREATE_NO_WINDOW);
42 }
43 self
44 }
45}
46
47#[cfg(test)]
48mod tests {
49 use super::*;
50
51 #[test]
52 fn the_flag_value_is_the_windows_constant() {
53 assert_eq!(CREATE_NO_WINDOW, 0x0800_0000);
54 }
55
56 #[test]
57 fn breakaway_flag_is_the_windows_constant() {
58 assert_eq!(CREATE_BREAKAWAY_FROM_JOB, 0x0100_0000);
59 }
60
61 #[test]
62 fn command_std_returns_a_ready_command_by_value() {
63 let command = command_std(if cfg!(windows) { "cmd" } else { "true" });
64 assert_eq!(
65 command.get_program(),
66 if cfg!(windows) { "cmd" } else { "true" }
67 );
68 }
69
70 #[test]
71 fn a_std_command_chains_and_still_runs() {
72 let mut command = if cfg!(windows) {
73 let mut c = std::process::Command::new("cmd");
74 c.args(["/C", "exit 0"]);
75 c
76 } else {
77 std::process::Command::new("true")
78 };
79 let status = command.no_window().status().expect("run");
80 assert!(status.success());
81 }
82}