calepin 0.0.9

A Rust CLI for preprocessing Typst documents with executable code chunks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
mod assets;
mod watcher;

use std::fs;
use std::io::{self, BufRead, BufReader, Read, Write};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread;
use std::time::Duration;

use anyhow::{anyhow, Context, Result};

use crate::cli::{StopArgs, WatchArgs};
use crate::html::prepare_html_theme;
use crate::typst::compile::{reject_reserved_typst_inputs, resolve_output_path, typst_watch_args};
use crate::typst::paths::resolve_layout;
use crate::typst::preprocess::{
    execute_preprocess_plan, prepare_preprocess_plan, preprocess, PreprocessOptions,
};

const WATCH_PID_FILENAME: &str = "watch.pid";

fn preprocess_options(args: &WatchArgs, sync_pages: bool) -> PreprocessOptions {
    PreprocessOptions {
        input: args.input.clone(),
        config: args.common.config.clone(),
        quiet: args.common.quiet,
        timeout: args.common.timeout,
        sync_pages,
    }
}

pub fn run_watch(args: WatchArgs) -> Result<()> {
    let format = args.format.map(|format| format.as_str().to_string());
    let sync_pages = format.as_deref().unwrap_or("pdf") == "pdf";
    reject_reserved_typst_inputs(&args.typst_args)?;

    let initial = preprocess(preprocess_options(&args, sync_pages))?;
    let prepared_theme = prepare_html_theme(
        &initial.layout.root,
        format.as_deref(),
        None,
        None,
        None,
    )?;

    let stop = Arc::new(AtomicBool::new(false));
    let stop_for_handler = Arc::clone(&stop);
    ctrlc::set_handler(move || {
        stop_for_handler.store(true, Ordering::Relaxed);
    })
    .context("failed to set Ctrl+C handler")?;

    let resolved_output = resolve_output_path(
        &initial.layout,
        args.output.as_deref(),
        format.as_deref(),
    );
    let root = initial.layout.root.clone();
    let asset_server = if format.as_deref() == Some("html") {
        let server = assets::start(root.clone(), Arc::clone(&stop))?;
        if !args.common.quiet {
            eprintln!("serving Calepin assets at {}", server.base_url());
        }
        Some(server)
    } else {
        None
    };

    let watch_args = typst_watch_args(
        &initial.layout,
        args.output.as_deref(),
        format.as_deref(),
        &args.typst_args,
        prepared_theme.raw_theme_input.as_deref(),
        asset_server.as_ref().map(|server| server.base_url()),
    );

    let child = Command::new(&initial.executables.typst)
        .args(&watch_args)
        .current_dir(&root)
        .stdin(Stdio::inherit())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .with_context(|| {
            format!(
                "failed to start {} watch",
                initial.executables.typst.display()
            )
        });
    let mut child = match child {
        Ok(child) => child,
        Err(error) => {
            stop.store(true, Ordering::Relaxed);
            if let Some(server) = asset_server {
                server.join();
            }
            return Err(error);
        }
    };
    let watch_pid_path = watch_pid_file_path(&initial.layout.results_path);
    if let Err(error) = write_watch_pid_file(&watch_pid_path, child.id()) {
        cwarn!("failed to write watch pid file {}: {}", watch_pid_path.display(), error);
    }
    let stdout = child
        .stdout
        .take()
        .context("failed to capture typst watch stdout")?;
    let stderr = child
        .stderr
        .take()
        .context("failed to capture typst watch stderr")?;
    let stdout_relay = thread::spawn(move || relay_stdout(stdout));
    let stderr_relay = thread::spawn(move || relay_stderr(stderr));

    let watcher_stop = Arc::clone(&stop);
    let watcher_args = args.clone();
    let watcher_root = root.clone();
    let watcher_output = resolved_output.clone();
    let quiet = args.common.quiet;
    let watcher = thread::spawn(move || {
        let options = preprocess_options(&watcher_args, sync_pages);
        let mut last_fingerprint = initial.fingerprint;
        let result = watcher::watch_root(
            &watcher_root,
            &watcher_output,
            watcher_args.common.config.as_deref(),
            Arc::clone(&watcher_stop),
            move |changed| match prepare_preprocess_plan(options.clone()) {
                Ok(plan) => {
                    if plan.fingerprint == last_fingerprint && plan.layout.results_path.exists() {
                        return;
                    }
                    if !quiet {
                        let names = changed
                            .iter()
                            .filter_map(|path| path.file_name())
                            .map(|name| name.to_string_lossy().to_string())
                            .collect::<Vec<_>>()
                            .join(", ");
                        eprintln!("rebuilding {names}...");
                    }
                    match execute_preprocess_plan(plan) {
                        Ok(output) => {
                            last_fingerprint = output.fingerprint;
                        }
                        Err(error) => {
                            cwarn!("rebuild failed: {}", error);
                        }
                    }
                }
                Err(error) => {
                    cwarn!("rebuild failed: {}", error);
                }
            },
        );
        if let Err(error) = result {
            cwarn!("watch error: {}", error);
        }
    });

    loop {
        if stop.load(Ordering::Relaxed) {
            break;
        }
        match child.try_wait() {
            Ok(Some(_status)) => break,
            Ok(None) => thread::sleep(Duration::from_millis(200)),
            Err(error) => {
                cwarn!("failed to poll typst watch: {}", error);
                break;
            }
        }
    }

    stop.store(true, Ordering::Relaxed);
    let _ = child.kill();
    let _ = child.wait();
    let _ = remove_watch_pid_file(&watch_pid_path);
    join_relay("stdout", stdout_relay);
    join_relay("stderr", stderr_relay);
    let _ = watcher.join();
    if let Some(server) = asset_server {
        server.join();
    }
    Ok(())
}

pub fn run_stop(args: StopArgs) -> Result<()> {
    match args.input {
        Some(input) => {
            let layout = resolve_layout(&input, None)
                .with_context(|| format!("failed to resolve watch context from {}", input.display()))?;
            let watch_pid_path = watch_pid_file_path(&layout.results_path);
            stop_watch_from_pid_file(&watch_pid_path)?;
        }
        None => {
            let calepin_dir = std::env::current_dir()?.join(".calepin");
            let mut watch_pid_paths = Vec::new();
            collect_watch_pid_files(&calepin_dir, &mut watch_pid_paths)?;
            if watch_pid_paths.is_empty() {
                return Err(anyhow!("no running calepin watch found"));
            }
            for path in watch_pid_paths {
                stop_watch_from_pid_file(&path)?;
            }
        }
    }
    Ok(())
}

fn stop_watch_from_pid_file(path: &Path) -> Result<bool> {
    let pid = read_watch_pid(path)?;
    let stopped = if is_process_running(pid) {
        terminate_process(pid)?;
        true
    } else {
        false
    };
    let _ = fs::remove_file(path);
    Ok(stopped)
}

fn read_watch_pid(path: &Path) -> Result<u32> {
    let contents = fs::read_to_string(path)
        .with_context(|| format!("failed to read watch pid file {}", path.display()))?;
    contents
        .trim()
        .parse::<u32>()
        .with_context(|| format!("invalid watch pid in {}", path.display()))
}

fn write_watch_pid_file(path: &Path, pid: u32) -> Result<()> {
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent)
            .with_context(|| format!("failed to create {}", parent.display()))?;
    }
    fs::write(path, pid.to_string())
        .with_context(|| format!("failed to write watch pid file {}", path.display()))
}

fn remove_watch_pid_file(path: &Path) -> Result<()> {
    fs::remove_file(path).context("failed to remove watch pid file")?;
    Ok(())
}

fn watch_pid_file_path(results_path: &Path) -> PathBuf {
    let parent = results_path
        .parent()
        .expect("results path should have a parent directory");
    parent.join(WATCH_PID_FILENAME)
}

fn collect_watch_pid_files(dir: &Path, out: &mut Vec<PathBuf>) -> Result<()> {
    let entries = match fs::read_dir(dir) {
        Ok(entries) => entries,
        Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(()),
        Err(error) => return Err(error.into()),
    };
    for entry in entries {
        let entry = entry?;
        let file_type = entry.file_type()?;
        let entry_path = entry.path();
        if file_type.is_dir() {
            collect_watch_pid_files(&entry_path, out)?;
        } else if file_type.is_file() && entry.file_name() == WATCH_PID_FILENAME {
            out.push(entry_path);
        }
    }
    Ok(())
}

#[cfg(unix)]
fn is_process_running(pid: u32) -> bool {
    Command::new("kill")
        .arg("-0")
        .arg(pid.to_string())
        .status()
        .map(|status| status.success())
        .unwrap_or(false)
}

#[cfg(unix)]
fn terminate_process(pid: u32) -> Result<()> {
    let status = Command::new("kill")
        .arg("-TERM")
        .arg(pid.to_string())
        .status()
        .with_context(|| format!("failed to signal watch process {pid}"))?;
    if !status.success() {
        return Err(anyhow!("failed to signal watch process {pid}"));
    }
    Ok(())
}

#[cfg(windows)]
fn is_process_running(_pid: u32) -> bool {
    true
}

#[cfg(windows)]
fn terminate_process(pid: u32) -> Result<()> {
    let status = Command::new("taskkill")
        .arg("/PID")
        .arg(pid.to_string())
        .arg("/F")
        .status()
        .with_context(|| format!("failed to signal watch process {pid}"))?;
    if !status.success() {
        return Err(anyhow!("failed to signal watch process {pid}"));
    }
    Ok(())
}

fn relay_stdout<R>(mut reader: R) -> io::Result<()>
where
    R: Read,
{
    relay_typst_watch_output(&mut reader, io::stdout())
}

fn relay_stderr<R>(mut reader: R) -> io::Result<()>
where
    R: Read,
{
    relay_typst_watch_output(&mut reader, io::stderr())
}

fn relay_typst_watch_output<R, W>(reader: R, writer: W) -> io::Result<()>
where
    R: Read,
    W: Write,
{
    let mut reader = BufReader::new(reader);
    let mut relay = TypstWatchOutputRelay::new(writer);
    let mut line = String::new();

    loop {
        line.clear();
        let bytes = reader.read_line(&mut line)?;
        if bytes == 0 {
            break;
        }
        relay.write_line(&line)?;
    }

    relay.finish()
}

struct TypstWatchOutputRelay<W> {
    writer: W,
    seen_watching: bool,
    seen_writing: bool,
    pending_blank: bool,
}

impl<W: Write> TypstWatchOutputRelay<W> {
    fn new(writer: W) -> Self {
        Self {
            writer,
            seen_watching: false,
            seen_writing: false,
            pending_blank: false,
        }
    }

    fn write_line(&mut self, line: &str) -> io::Result<()> {
        let line = line.trim_end_matches(['\r', '\n']);
        if line.trim().is_empty() {
            self.pending_blank = true;
            return Ok(());
        }

        let should_write = match typst_watch_line(line) {
            TypstWatchLine::Watching => {
                let first = !self.seen_watching;
                self.seen_watching = true;
                first
            }
            TypstWatchLine::Writing => {
                let first = !self.seen_writing;
                self.seen_writing = true;
                first
            }
            TypstWatchLine::Status => true,
            TypstWatchLine::Other => {
                if self.pending_blank {
                    writeln!(self.writer)?;
                }
                true
            }
        };
        self.pending_blank = false;

        if should_write {
            writeln!(self.writer, "{line}")?;
        }
        Ok(())
    }

    fn finish(mut self) -> io::Result<()> {
        self.writer.flush()
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum TypstWatchLine {
    Watching,
    Writing,
    Status,
    Other,
}

fn typst_watch_line(line: &str) -> TypstWatchLine {
    if line.starts_with("watching ") {
        TypstWatchLine::Watching
    } else if line.starts_with("writing to ") {
        TypstWatchLine::Writing
    } else if line.starts_with('[') && line.contains("] ") {
        TypstWatchLine::Status
    } else {
        TypstWatchLine::Other
    }
}

fn join_relay(name: &str, handle: thread::JoinHandle<io::Result<()>>) {
    match handle.join() {
        Ok(Ok(())) => {}
        Ok(Err(error)) => {
            cwarn!("failed to relay typst watch {name}: {}", error);
        }
        Err(_) => {
            cwarn!("typst watch {name} relay panicked");
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn relay_typst_watch_output_compacts_status_lines() {
        let input = "\
watching example.typ
writing to example.pdf

[10:17:43] compiling ...

watching example.typ
writing to example.pdf

[10:17:43] compiled successfully in 88.22 ms

watching example.typ
writing to example.pdf

[10:17:53] compiling ...
";
        let mut output = Vec::new();

        relay_typst_watch_output(input.as_bytes(), &mut output).unwrap();

        assert_eq!(
            String::from_utf8(output).unwrap(),
            "\
watching example.typ
writing to example.pdf
[10:17:43] compiling ...
[10:17:43] compiled successfully in 88.22 ms
[10:17:53] compiling ...
"
        );
    }

    #[test]
    fn relay_typst_watch_output_preserves_diagnostic_spacing() {
        let input = "\
error: failed

hint: check this
";
        let mut output = Vec::new();

        relay_typst_watch_output(input.as_bytes(), &mut output).unwrap();

        assert_eq!(
            String::from_utf8(output).unwrap(),
            "\
error: failed

hint: check this
"
        );
    }
}