diff --git a/Cargo.lock b/Cargo.lock index 4508742f..9d337a8d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4039,6 +4039,7 @@ dependencies = [ "time", "tokio", "tokio-rustls", + "windows-sys 0.61.2", "zeroize", "zip", ] diff --git a/crates/ledger-cli/Cargo.toml b/crates/ledger-cli/Cargo.toml index a99d2b3c..7d8e19cd 100644 --- a/crates/ledger-cli/Cargo.toml +++ b/crates/ledger-cli/Cargo.toml @@ -24,6 +24,11 @@ ledger-mcp = { path = "../ledger-mcp" } ledger-store = { path = "../ledger-store" } ledger-gui = { path = "../ledger-gui", optional = true } +# The parent's console for a command run in a terminal (console.rs): the +# raw bindings, already in the lockfile through ledger-store. +[target.'cfg(windows)'.dependencies] +windows-sys = { version = "0.61", features = ["Win32_Foundation", "Win32_System_Console"] } + [dev-dependencies] base64 = "0.22" chrono = "0.4" diff --git a/crates/ledger-cli/src/console.rs b/crates/ledger-cli/src/console.rs new file mode 100644 index 00000000..f7ee1d9d --- /dev/null +++ b/crates/ledger-cli/src/console.rs @@ -0,0 +1,85 @@ +//! The program's console on Windows (plan P15 unit C1, WB-pkg-2). +//! +//! The installed program is one executable that is the window, the books +//! server child and the assistants' bridge. Built for the Windows GUI +//! subsystem (`windows_subsystem = "windows"` in `main.rs`), a double-click +//! or a Start Menu entry opens no console window. The same file still has +//! to be a command line: a person who types `wanderbooks books list` in a +//! terminal needs the words printed there, and an assistant that starts +//! `wanderbooks mcp --book X` hands it pipes, which a GUI-subsystem +//! program receives as they are. +//! +//! ```text +//! GOAL: no console window when the program starts as an app; its words +//! reach the terminal that started it as a command; a pipe handed to +//! it is never replaced. +//! SOURCE: WB-pkg-2 (one executable); Microsoft's documentation of +//! AttachConsole(ATTACH_PARENT_PROCESS) and SetStdHandle. +//! INVARIANTS: +//! 1. A standard handle the parent gave (a pipe, a file) is left as it +//! is; only a missing one (null or invalid) is pointed at the +//! parent's console (CONIN$ for input, CONOUT$ for output and +//! errors). +//! 2. With no parent console (a Start Menu launch, an assistant that +//! gave pipes and has no console) nothing is attached and nothing is +//! opened: the program never makes a console window of its own. +//! SCENARIOS: +//! 1. IF an assistant starts `mcp` with three pipes: SHOULD speak over +//! them, unchanged. +//! 2. IF a terminal starts `books list`: SHOULD print into the terminal. +//! 3. IF the Start Menu starts it with no words: SHOULD open the window +//! and no console. +//! ``` +//! +//! Not run on Windows by the author (the unit was written on Linux); the +//! `release` workflow's Windows job builds it, and the first install is +//! the evidence for the scenarios. + +/// Point the missing standard handles at the parent's console, if there +/// is one (INVARIANTS 1 and 2). A no-op everywhere but Windows. +#[cfg(windows)] +pub fn attach_parent_console() { + use std::os::windows::io::IntoRawHandle; + use windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE; + use windows_sys::Win32::System::Console::{ + ATTACH_PARENT_PROCESS, AttachConsole, GetStdHandle, STD_ERROR_HANDLE, STD_INPUT_HANDLE, + STD_OUTPUT_HANDLE, SetStdHandle, + }; + + // SAFETY: plain Win32 calls with no pointers of ours; a failure is a + // value, and the handles opened below are given to the process, which + // keeps them open for its life. + unsafe { + let missing = |id| { + let handle = GetStdHandle(id); + handle.is_null() || handle == INVALID_HANDLE_VALUE + }; + let wanted = [ + (STD_INPUT_HANDLE, "CONIN$", true), + (STD_OUTPUT_HANDLE, "CONOUT$", false), + (STD_ERROR_HANDLE, "CONOUT$", false), + ]; + if !wanted.iter().any(|(id, _, _)| missing(*id)) { + return; + } + if AttachConsole(ATTACH_PARENT_PROCESS) == 0 { + return; + } + for (id, device, input) in wanted { + if !missing(id) { + continue; + } + let opened = std::fs::OpenOptions::new() + .read(input) + .write(!input) + .open(device); + if let Ok(file) = opened { + SetStdHandle(id, file.into_raw_handle()); + } + } + } +} + +/// Nothing to do where a program is a console program already. +#[cfg(not(windows))] +pub fn attach_parent_console() {} diff --git a/crates/ledger-cli/src/main.rs b/crates/ledger-cli/src/main.rs index 69e68251..406c17f6 100644 --- a/crates/ledger-cli/src/main.rs +++ b/crates/ledger-cli/src/main.rs @@ -9,6 +9,13 @@ //! From nothing to the first post: `wanderbooks setup`, `wanderbooks books //! create ID`, `wanderbooks connect claude --book ID` (then talk to the //! agent). +//! +//! On Windows the executable is built for the GUI subsystem, so starting +//! it from the Start Menu opens no console window; `console` gives a +//! command run in a terminal its words back (WB-pkg-2). +#![cfg_attr(all(windows, feature = "gui"), windows_subsystem = "windows")] + +mod console; use std::env; use std::io::Write; @@ -700,6 +707,11 @@ fn main() { eprintln!("error: an argument isn't UTF-8 text"); std::process::exit(2); }; + // A command run in a terminal prints there; the app (no words) opens + // no console at all (console.rs). + if !words.is_empty() { + console::attach_parent_console(); + } // A bare launch is the viewer. if is_bare_launch(&words, std::io::IsTerminal::is_terminal(&std::io::stdin())) { std::process::exit(run_gui(Vec::new())); diff --git a/crates/ledger-gui/src/app.rs b/crates/ledger-gui/src/app.rs index 211c5fb5..f2e16b50 100644 --- a/crates/ledger-gui/src/app.rs +++ b/crates/ledger-gui/src/app.rs @@ -214,7 +214,7 @@ const WARN_AGE: std::time::Duration = std::time::Duration::from_secs(60); const FIRST_CONNECT_KEY: &str = "first_connect_done"; /// What a window is called when no company is in front. -const TITLE: &str = "wanderbooks"; +const TITLE: &str = "Wanderbooks"; impl App { /// The live app over one company's books directory. diff --git a/crates/ledger-gui/src/export.rs b/crates/ledger-gui/src/export.rs index 167225d7..c5739b14 100644 --- a/crates/ledger-gui/src/export.rs +++ b/crates/ledger-gui/src/export.rs @@ -141,10 +141,17 @@ pub fn open(path: &Path) { #[cfg(target_os = "macos")] let cmd = std::process::Command::new("open").arg(path).spawn(); #[cfg(target_os = "windows")] - let cmd = std::process::Command::new("cmd") - .args(["/C", "start", ""]) - .arg(path) - .spawn(); + let cmd = { + // The window has no console (WB-pkg-2): a console program it + // starts would open one for a moment, so it is told not to + // (CREATE_NO_WINDOW). + use std::os::windows::process::CommandExt; + std::process::Command::new("cmd") + .args(["/C", "start", ""]) + .arg(path) + .creation_flags(0x0800_0000) + .spawn() + }; #[cfg(not(any(target_os = "macos", target_os = "windows")))] let cmd = std::process::Command::new("xdg-open").arg(path).spawn(); let _ = cmd; diff --git a/crates/ledger-gui/src/lib.rs b/crates/ledger-gui/src/lib.rs index eb4a4f9d..d02612d2 100644 --- a/crates/ledger-gui/src/lib.rs +++ b/crates/ledger-gui/src/lib.rs @@ -140,7 +140,7 @@ pub fn run( }; let options = eframe::NativeOptions { viewport: egui::ViewportBuilder::default() - .with_title("wanderbooks") + .with_title("Wanderbooks") .with_app_id("wanderbooks") .with_inner_size([1440.0, 920.0]) // Small on purpose: tiling window managers hand out small slots diff --git a/crates/ledger-gui/tests/tabs.rs b/crates/ledger-gui/tests/tabs.rs index d2e6f193..9da8baef 100644 --- a/crates/ledger-gui/tests/tabs.rs +++ b/crates/ledger-gui/tests/tabs.rs @@ -336,7 +336,7 @@ fn each_tab_shows_its_own_company_and_figures() { loaded(&mut h); assert_eq!(titles(&h), ["Alpha Co", "Beta Co"]); assert_eq!(h.state().active(), Some(0)); - assert_eq!(h.state().window_title(), "Alpha Co · wanderbooks"); + assert_eq!(h.state().window_title(), "Alpha Co · Wanderbooks"); // Alpha: cash 10,000.00 less 1,234.56 rent is 8,765.44. assert!(shows(&h, "8,765.44"), "alpha's cash: {:?}", texts(&h)); @@ -354,7 +354,7 @@ fn each_tab_shows_its_own_company_and_figures() { // Beta: cash 25,000.00 less 777.77 is 24,222.23. show_tab(&mut h, "Beta Co"); assert_eq!(h.state().active(), Some(1)); - assert_eq!(h.state().window_title(), "Beta Co · wanderbooks"); + assert_eq!(h.state().window_title(), "Beta Co · Wanderbooks"); assert!( matches!(h.state().route(), Route::Overview), "a tab opens on its overview, not on the page the other was on" @@ -768,7 +768,7 @@ fn start_up_restores_the_open_tabs_their_order_and_the_one_in_front() { assert_eq!(titles(&h), ["Gamma Co", "Alpha Co", "Beta Co"]); assert_eq!(h.state().active(), Some(1), "the same one in front"); assert!(h.state().views().iter().all(|v| v.error().is_none())); - assert_eq!(h.state().window_title(), "Alpha Co · wanderbooks"); + assert_eq!(h.state().window_title(), "Alpha Co · Wanderbooks"); // No record: the data root's first, alone. let h2 = start_with_prefs(&w, &[]); @@ -894,15 +894,15 @@ fn closing_the_front_tab_shows_a_neighbour_and_the_last_leaves_an_empty_window() show_tab(&mut h, "Beta Co"); close_tab(&mut h, "Beta Co"); assert_eq!(titles(&h), ["Alpha Co", "Gamma Co"]); - assert_eq!(h.state().window_title(), "Gamma Co · wanderbooks"); + assert_eq!(h.state().window_title(), "Gamma Co · Wanderbooks"); // A tab behind the front one closing leaves the front one in front. close_tab(&mut h, "Alpha Co"); assert_eq!(titles(&h), ["Gamma Co"]); - assert_eq!(h.state().window_title(), "Gamma Co · wanderbooks"); + assert_eq!(h.state().window_title(), "Gamma Co · Wanderbooks"); // The last one. close_tab(&mut h, "Gamma Co"); assert!(h.state().views().is_empty()); - assert_eq!(h.state().window_title(), "wanderbooks"); + assert_eq!(h.state().window_title(), "Wanderbooks"); assert!(shows(&h, "No company is open"), "{:?}", texts(&h)); assert!( shows(&h, "Open a company with the + at the top of the window."), @@ -1737,7 +1737,7 @@ fn the_status_card_shows_the_company_in_front_and_follows_the_tab() { "alpha's hue left the card" ); // The window's title still names the company. - assert_eq!(h.state().window_title(), "Beta Co · wanderbooks"); + assert_eq!(h.state().window_title(), "Beta Co · Wanderbooks"); } } diff --git a/crates/ledger-host/src/commands.rs b/crates/ledger-host/src/commands.rs index 4ebe37c4..6564ef85 100644 --- a/crates/ledger-host/src/commands.rs +++ b/crates/ledger-host/src/commands.rs @@ -1967,11 +1967,20 @@ pub fn connect_agent(common: &Common, agent: &str, book: &str) -> Result| -> Result { - let mut child = std::process::Command::new(&program) + let mut command = std::process::Command::new(&program); + command .args(args) .stdin(std::process::Stdio::null()) .stdout(std::process::Stdio::null()) - .stderr(std::process::Stdio::piped()) + .stderr(std::process::Stdio::piped()); + // The window has no console (WB-pkg-2): this one is not to open + // a console of its own for a moment (CREATE_NO_WINDOW). + #[cfg(windows)] + { + use std::os::windows::process::CommandExt; + command.creation_flags(0x0800_0000); + } + let mut child = command .spawn() .map_err(|e| format!("{label} couldn't be run: {}", e.kind()))?; let deadline = std::time::Instant::now() + CONNECT_WAIT;