Skip to main content

uxn_tal/
util.rs

1use std::process::Command;
2use uxn_tal_common::cache::RomEntryResolver;
3
4#[cfg(windows)]
5use std::os::windows::process::CommandExt;
6
7/// Create a git command with console window hidden on Windows
8pub fn create_git_command() -> Command {
9    #[cfg(windows)]
10    let mut cmd = Command::new("git");
11    #[cfg(not(windows))]
12    let cmd = Command::new("git");
13    #[cfg(windows)]
14    {
15        // Hide console window on Windows (CREATE_NO_WINDOW = 0x08000000)
16        cmd.creation_flags(0x08000000);
17    }
18    cmd
19}
20
21/// Real implementation of RomEntryResolver for use in uxn-tal and integration tests.
22pub struct RealRomEntryResolver;
23impl RomEntryResolver for RealRomEntryResolver {
24    fn resolve_entry_and_cache_dir(
25        &self,
26        url: &str,
27    ) -> Result<(std::path::PathBuf, std::path::PathBuf), String> {
28        crate::fetch::downloader::resolve_and_fetch_entry(url).map_err(|e| format!("{e}"))
29    }
30}
31use crate::assemble_file;
32// src/util.rs
33use std::io::IsTerminal;
34
35// Helper: pause for 15 seconds on error
36pub fn pause_on_error() {
37    if !std::io::stderr().is_terminal() && !std::io::stdout().is_terminal() {
38        // no console attached — don't pause
39        return;
40    }
41    use std::{thread, time};
42    eprintln!("\n---\nKeeping window open for 15 seconds so you can read the above. Press Enter to continue...");
43    use std::sync::mpsc;
44
45    let (tx, rx) = mpsc::channel();
46    #[cfg(not(target_arch = "wasm32"))]
47    thread::spawn(move || {
48        let mut _buf = String::new();
49        let _ = std::io::stdin().read_line(&mut _buf);
50        let _ = tx.send(());
51    });
52    let _ = rx.recv_timeout(time::Duration::from_secs(15));
53}
54// Helper: pause for Windows console
55pub fn pause_for_windows() {
56    #[cfg(target_os = "windows")]
57    {
58        if std::io::stdout().is_terminal() || std::io::stderr().is_terminal() {
59            use std::io::Write;
60
61            print!("Press Enter to continue...");
62            let _ = std::io::stdout().flush();
63            let mut _buf = String::new();
64            let _ = std::io::stdin().read_line(&mut _buf);
65        }
66    }
67}
68
69use std::path::{Path, PathBuf};
70/// Real implementation of get_or_write_cached_rom for RomCache trait
71pub struct RealRomCache;
72impl uxn_tal_common::cache::RomCache for RealRomCache {
73    fn get_or_write_cached_rom(&self, url: &str, out_path: &Path) -> Result<PathBuf, String> {
74        // Try to resolve and fetch the entry (tal or orca file) and get the cache dir
75        let (entry_path, cache_dir) = crate::fetch::downloader::resolve_and_fetch_entry(url)
76            .map_err(|e| format!("resolve_and_fetch_entry failed: {e}"))?;
77        let rom_path = cache_dir.join(
78            out_path
79                .file_name()
80                .unwrap_or_else(|| std::ffi::OsStr::new("out.rom")),
81        );
82        // If ROM already exists, return it
83        if rom_path.exists() {
84            return Ok(rom_path);
85        }
86        // If entry is a .rom, just copy it
87        if let Some(ext) = entry_path.extension() {
88            if ext == "rom" {
89                std::fs::copy(&entry_path, &rom_path)
90                    .map_err(|e| format!("Failed to copy ROM: {e}"))?;
91                return Ok(rom_path);
92            }
93        }
94        // Otherwise, assemble .tal to .rom
95        let tal_path = entry_path;
96        let rom_bytes = assemble_file(&tal_path).map_err(|e| format!("Assembler error: {e}"))?;
97        std::fs::write(&rom_path, &rom_bytes).map_err(|e| format!("Failed to write ROM: {e}"))?;
98        Ok(rom_path)
99    }
100}