implement watch mode

This commit is contained in:
2021-11-14 15:18:21 +01:00
parent bce7d82dae
commit 289382f351
3 changed files with 44 additions and 6 deletions

7
Cargo.lock generated
View File

@@ -959,6 +959,12 @@ version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099"
[[package]]
name = "pico-args"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db8bcd96cb740d03149cbad5518db9fd87126a10ab519c011893b1754134c468"
[[package]] [[package]]
name = "pkg-config" name = "pkg-config"
version = "0.3.22" version = "0.3.22"
@@ -1389,6 +1395,7 @@ dependencies = [
"anyhow", "anyhow",
"minifb", "minifb",
"notify", "notify",
"pico-args",
"wasmtime", "wasmtime",
] ]

View File

@@ -9,4 +9,5 @@ edition = "2021"
wasmtime = "0.30" wasmtime = "0.30"
anyhow = "1" anyhow = "1"
minifb = "0.19" minifb = "0.19"
notify = "4" notify = "4"
pico-args = "0.4"

View File

@@ -1,18 +1,48 @@
use anyhow::{anyhow, Result}; use std::path::PathBuf;
use std::sync::mpsc;
use std::time::Duration;
use anyhow::{bail, Result};
use microw8::MicroW8; use microw8::MicroW8;
use notify::{DebouncedEvent, Watcher};
use pico_args::Arguments;
mod microw8; mod microw8;
fn main() -> Result<()> { fn main() -> Result<()> {
let filename = std::env::args() let mut args = Arguments::from_env();
.nth(1)
.ok_or_else(|| anyhow!("Missing .uw8 file path"))?; let watch_mode = args.contains(["-w", "--watch"]);
let filename = args.free_from_os_str::<PathBuf, bool>(|s| Ok(s.into()))?;
let mut uw8 = MicroW8::new()?; let mut uw8 = MicroW8::new()?;
uw8.load_from_file(filename)?; let (tx, rx) = mpsc::channel();
let mut watcher = notify::watcher(tx, Duration::from_millis(100))?;
if watch_mode {
watcher.watch(&filename, notify::RecursiveMode::NonRecursive)?;
}
if let Err(err) = uw8.load_from_file(&filename) {
if !watch_mode {
return Err(err);
}
eprintln!("Load error: {}", err);
}
while uw8.is_open() { while uw8.is_open() {
match rx.try_recv() {
Ok(DebouncedEvent::Create(_) | DebouncedEvent::Write(_)) => {
if let Err(err) = uw8.load_from_file(&filename) {
eprintln!("Load error: {}", err)
}
}
Err(mpsc::TryRecvError::Disconnected) => bail!("File watcher disconnected"),
_ => (),
}
uw8.run_frame()?; uw8.run_frame()?;
} }