feat(windows): export into Downloads and reveal what came out

Export opened a console, ran the CLI in it and left the result in
whatever directory the app happened to be started from, which on this
platform is next to the executable. The mac has always written into
~/Downloads as codeburn-<timestamp> and then revealed the result in
Finder with it selected, so this does the same: no console, a folder of
CSVs or one JSON file with the mac's yyyy-MM-dd-HHmmss stamp, and
Explorer opening on it through /select, which is the Windows counterpart
of activateFileViewerSelecting.

The Downloads folder comes from the known-folder API rather than
%USERPROFILE%\Downloads, since it can be moved. The stamp is formatted in
the page because a local-time format in Rust would cost a dependency or a
hand-rolled timezone; it never reaches a shell, but it does become a
directory name, so Rust checks it is digits and hyphens of the right
length rather than trusting it.
This commit is contained in:
iamtoruk 2026-09-02 00:17:50 -07:00
parent f021513479
commit c9502bc7ff
3 changed files with 96 additions and 1 deletions

View file

@ -329,6 +329,21 @@ impl CodeburnCli {
}
}
/// `codeburn export -f <format> -o <path>`, 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<String> {
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

View file

@ -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-<stamp>`, 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<String, String> {
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);

View file

@ -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-<stamp>, 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(() => {})}