From 2f1aa8dd458b7369e48f9be84777587acbbc4306 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Wed, 4 Nov 2026 16:46:34 +0100 Subject: [PATCH] Make duration margin an independent toggle --- crates/rundown-converter/src/detect.rs | 2 + crates/rundown-converter/src/gui.rs | 12 ++++-- crates/rundown-converter/src/gui/review.rs | 12 ++++++ crates/rundown-converter/src/model.rs | 4 ++ crates/rundown-converter/src/pipeline.rs | 1 + crates/rundown-converter/src/time.rs | 16 +++++-- crates/rundown-converter/tests/conversion.rs | 45 ++++++++++++++++++++ docs/rundown-converter-plan.md | 2 +- docs/rundown-converter.md | 17 +++++--- 9 files changed, 96 insertions(+), 15 deletions(-) diff --git a/crates/rundown-converter/src/detect.rs b/crates/rundown-converter/src/detect.rs index f8f239c..e407d7f 100644 --- a/crates/rundown-converter/src/detect.rs +++ b/crates/rundown-converter/src/detect.rs @@ -768,6 +768,7 @@ fn infer_inner( unit.as_deref(), profile.duration_mode.as_deref(), profile.duration_split, + profile.duration_add_margin, profile.duration_separator.as_deref(), ) .is_some(), @@ -796,6 +797,7 @@ fn infer_inner( duration_mode: profile.duration_mode.clone(), duration_separator: profile.duration_separator.clone(), duration_split: profile.duration_split, + duration_add_margin: profile.duration_add_margin, evidence: evidence.clone(), column_usage, column_labels, diff --git a/crates/rundown-converter/src/gui.rs b/crates/rundown-converter/src/gui.rs index 0a09012..197f9ee 100644 --- a/crates/rundown-converter/src/gui.rs +++ b/crates/rundown-converter/src/gui.rs @@ -87,7 +87,6 @@ fn duration_mode_label(mode: &str) -> &'static str { "minimum" => "Minimum", "maximum" => "Maximum", "sum" => "Sum", - "margin" => "Add margin", _ => "Use the whole cell", } } @@ -97,6 +96,10 @@ fn duration_handling_controls(ui: &mut egui::Ui, profile: &mut Profile, id: egui profile.duration_mode = Some("sum".into()); profile.duration_split = true; } + if profile.duration_mode.as_deref() == Some("margin") { + profile.duration_mode = Some("maximum".into()); + profile.duration_add_margin = true; + } ui.indent(id.with("indent"), |ui| { ui.horizontal_wrapped(|ui| { ui.weak("Duration handling"); @@ -112,14 +115,15 @@ fn duration_handling_controls(ui: &mut egui::Ui, profile: &mut Profile, id: egui ("minimum", "Minimum"), ("maximum", "Maximum"), ("sum", "Sum"), - ("margin", "Add margin"), ] { ui.selectable_value(&mut mode, value.into(), label); } }) .response - .on_hover_text("Choose how duration values are extracted when one cell contains several values. Add margin uses the largest value for the event and keeps the remainder as a gap."); + .on_hover_text("Choose how duration values are extracted when one cell contains several values."); profile.duration_mode = (mode != "strict").then_some(mode.clone()); + ui.checkbox(&mut profile.duration_add_margin, "Add margin") + .on_hover_text("Keep the difference between the sum of source durations and the selected duration as a gap after the event. With splitting, apply this to each entry. Sum leaves no remainder."); ui.checkbox(&mut profile.duration_split, "Split into multiple entries"); if profile.duration_split { let separator = profile @@ -600,7 +604,7 @@ impl App { self.decisions_changed(); } } - ui.label("Optional JSON profile: columns (start/start_end/end/duration/title/secondary_title), column_usage (notes/metadata/ignore), secondary_title_mode, duration_unit, duration_mode, duration_split, duration_separator, row_kinds, titles, starts, ends, durations, untimed_as_notes, ignore_warnings. Column letters and source rows are shown in diagnostics."); + ui.label("Optional JSON profile: columns (start/start_end/end/duration/title/secondary_title), column_usage (notes/metadata/ignore), secondary_title_mode, duration_unit, duration_mode, duration_add_margin, duration_split, duration_separator, row_kinds, titles, starts, ends, durations, untimed_as_notes, ignore_warnings. Column letters and source rows are shown in diagnostics."); if ui .add( egui::TextEdit::multiline(&mut self.profile) diff --git a/crates/rundown-converter/src/gui/review.rs b/crates/rundown-converter/src/gui/review.rs index 51d0548..6e4339c 100644 --- a/crates/rundown-converter/src/gui/review.rs +++ b/crates/rundown-converter/src/gui/review.rs @@ -596,6 +596,7 @@ fn column_settings_changed(current: &Profile, analyzed: &Profile) -> bool { || current.duration_mode != analyzed.duration_mode || current.duration_separator != analyzed.duration_separator || current.duration_split != analyzed.duration_split + || current.duration_add_margin != analyzed.duration_add_margin } fn schedule_settings_changed(current: &Profile, analyzed: &Profile) -> bool { @@ -3533,6 +3534,17 @@ mod tests { assert_eq!(profile.duration_mode.as_deref(), Some("maximum")); let labels = render(&mut app, &ctx, size, vec![]); assert!(labels.iter().any(|(text, _)| text == "Separator")); + let margin = labels + .iter() + .find(|(text, _)| text == "Add margin") + .unwrap() + .1 + .center(); + click_size(&mut app, &ctx, margin, size); + let profile: Profile = serde_json::from_str(&app.profile).unwrap(); + assert!(profile.duration_add_margin); + assert!(profile.duration_split); + assert_eq!(profile.duration_mode.as_deref(), Some("maximum")); } #[test] diff --git a/crates/rundown-converter/src/model.rs b/crates/rundown-converter/src/model.rs index 16fd015..67928dc 100644 --- a/crates/rundown-converter/src/model.rs +++ b/crates/rundown-converter/src/model.rs @@ -77,6 +77,8 @@ pub struct Profile { pub duration_separator: Option, #[serde(default)] pub duration_split: bool, + #[serde(default)] + pub duration_add_margin: bool, /// Explicitly retain rows without times as notes on the preceding event. pub untimed_as_notes: bool, /// Try excluding rows with unresolved-content warnings, then validate again. @@ -123,6 +125,8 @@ pub struct Mapping { pub duration_separator: Option, #[serde(default)] pub duration_split: bool, + #[serde(default)] + pub duration_add_margin: bool, pub evidence: Vec, #[serde(default)] pub column_usage: BTreeMap, diff --git a/crates/rundown-converter/src/pipeline.rs b/crates/rundown-converter/src/pipeline.rs index bb55485..e08bd66 100644 --- a/crates/rundown-converter/src/pipeline.rs +++ b/crates/rundown-converter/src/pipeline.rs @@ -62,6 +62,7 @@ fn duration_plan( map.duration_unit.as_deref(), profile.duration_mode.as_deref(), profile.duration_split, + profile.duration_add_margin, profile.duration_separator.as_deref(), ) }) diff --git a/crates/rundown-converter/src/time.rs b/crates/rundown-converter/src/time.rs index 2d55307..55a78da 100644 --- a/crates/rundown-converter/src/time.rs +++ b/crates/rundown-converter/src/time.rs @@ -189,7 +189,7 @@ pub fn select_durations( }); } if mode == "split" { - return select_duration_parts(cell, unit, Some("sum"), true, split_separator); + return select_duration_parts(cell, unit, Some("sum"), true, false, split_separator); } let groups: Vec> = cell .text @@ -246,12 +246,22 @@ pub fn select_duration_parts( unit: Option<&str>, mode: Option<&str>, split: bool, + add_margin: bool, split_separator: Option<&str>, ) -> Option { let legacy = mode == Some("split"); let mode = if legacy { Some("sum") } else { mode }; + let select = |cell: &Cell| -> Option { + let mut selected = select_durations(cell, unit, mode, None)?; + if add_margin && !matches!(mode, None | Some("strict" | "margin")) { + let total = select_durations(cell, unit, Some("sum"), None)?; + selected.margin = + (total.events.iter().sum::() - selected.events.iter().sum::()).max(0); + } + Some(selected) + }; if !split && !legacy { - return select_durations(cell, unit, mode, None); + return select(cell); } let separator = separator(split_separator); if separator.is_empty() { @@ -276,7 +286,7 @@ pub fn select_duration_parts( text: part.into(), ..cell.clone() }; - let selected = select_durations(&part, unit, mode, None)?; + let selected = select(&part)?; result.events.extend(selected.events); result.margins.push(selected.margin); result.margin += selected.margin; diff --git a/crates/rundown-converter/tests/conversion.rs b/crates/rundown-converter/tests/conversion.rs index 30f4fff..e0aa6a0 100644 --- a/crates/rundown-converter/tests/conversion.rs +++ b/crates/rundown-converter/tests/conversion.rs @@ -1519,3 +1519,48 @@ fn split_window_bounds_the_total_and_preserves_slack() { .any(|d| d.code == "CONFLICTING_TIMES" && d.message.contains("sum of split")) ); } + +#[test] +fn margin_toggle_combines_with_minimum_maximum_sum_and_split() { + for split in [false, true] { + let t = table(&[ + &["Start", "Duration", "Title"], + &[ + "09:00", + if split { + "20 min + 5 min\n20 min + 5 min" + } else { + "20 min + 5 min" + }, + "Talks", + ], + ]); + for (mode, duration, margin) in [("minimum", 5, 20), ("maximum", 20, 5), ("sum", 25, 0)] { + let profile = Profile { + duration_mode: Some(mode.into()), + duration_add_margin: true, + duration_split: split, + ..Default::default() + }; + let a = analyze(&t, &profile); + assert!( + a.diagnostics.is_empty(), + "{mode}/{split}: {:?}", + a.diagnostics + ); + let r = a.rundown.unwrap(); + assert_eq!(r.entries.len(), if split { 2 } else { 1 }); + assert!(r.entries.iter().all(|e| e.duration_ms == duration * 60_000)); + assert_eq!(r.gaps.len(), if margin == 0 { 0 } else { r.entries.len() }); + assert!( + r.gaps + .iter() + .all(|g| g.end_ms.unwrap() - g.start_ms.unwrap() == margin * 60_000) + ); + if split { + assert_eq!(r.entries[1].start_ms, 9 * 3_600_000 + 25 * 60_000); + } + assert!(export::ontime(&r).is_ok()); + } + } +} diff --git a/docs/rundown-converter-plan.md b/docs/rundown-converter-plan.md index 485c386..12c710b 100644 --- a/docs/rundown-converter-plan.md +++ b/docs/rundown-converter-plan.md @@ -297,7 +297,7 @@ errors must remain visible with direct corrective actions. errors. Test ambiguous cells clarified by other rows, incompatible meanings in one column, structural rows and misleading headers/units. - [x] Keep one Duration column role while offering nested whole-cell, minimum, - maximum, sum and margin extraction policies, plus an independent split toggle. Let a Start mapping select + maximum and sum extraction policies, plus independent margin and split toggles. Let a Start mapping select only the first clock in a mixed range; retain margin gaps and split entries in the IR. - [x] Add a secondary title role with Either, Merge, Notes and Metadata handling, diff --git a/docs/rundown-converter.md b/docs/rundown-converter.md index 40fdbed..2cc54fe 100644 --- a/docs/rundown-converter.md +++ b/docs/rundown-converter.md @@ -124,9 +124,10 @@ visually distinct from labels and ordinary actions; the active tab uses the stro bounds. **Start** selects only the first clock from a cell, so a mixed `08h45 - 09h10` value can provide the 08:45 anchor while the selected duration column determines the timer. A duration column has a nested **Duration handling** - choice instead of extra column roles: whole cell, minimum, maximum, sum or add - margin. A separate **Split into multiple entries** toggle applies that choice - to each separated group; Add margin then creates a gap after each entry. Selecting one duration column explicitly resolves + choice instead of extra column roles: whole cell, minimum, maximum or sum. + Separate **Add margin** and **Split into multiple entries** toggles can be + combined with that choice. Add margin keeps the unused duration as a gap; + splitting applies the selected handling and margin to each separated group. Selecting one duration column explicitly resolves duplicate TIMER headers. **Secondary title** selects another title source with a nested handling menu: **Either** uses the main title when present and the secondary when it is empty; @@ -220,7 +221,7 @@ values from calculated values, unresolved values and gaps. | Cue at 11:45 lasting 15 minutes; next cue at 13:30 | Preserve a 12:00–13:30 gap and the 13:30 start | | Start–end cell `08h00 - 08h30` | Use 08:00 and 08:30 directly and derive a 30-minute duration | | Start cell `08h45 - 09h10`, duration `20 min + 5 min` with Maximum | Use 08:45 and a 20-minute timer; the unused end in the Start cell does not constrain it | -| Duration `20 min + 5 min` with Add margin | Create a 20-minute event followed by a five-minute untimed gap | +| Duration `20 min + 5 min` with Maximum and Add margin | Create a 20-minute event followed by a five-minute untimed gap | | Multiline duration with Split | Create sequential virtual entries, one for each separated duration group; a start–end range bounds their combined durations and margins, with remaining time kept as a gap | | Named lunch row from 12:00 to 13:30 | Preserve an explicit break with its original label | | Cue at 11:45; next cue at 13:30; no end or duration | Event duration versus break is unknown; do not invent a lunch break | @@ -308,14 +309,16 @@ both titles empty still need correction. `duration_mode` is optional and remains attached to the single `duration` role. `minimum`, `maximum` and `sum` select from explicit duration values in mixed text. -`margin` uses the largest value in each duration group for the event and preserves -the remainder as an untimed gap. `duration_split: true` creates sequential virtual +`duration_add_margin: true` preserves the difference between the source duration +sum and the selected duration as an untimed gap. For `20 min + 5 min`, Maximum +leaves a five-minute gap, Minimum leaves a twenty-minute gap, and Sum leaves none. `duration_split: true` creates sequential virtual events and applies the selected handling to each group separated by `duration_separator`; `\\n`, `\\n\\n` and `\\t` mean newline, blank line and tab. The default `strict` behavior requires the complete cell to be one duration. For non-split policies, a one-value summary line followed by a more detailed multi-value line is treated as an alternative expression, avoiding double -counting. Legacy profiles using `duration_mode: "split"` are read as Sum with +counting. Legacy `duration_mode: "margin"` profiles remain supported; new GUI +choices use Maximum with the Add margin toggle. Legacy profiles using `duration_mode: "split"` are read as Sum with splitting enabled. `column_usage` maps column letters to `notes`, `metadata` or `ignore`. Every choice