diff --git a/windows/src-tauri/src/cli.rs b/windows/src-tauri/src/cli.rs index 314a4421..c5cf2684 100644 --- a/windows/src-tauri/src/cli.rs +++ b/windows/src-tauri/src/cli.rs @@ -329,6 +329,21 @@ impl CodeburnCli { } } + /// `codeburn export -f -o `, the mac's `runExport`. The path is built by + /// `commands::export_usage` rather than supplied by the reader, so it is not put through + /// the argument allowlist, which would reject a drive letter and a backslash. Nothing is + /// interpolated through a shell: this is argv, as every other spawn here is. + pub async fn export_to(&self, format: &str, output: &std::path::Path) -> Result<()> { + if !matches!(format, "csv" | "json") { + bail!("unknown export format"); + } + let path = output.to_string_lossy().to_string(); + let warm = PAYLOAD_SEEN.load(std::sync::atomic::Ordering::Relaxed); + self.run_capture(&["export", "-f", format, "-o", &path], silence_window(warm)) + .await?; + Ok(()) + } + async fn run_capture(&self, args: &[&str], silence_secs: u64) -> Result { self.run_capture_with_env(args, silence_secs, &[]).await } @@ -764,6 +779,38 @@ fn expand_env(value: &str) -> String { result } +/// Opens the folder holding `path` with `path` itself selected: the Windows counterpart of +/// the mac's `NSWorkspace.activateFileViewerSelecting`, which is what the export reveal has +/// always done there. `explorer.exe` is spawned out of `%SystemRoot%` for the same reason +/// every other system tool here is, and its exit code is ignored because it reports 1 on a +/// perfectly successful open. +#[cfg(windows)] +pub fn reveal_in_file_manager(path: &std::path::Path) -> Result<()> { + let root = env::var_os("SystemRoot") + .map(PathBuf::from) + .filter(|p| p.is_absolute()) + .unwrap_or_else(|| PathBuf::from(r"C:\Windows")); + // One argument, comma-joined: `/select,C:\path`. Passing them as two would open the + // reader's Documents folder instead, which is explorer's answer to an unparsed switch. + let mut arg = std::ffi::OsString::from("/select,"); + arg.push(path.as_os_str()); + std::process::Command::new(root.join("explorer.exe")) + .arg(arg) + .spawn() + .with_context(|| "could not open Explorer")?; + Ok(()) +} + +#[cfg(not(windows))] +pub fn reveal_in_file_manager(path: &std::path::Path) -> Result<()> { + let target = path.parent().unwrap_or(path); + std::process::Command::new("xdg-open") + .arg(target) + .spawn() + .with_context(|| "could not open the file manager")?; + Ok(()) +} + /// Runs a codeburn subcommand in the user's terminal emulator so they can see the output. /// Linux: tries `x-terminal-emulator`, `gnome-terminal`, `konsole`, then falls back to a /// detached headless spawn. Windows: opens a console via `cmd /C start`. Never diff --git a/windows/src-tauri/src/lib.rs b/windows/src-tauri/src/lib.rs index df6826a1..fc45716a 100644 --- a/windows/src-tauri/src/lib.rs +++ b/windows/src-tauri/src/lib.rs @@ -153,6 +153,7 @@ pub fn run() { commands::set_currency, commands::open_terminal_command, commands::open_claude_login, + commands::export_usage, commands::quit_app, commands::hide_popover, commands::set_tray_tooltip, @@ -845,6 +846,41 @@ mod commands { crate::cli::spawn_claude_login(&app).map_err(|e| e.to_string()) } + /// Export straight into the Downloads folder and reveal what came out, the mac's + /// `runExport`: `~/Downloads/codeburn-`, a folder of CSVs or one JSON file, then + /// the file manager opens with it selected. No console window and nothing left in the + /// working directory, which is what the old "export through a terminal" did. + /// + /// The stamp is formatted by the page because a local `yyyy-MM-dd-HHmmss` in Rust would + /// mean either a new dependency or a hand-rolled timezone conversion. It never reaches a + /// shell, but it does become a directory name, so it is checked here rather than trusted: + /// digits and hyphens only, and the length the format produces. + #[tauri::command] + pub async fn export_usage(format: String, stamp: String) -> Result { + if !matches!(format.as_str(), "csv" | "json") { + return Err("unknown export format".into()); + } + if stamp.len() != 17 || !stamp.bytes().all(|b| b.is_ascii_digit() || b == b'-') { + return Err("invalid export timestamp".into()); + } + let downloads = dirs::download_dir() + .or_else(|| dirs::home_dir().map(|home| home.join("Downloads"))) + .ok_or_else(|| "could not find your Downloads folder".to_string())?; + // CSV writes a folder of one-table-per-file CSVs; JSON writes a single file, which + // wants the extension the mac gives it. + let name = match format.as_str() { + "json" => format!("codeburn-{stamp}.json"), + _ => format!("codeburn-{stamp}"), + }; + let output = downloads.join(name); + let cli = crate::cli::CodeburnCli::resolve(); + cli.export_to(&format, &output) + .await + .map_err(|e| e.to_string())?; + crate::cli::reveal_in_file_manager(&output).map_err(|e| e.to_string())?; + Ok(output.to_string_lossy().to_string()) + } + #[tauri::command] pub fn quit_app(app: AppHandle) { app.exit(0); diff --git a/windows/src/App.tsx b/windows/src/App.tsx index 3d614441..fa405625 100644 --- a/windows/src/App.tsx +++ b/windows/src/App.tsx @@ -482,6 +482,18 @@ export function App() { const openTerminal = (args: string[]) => { invoke('open_terminal_command', { args }).catch(err => setError(String(err))) } + + /// The mac's runExport: into the Downloads folder as codeburn-, then revealed in + /// the file manager with the result selected. The stamp is the mac's local-time + /// `yyyy-MM-dd-HHmmss`, which is why it is formatted here rather than in Rust. + const runExport = (format: 'csv' | 'json') => { + const now = new Date() + const two = (n: number) => String(n).padStart(2, '0') + const stamp = `${now.getFullYear()}-${two(now.getMonth() + 1)}-${two(now.getDate())}` + + `-${two(now.getHours())}${two(now.getMinutes())}${two(now.getSeconds())}` + invoke('export_usage', { format, stamp }).catch(err => setError(String(err))) + } + const connectClaude = () => { invoke('open_claude_login').catch(err => setError(String(err))) } @@ -661,7 +673,7 @@ export function App() { onCurrency={applyCurrency} loading={overlay} onRefresh={userRefresh} - onExport={format => openTerminal(['export', '-f', format])} + onExport={runExport} onOpenReport={() => openTerminal(['report'])} onToggleTheme={cycleTheme} onQuit={() => invoke('quit_app').catch(() => {})}