diff --git a/compiler/src/cli.rs b/compiler/src/cli.rs index eebb74d..68681ac 100644 --- a/compiler/src/cli.rs +++ b/compiler/src/cli.rs @@ -46,6 +46,10 @@ pub struct WatchArgs { /// Path to a file or directory to watch pub watch: String, + /// Initially compile the document when watch is started + #[arg(short = 'i', long, action)] + pub initial_make: bool, + #[command(flatten)] pub com: CommonArgGroup, } diff --git a/compiler/src/main.rs b/compiler/src/main.rs index 11f336d..2d19020 100644 --- a/compiler/src/main.rs +++ b/compiler/src/main.rs @@ -55,6 +55,7 @@ fn main() { &str_to_pathbuf(&o.com.outdir, true)?, &str_to_pathbuf(&o.watch, true)?, args_to_comp_verbosity_enum(o.com.verbose), + o.initial_make, )?; return Ok(()); diff --git a/compiler/src/watch.rs b/compiler/src/watch.rs index f47bcae..068f3dd 100644 --- a/compiler/src/watch.rs +++ b/compiler/src/watch.rs @@ -38,6 +38,7 @@ pub fn watch_sync + Debug>( outdir: P, watch: P, verbosity: compiler::OutputVerbosity, + initial_make: bool, ) -> CompResult<()> { let (tx, rx) = std::sync::mpsc::channel(); let texpdfc = &texpdfc.as_ref().to_path_buf(); @@ -48,6 +49,12 @@ pub fn watch_sync + Debug>( .verbosity(verbosity) .to_owned(); + // first compilation done here, if it is specified + if initial_make { + log::info("Initial pre-watch compilation (-i) specified..."); + watch_compile(&compiler); + } + // set up debouncer watcher let mut debouncer = new_debouncer( Duration::from_millis(500), // this value seems to debounce successfully without too long of a delay @@ -88,15 +95,7 @@ pub fn watch_sync + Debug>( // check if any of the recieved errors are to be tracked if evs.iter().any(|ev| tracked_evs.contains(&ev.kind)) { - // handle the error here (safely) instead of in main -- we don't want to exit the program on error when watching. - if let Err(e) = || -> CompResult { - compiler - .compile() - .map_err(|_| CompError::CompilationError("Compile error".to_string())) - }() { - // this just prints the error without stopping the process - e.handle_safe(); - } + watch_compile(&compiler); } } Err(errs) => { @@ -109,3 +108,15 @@ pub fn watch_sync + Debug>( Ok(()) } + +fn watch_compile(compiler: &Compiler) { + // handle the error here (safely) instead of in main -- we don't want to exit the program on error when watching. + if let Err(e) = || -> CompResult { + compiler + .compile() + .map_err(|_| CompError::CompilationError("Compile error".to_string())) + }() { + // this just prints the error without stopping the process + e.handle_safe(); + } +}