Skip to main content

fresh/services/
process_hidden.rs

1//! Suppress the transient console window that Windows opens for
2//! console-subsystem children spawned from a GUI parent (git, ssh,
3//! docker, formatters, plugin processes, …). Setting `CREATE_NO_WINDOW`
4//! at spawn time prevents the brief black flash.
5//!
6//! No-op on non-Windows targets.
7
8#[cfg(windows)]
9const CREATE_NO_WINDOW: u32 = 0x0800_0000;
10
11pub trait HideWindow {
12    /// Mark the command so its child does not get a visible console
13    /// window on Windows. No-op elsewhere.
14    fn hide_window(&mut self) -> &mut Self;
15}
16
17impl HideWindow for std::process::Command {
18    #[cfg(windows)]
19    fn hide_window(&mut self) -> &mut Self {
20        use std::os::windows::process::CommandExt;
21        self.creation_flags(CREATE_NO_WINDOW)
22    }
23    #[cfg(not(windows))]
24    fn hide_window(&mut self) -> &mut Self {
25        self
26    }
27}
28
29impl HideWindow for tokio::process::Command {
30    #[cfg(windows)]
31    fn hide_window(&mut self) -> &mut Self {
32        self.creation_flags(CREATE_NO_WINDOW)
33    }
34    #[cfg(not(windows))]
35    fn hide_window(&mut self) -> &mut Self {
36        self
37    }
38}