texlang_stdlib/
sleep.rs

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