From c0717f46867c50c8d6589ee6cdd9ebaba49dbde6 Mon Sep 17 00:00:00 2001 From: jacksonfu14 Date: Mon, 17 Nov 2025 09:32:08 +0100 Subject: feature: add render to cli --- morphology-cli/src/main.rs | 115 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 95 insertions(+), 20 deletions(-) (limited to 'morphology-cli/src/main.rs') diff --git a/morphology-cli/src/main.rs b/morphology-cli/src/main.rs index acfaad3..402225b 100644 --- a/morphology-cli/src/main.rs +++ b/morphology-cli/src/main.rs @@ -6,9 +6,11 @@ //! generate 5 //! generate --pretty 5 //! generate 5 --neg-first-outer --backward +//! generate 5 --render --output-dir output //! //! This will list all valid meta-affine sequences of length 5 and print the total count. //! With --pretty, values are displayed in nested bracket notation. +//! With --render, sequences are rendered to SVG files using LilyPond. use morphology::sequence::{ generate_all_sequences, SequenceOptions, PrecedenceOrder, SortingMethod, @@ -29,10 +31,40 @@ fn main() { let backward = args.contains(&"--backward".to_string()); let interpolate_length = args.contains(&"--interpolate-length".to_string()); + // Rendering flags + let render = args.contains(&"--render".to_string()); + let keep_temp = args.contains(&"--keep-temp".to_string()); + let parallel = args.contains(&"--parallel".to_string()); + + // Extract --output-dir value + let output_dir = args.iter() + .position(|arg| arg == "--output-dir") + .and_then(|pos| args.get(pos + 1)) + .map(|s| s.as_str()) + .unwrap_or("output"); + + // Extract --max-batch-size value + let max_batch_size: Option = args.iter() + .position(|arg| arg == "--max-batch-size") + .and_then(|pos| args.get(pos + 1)) + .and_then(|s| s.parse().ok()); + // Filter out flags to get positional arguments + // Also filter out values that come after flags with parameters + let mut skip_next = false; let positional: Vec<&String> = args.iter() .skip(1) // Skip program name - .filter(|arg| !arg.starts_with("--")) + .filter(|arg| { + if skip_next { + skip_next = false; + return false; + } + if *arg == "--output-dir" || *arg == "--max-batch-size" { + skip_next = true; + return false; + } + !arg.starts_with("--") + }) .collect(); if positional.len() != 1 { @@ -46,11 +78,19 @@ fn main() { eprintln!(" --breadth-first Use BreadthFirst sorting method"); eprintln!(" --backward Sort sequences backward (last to first)"); eprintln!(" --interpolate-length Interpolate sequences of different lengths"); + eprintln!("\nRendering options:"); + eprintln!(" --render Render sequences to SVG files using LilyPond"); + eprintln!(" --output-dir Output directory for SVG files (default: output)"); + eprintln!(" --keep-temp Keep temporary .ly files for debugging"); + eprintln!(" --parallel Enable parallel rendering for better performance"); + eprintln!(" --max-batch-size Maximum sequences per batch (avoids overwhelming filesystem)"); eprintln!("\nExamples:"); eprintln!(" {} 5", args[0]); eprintln!(" {} --pretty 5", args[0]); eprintln!(" {} 5 --neg-first-outer --backward", args[0]); eprintln!(" {} 5 --breadth-first --interpolate-length", args[0]); + eprintln!(" {} 5 --render --output-dir output", args[0]); + eprintln!(" {} 5 --render --parallel --max-batch-size 50", args[0]); process::exit(1); } @@ -97,27 +137,62 @@ fn main() { // Generate all sequences with specified options let sequences = generate_all_sequences(length, Some(options)); - // Choose delimiters based on output format - let (open, close) = if pretty { ("", "") } else { ("[", "]") }; - - // Print each sequence - for (i, sequence) in sequences.iter().enumerate() { - print!("{}. {}", i + 1, open); - for (j, value) in sequence.values().iter().enumerate() { - if pretty { - print!("{}", value.pretty_print()); - } else { - print!("{}", value); + if render { + // Render sequences to SVG files + println!("Rendering {} sequences to SVG files...", sequences.len()); + + // Build rendering configuration + let mut config = morphology_img::RenderConfig::new(output_dir) + .keep_temp_files(keep_temp) + .parallel(parallel); + + if let Some(batch_size) = max_batch_size { + config = config.max_batch_size(batch_size); + } + + // Render sequences + match morphology_img::render_sequences(&sequences, &config) { + Ok(svg_paths) => { + println!("\nSuccessfully generated {} SVG files in '{}'", svg_paths.len(), output_dir); + println!("\nSVG files:"); + for (i, path) in svg_paths.iter().enumerate() { + println!(" {}. {}", i + 1, path.display()); + } + println!("\n{}", "=".repeat(70)); + println!("Total sequences rendered: {}", sequences.len()); + println!("{}", "=".repeat(70)); } - if j < sequence.values().len() - 1 { - print!(", "); + Err(e) => { + eprintln!("\nError rendering sequences: {}", e); + eprintln!("\nMake sure LilyPond is installed:"); + eprintln!(" macOS: brew install lilypond"); + eprintln!(" Ubuntu: sudo apt-get install lilypond"); + eprintln!(" Windows: https://lilypond.org/download.html"); + process::exit(1); } } - println!("{}", close); - } + } else { + // Print sequences to stdout (original behavior) + let (open, close) = if pretty { ("", "") } else { ("[", "]") }; + + for (i, sequence) in sequences.iter().enumerate() { + print!("{}. {}", i + 1, open); + for (j, value) in sequence.values().iter().enumerate() { + if pretty { + print!("{}", value.pretty_print()); + } else { + print!("{}", value); + } + if j < sequence.values().len() - 1 { + print!(", "); + } + } + println!("{}", close); + } - // Print summary - println!("\n{}", "=".repeat(70)); - println!("Total sequences of length {}: {}", length, sequences.len()); - println!("{}", "=".repeat(70)); + // Print summary + println!("\n{}", "=".repeat(70)); + println!("Total sequences of length {}: {}", length, sequences.len()); + println!("{}", "=".repeat(70)); + } } -- cgit v1.2.3