surjection_core/process.rs
1//! Starting other programs from the app without a console window (#41).
2//!
3//! The app is a GUI process: on Windows every console program it starts
4//! (git, `op`, taskkill, ...) gets a console window of its own unless the
5//! spawn sets `CREATE_NO_WINDOW`. Every `Command` that core or the app
6//! starts goes through [`hide_window`]; a test
7//! (`tests/no_console_window.rs`) keeps it that way.
8//!
9//! Hiding is opt-in per process: the app calls [`hide_console_windows`]
10//! once at start-up. The CLI never does, so the git and `op` it starts
11//! keep its console: an ssh prompt reaches the user's terminal and Ctrl+C
12//! reaches git.
13
14use std::process::Command;
15use std::sync::atomic::{AtomicBool, Ordering};
16
17/// `CREATE_NO_WINDOW`: the child runs without a console window.
18pub const CREATE_NO_WINDOW: u32 = 0x0800_0000;
19
20static HIDE: AtomicBool = AtomicBool::new(false);
21
22/// From now on [`hide_window`] starts programs without a console window.
23/// For GUI processes (the app), called once before anything is started.
24pub fn hide_console_windows() {
25 HIDE.store(true, Ordering::Relaxed);
26}
27
28/// True once [`hide_console_windows`] was called in this process.
29pub fn hiding_console_windows() -> bool {
30 HIDE.load(Ordering::Relaxed)
31}
32
33/// Starts `command` without a console window on Windows once this process
34/// called [`hide_console_windows`]; otherwise (the CLI, other OSes) it
35/// does nothing and the child shares the parent's console.
36pub fn hide_window(command: &mut Command) -> &mut Command {
37 #[cfg(windows)]
38 if hiding_console_windows() {
39 use std::os::windows::process::CommandExt as _;
40 command.creation_flags(CREATE_NO_WINDOW);
41 }
42 command
43}