1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//! Windows-only: attach to the parent process's console on startup and
//! rebind stdio so `println!` / `tracing` reaches the terminal that
//! launched us.
//!
//! Why: release builds use `windows_subsystem = "windows"` so explorer
//! / shortcut / file-association launches don't flash a transient
//! console window. A windows-subsystem exe starts with no attached
//! console at all, so terminal launches need us to:
//!
//! 1. `AttachConsole(ATTACH_PARENT_PROCESS)` — grab the launching
//! terminal's console if it has one.
//! 2. For each standard handle that's currently **empty** (null),
//! re-open `CONOUT$` / `CONIN$` and wire it in with `SetStdHandle`.
//!
//! The "currently empty" check is load-bearing. Shell redirection
//! (`todoke --version > out.txt`, `todoke | clip`) leaves a real
//! redirected handle in place even before we attach. Clobbering it
//! would break piping and redirection — so we only touch handles that
//! would otherwise be null.
//!
//! When launched from explorer `AttachConsole` returns 0 and this
//! whole function is a no-op (stdio stays null — exactly what we want).
//!
//! **Call ordering is load-bearing.** Rust's `io::stdout()` /
//! `io::stderr()` / `io::stdin()` cache the handle on first use. If
//! anything writes to stdio *before* `attach_parent_console()` runs,
//! the null handle gets cached and the `SetStdHandle` calls here
//! become no-ops from Rust's perspective. `main()` invokes this as
//! the first statement; don't introduce earlier stdio usage (panic
//! hooks, static initializers, etc.) without re-thinking this.
use OpenOptions;
use IntoRawHandle;
use ;
use ;
/// Re-open the console device and install it as `which` — but ONLY if
/// `which` is currently null. A non-null existing handle means the
/// caller redirected it (e.g. `todoke > out.txt`), and we must leave
/// that alone.