From e8fcb84881d6d76ecff61310ba5ee5509b1b893d Mon Sep 17 00:00:00 2001 From: dave horner Date: Mon, 17 Nov 2025 18:54:47 -0500 Subject: [PATCH 1/2] feat(viewer): add .vm file support to seamlessly load and render both .vm and Rhai script files. - Update file watcher and script thread to pass both file contents and path. - Detect .vm files and parse them as flat text math trees using Context::from_text, wrapping the result in a ScriptContext. --- demos/viewer/src/script.rs | 27 ++++++++++++++++++++++----- demos/viewer/src/watcher.rs | 7 ++++--- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/demos/viewer/src/script.rs b/demos/viewer/src/script.rs index e2538a16..90ae7bc8 100644 --- a/demos/viewer/src/script.rs +++ b/demos/viewer/src/script.rs @@ -6,17 +6,34 @@ use std::sync::{Arc, Mutex}; /// Receives scripts and executes them with Fidget pub(crate) fn rhai_script_thread( - rx: Receiver, + rx: Receiver<(String, String)>, tx: Sender>, ) -> Result<()> { let mut engine = Engine::new(); loop { - let script = rx.recv()?; + let (script, path) = rx.recv()?; debug!("rhai script thread received script"); - let r = engine.run(&script).map_err(|e| e.to_string()); - debug!("rhai script thread is sending result to render thread"); - tx.send(r)?; + if path.ends_with(".vm") { + // Parse as flat text math tree + use fidget::context::Context; + use std::io::Cursor; + match Context::from_text(Cursor::new(&script)) { + Ok((ctx, node)) => { + let tree = ctx.export(node).unwrap(); + let mut sc = ScriptContext::new(); + sc.shapes.push(DrawShape { tree, color_rgb: [255, 255, 255] }); + tx.send(Ok(sc))?; + } + Err(e) => { + tx.send(Err(format!(".vm parse error: {e}")))?; + } + } + } else { + let r = engine.run(&script).map_err(|e| e.to_string()); + debug!("rhai script thread is sending result to render thread"); + tx.send(r)?; + } } } diff --git a/demos/viewer/src/watcher.rs b/demos/viewer/src/watcher.rs index 9371b6fc..0670ecc9 100644 --- a/demos/viewer/src/watcher.rs +++ b/demos/viewer/src/watcher.rs @@ -7,14 +7,15 @@ use std::path::Path; pub(crate) fn file_watcher_thread( path: &Path, rx: Receiver<()>, - tx: Sender, + tx: Sender<(String, String)>, ) -> Result<()> { let read_file = || -> Result { let out = String::from_utf8(std::fs::read(path)?).unwrap(); Ok(out) }; let mut contents = read_file()?; - tx.send(contents.clone())?; + let path_str = path.to_string_lossy().to_string(); + tx.send((contents.clone(), path_str.clone()))?; loop { // Wait for a file change notification @@ -31,7 +32,7 @@ pub(crate) fn file_watcher_thread( if contents != new_contents { contents = new_contents; debug!("file contents changed!"); - tx.send(contents.clone())?; + tx.send((contents.clone(), path_str.clone()))?; } } } From c8321bb78f43c3927412d12c25a9aca350fce642 Mon Sep 17 00:00:00 2001 From: dave horner Date: Mon, 17 Nov 2025 19:17:54 -0500 Subject: [PATCH 2/2] feat(env-fitler): allow set RUST_LOG=error to just print errors, silencing the underlying lib info/debug. --- demos/viewer/Cargo.toml | 1 + demos/viewer/src/main.rs | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/demos/viewer/Cargo.toml b/demos/viewer/Cargo.toml index 54070464..d3d4709f 100644 --- a/demos/viewer/Cargo.toml +++ b/demos/viewer/Cargo.toml @@ -18,6 +18,7 @@ zerocopy.workspace = true fidget.workspace = true workspace-hack.workspace = true +tracing-subscriber = { version = "0.3.20", features = ["env-filter"] } [features] default = ["jit"] diff --git a/demos/viewer/src/main.rs b/demos/viewer/src/main.rs index 51d09c7a..47f0ac0d 100644 --- a/demos/viewer/src/main.rs +++ b/demos/viewer/src/main.rs @@ -5,7 +5,6 @@ use eframe::{ egui, egui_wgpu::{self, wgpu}, }; -use env_logger::Env; use log::{debug, error, info}; use nalgebra::Point2; use notify::{Event, EventKind, Watcher}; @@ -223,7 +222,8 @@ fn render_3d( } fn main() -> Result<(), Box> { - env_logger::Builder::from_env(Env::default().default_filter_or("info")) + tracing_subscriber::fmt() + .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) .init(); let args = Args::parse();