demogod/lib.rs
1//! **demogod** records demos from a tape: a terminal, a browser beside it, or both, typed into
2//! and clicked through by a script, and saved as a GIF, an MP4 or WebM, a PNG, or an asciicast.
3//!
4//! ```text
5//! Output demo.gif
6//! Set Theme "Catppuccin Mocha"
7//!
8//! Caption "Run the tests" "in a real shell"
9//! Type "npm test"
10//! Enter
11//! Wait /passing/
12//! Sleep 2s
13//! ```
14//!
15//! The tape language is VHS's, so a VHS tape is a demogod tape; demogod adds captions, a browser
16//! pane, and actions run off camera. The terminal is a real PTY rendered by demogod itself — no
17//! ttyd, no browser, no ffmpeg — so a terminal demo needs nothing but this crate. And under the
18//! default tape timing, every step lasts as long as the tape says it does, not as long as the
19//! machine took: the same tape makes the same film, on a laptop or a busy CI runner.
20//!
21//! # Recording from Rust
22//!
23//! ```no_run
24//! use demogod::Demo;
25//!
26//! let saved = Demo::from_file("demo.tape")?
27//! // `Do break` in the tape runs this, off camera.
28//! .action("break", || std::fs::write("test/cart-test.ts", "// broken"))
29//! .run()?;
30//! for file in saved {
31//! println!("{}: {} bytes", file.path.display(), file.bytes);
32//! }
33//! # Ok::<(), demogod::Error>(())
34//! ```
35//!
36//! [`Demo::record`] returns the [`Recording`] instead, to save wherever and however often.
37//! [`Tape::parse`] reads a tape without running anything, and [`Demo::check`] finds what would
38//! fail — a missing program, an undeclared action — before a recording starts.
39
40mod browser;
41mod cdp;
42mod color;
43mod demo;
44mod encode;
45mod error;
46mod font;
47mod image;
48mod key;
49mod recording;
50mod render;
51mod settings;
52mod tape;
53mod terminal;
54mod theme;
55mod timeline;
56
57pub use color::Rgb;
58pub use demo::{Demo, Event};
59pub use error::{Error, Location, Result};
60pub use key::{Key, KeyCode};
61pub use recording::{Recording, Saved};
62pub use settings::{Length, LoopOffset, Settings, Timing, WindowBar};
63pub use tape::{Caption, Command, Focus, Pattern, Scene, ScrollTarget, Step, Tape, WaitScope};
64pub use theme::Theme;
65
66/// Where a program is on PATH, the way a shell would find it: as named, or on Windows with any
67/// of `PATHEXT`'s extensions added.
68pub(crate) fn which(program: &str) -> Option<std::path::PathBuf> {
69 let mut names = vec![program.to_string()];
70 if cfg!(windows) {
71 let extensions = std::env::var("PATHEXT").unwrap_or(".EXE;.CMD;.BAT".into());
72 names.extend(
73 extensions
74 .split(';')
75 .filter(|extension| !extension.is_empty())
76 .map(|extension| format!("{program}{extension}")),
77 );
78 }
79 let path = std::path::Path::new(program);
80 if path.components().count() > 1 {
81 return names.iter().map(std::path::PathBuf::from).find(|candidate| candidate.is_file());
82 }
83
84 std::env::split_paths(&std::env::var_os("PATH")?)
85 .find_map(|directory| names.iter().map(|name| directory.join(name)).find(|candidate| candidate.is_file()))
86}
87
88#[cfg(test)]
89mod tests {
90 #[test]
91 fn which_finds_programs_by_name_or_path_and_not_what_is_missing() {
92 let shell = super::which("sh").expect("sh is on PATH");
93 assert!(shell.is_file());
94 assert_eq!(super::which(shell.to_str().unwrap()), Some(shell));
95 assert_eq!(super::which("definitely-not-a-program-9000"), None);
96 }
97}