use std::path::PathBuf;
use std::time::Duration;
use tunes::live_coding::{runner::LiveRunner, watcher::FileWatcher};
fn main() -> anyhow::Result<()> {
let args: Vec<String> = std::env::args().collect();
if args.len() < 2 {
eprintln!("Usage: {} <path-to-rust-file>", args[0]);
eprintln!();
eprintln!("Example:");
eprintln!(" {} live_session.rs", args[0]);
std::process::exit(1);
}
let source_file = PathBuf::from(&args[1]);
if !source_file.exists() {
eprintln!("Error: File '{}' not found", source_file.display());
std::process::exit(1);
}
println!("🎵 Tunes Live Coding Mode");
println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
println!("📁 Watching: {}", source_file.display());
println!("💡 Edit and save to hear your changes!");
println!(" Press Ctrl+C to exit");
println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
let watcher = FileWatcher::new(&source_file)?;
let mut runner = LiveRunner::new(source_file.clone())?;
if let Err(e) = runner.compile_and_run() {
eprintln!("Initial compilation failed: {}", e);
println!("\n💡 Fix the errors and save to try again...\n");
}
loop {
if watcher.wait_for_change(Duration::from_millis(500)) {
println!("\n📝 File changed detected!");
std::thread::sleep(Duration::from_millis(500));
if let Err(e) = runner.compile_and_run() {
eprintln!("Compilation failed: {}", e);
println!("\n💡 Fix the errors and save to try again...\n");
}
}
if !runner.is_running() {
eprintln!("\n⚠️ Process stopped unexpectedly");
println!("💡 Waiting for file changes...\n");
while !watcher.wait_for_change(Duration::from_secs(1)) {
}
println!("\n📝 File changed - recompiling...");
if let Err(e) = runner.compile_and_run() {
eprintln!("Compilation failed: {}", e);
}
}
}
}