texlang_stdlib/
sleep.rs

1//! Primitive that pauses execution for a duration of time
2
3use core::time;
4use std::thread;
5use texlang::traits::*;
6use texlang::*;
7
8pub const SLEEP_DOC: &str = "Sleep for a number of milliseconds";
9
10/// Get the `\sleep` expansion primitive.
11pub fn get_sleep<S: TexlangState>() -> command::BuiltIn<S> {
12    command::BuiltIn::new_execution(
13        |_: token::Token, input: &mut vm::ExecutionInput<S>| -> command::Result<()> {
14            let milliseconds = usize::parse(input)?;
15            writeln![
16                input.vm().terminal.borrow_mut(),
17                "\\sleep: sleeping for {milliseconds}ms",
18            ]
19            .unwrap();
20            thread::sleep(time::Duration::from_millis(milliseconds as u64));
21            Ok(())
22        },
23    )
24}