diff --git a/src/main.rs b/src/main.rs index 01d29a4..c95a5c9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,14 +46,38 @@ pub enum Commands { /// Watches changes to source files and re run them watch { - /// Files to run. - files: Vec, + #[clap(subcommand)] + command: WatchSubcommand, }, /// init { path: String }, } +#[allow(non_camel_case_types)] +#[derive(Subcommand)] +pub enum WatchSubcommand { + /// Runs a set of files or the default target. + run { + /// Files to run. + files: Vec, + }, + + /// Runs tests contained within a particular test file or + test { + /// Wether to capture standard output or not. + #[clap(short, long)] + capture: bool, + + /// Files to run tests from. + files: Vec, + + /// Specific tests to run. + #[clap(short, long)] + tests: Vec, + }, +} + fn append_includes(list: &mut Vec) { list.extend( Config::get_current() @@ -81,6 +105,9 @@ fn main() { match args.command { Commands::check { files } => check::main(files), Commands::run { mut files } => { + if files.is_empty() { + files.push(Config::get_current().main_file().to_string()); + } append_includes(&mut files); let args = compilation_args(); run::main(files, args); @@ -95,11 +122,30 @@ fn main() { let tests = (!tests.is_empty()).then_some(tests); test::main(capture, files, tests) } - Commands::watch { mut files } => { + Commands::watch { + command: WatchSubcommand::run { mut files }, + } => { append_includes(&mut files); let args = compilation_args(); - watch::main(files, args) + let files = files.clone(); + watch::main(files, move || { + run::main(files.clone(), args.clone()); + }) } + Commands::watch { + command: WatchSubcommand::test { + mut files, + tests, + capture, + }, + } => { + append_includes(&mut files); + let args = compilation_args(); + watch::main(files, move || { + run::main(files.clone(), args.clone()); + }) + } + Commands::init { path } => config::create(path), } } diff --git a/src/watch.rs b/src/watch.rs index a8f8e2c..61489b9 100644 --- a/src/watch.rs +++ b/src/watch.rs @@ -24,12 +24,9 @@ impl Repeater { } } -pub fn main(files: Vec, args: Vec) { +pub fn main(files: Vec, op: impl Fn() + 'static) { log_process(&format!("watching files '{files:?}'")); - let passed = files.clone(); - let repeater = Repeater::new(move || { - crate::run::main(passed.clone(), args.clone()); - }); + let repeater = Repeater::new(op); repeater.repeat(); let (send, rec) = mpsc::channel();