Cotton code
48 lines · 1.6 KB · RustRaw
//! A scene's worked-out figures: words, pages (in eighths), minutes and who//! speaks, kept by the core as the body changes.use serde::{Deserialize, Serialize};use super::layout;use super::lines::{lines, Kind};use super::names;/// Lines on a page of script (Courier 12 pt, six lines an inch).pub const LINES_PER_PAGE: usize = 55;#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]pub struct Figures {    pub words: u64,    /// Pages in eighths: `0.375` is three eighths.    pub pages: f64,    /// A page a minute.    pub minutes: f64,    /// The names that speak, in first-speech order.    pub speakers: Vec<String>,    /// Each speaker's speeches, in the same order.    pub speeches: Vec<(String, u64)>,}/// Pages for so many lines of script, rounded up to an eighth; nothing for/// no lines.pub fn pages_of(rows: usize) -> f64 {    if rows == 0 {        return 0.0;    }    let eighths = (rows * 8).div_ceil(LINES_PER_PAGE).max(1);    eighths as f64 / 8.0}/// The figures of one scene, from its heading and body.pub fn of_scene(heading: &str, body: &str, names: &[String]) -> Figures {    let read = lines(body, names);    let words = read        .iter()        .filter(|line| !matches!(line.kind, Kind::Note | Kind::Blank))        .flat_map(|line| line.text.split_whitespace())        .filter(|word| word.chars().any(char::is_alphanumeric))        .count() as u64;    let rows = layout::scene_rows(heading, &read);    let pages = pages_of(rows);    Figures { words, pages, minutes: pages, speakers: names::speakers(&read), speeches: names::speeches(&read) }}