diff --git a/crates/rundown-converter/src/gui/review.rs b/crates/rundown-converter/src/gui/review.rs index 4e18c02..b0a165d 100644 --- a/crates/rundown-converter/src/gui/review.rs +++ b/crates/rundown-converter/src/gui/review.rs @@ -33,6 +33,112 @@ fn short(text: &str, count: usize) -> String { result } +const PREVIEW_AMBER: Color32 = Color32::from_rgb(235, 190, 95); +const PREVIEW_RED: Color32 = Color32::from_rgb(245, 125, 120); + +fn needs_information(d: &Diagnostic) -> bool { + d.code.starts_with("MISSING_") + || d.code.starts_with("UNRESOLVED_") + || matches!( + d.code.as_str(), + "UNTIMED_ROW" | "AMBIGUOUS_COLUMN" | "HEADER_REQUIRED" | "ORPHAN_NOTE" | "NO_EVENTS" + ) +} + +struct PreviewRow { + row: usize, + start: String, + end: String, + duration: String, + title: String, + source_only: bool, + issues: Vec, + decisions: String, +} + +/// Combine timed draft entries and omitted problem rows without changing the IR. +fn preview_rows(a: &Analysis, table: Option<&Table>) -> Vec { + let mut rows = Vec::new(); + let mut represented = BTreeSet::new(); + if let Some(r) = &a.rundown { + for e in &r.entries { + let sources: BTreeSet<_> = e.source_rows.iter().map(|s| s.row).collect(); + represented.extend(&sources); + rows.push(PreviewRow { + row: e.source_rows.first().map_or(0, |s| s.row), + start: format!( + "{}{}", + display(e.start_ms), + if e.explicit_start { "" } else { " *" } + ), + end: display(e.end_ms), + duration: if e.kind == "marker" { + "Marker".into() + } else { + display(e.duration_ms) + }, + title: e.title.clone(), + source_only: false, + issues: a + .diagnostics + .iter() + .enumerate() + .filter(|(_, d)| diagnostic_row(d).is_some_and(|r| sources.contains(&r))) + .map(|(i, _)| i) + .collect(), + decisions: e.decisions.join("\n"), + }); + } + } + if let Some(table) = table { + for d in &a.diagnostics { + let Some(row) = diagnostic_row(d).filter(|r| *r <= table.rows.len()) else { + continue; + }; + if !represented.insert(row) { + continue; + } + let raw = |col: Option| { + col.map(|c| table.cell(row - 1, c).text) + .filter(|s| !s.trim().is_empty() && !matches!(s.trim(), "-" | "—")) + .unwrap_or("?".into()) + }; + let title = a + .mapping + .as_ref() + .map(|m| table.cell(row - 1, m.title).text) + .filter(|s| !s.trim().is_empty()) + .unwrap_or_else(|| { + table.rows[row - 1] + .iter() + .enumerate() + .filter(|(_, c)| !c.text.trim().is_empty()) + .map(|(c, v)| format!("{}: {}", column_name(c), v.text)) + .collect::>() + .join(" · ") + }); + rows.push(PreviewRow { + row, + start: raw(a.mapping.as_ref().map(|m| m.start)), + end: raw(a.mapping.as_ref().and_then(|m| m.end)), + duration: raw(a.mapping.as_ref().and_then(|m| m.duration)), + title, + source_only: true, + issues: a + .diagnostics + .iter() + .enumerate() + .filter(|(_, d)| diagnostic_row(d) == Some(row)) + .map(|(i, _)| i) + .collect(), + decisions: String::new(), + }); + } + } + rows.sort_by_key(|r| r.row); + rows +} + impl App { pub(super) fn decisions_changed(&mut self) { self.dirty = true; @@ -112,7 +218,7 @@ impl App { .as_ref() .is_some_and(|a| !a.diagnostics.is_empty()) { - ui.label("Draft schedule: unresolved rows may be missing. Resolve the issues before export."); + ui.label("Draft schedule: highlighted rows need attention before export."); } egui::ScrollArea::both() .id_salt("schedule_preview_scroll") @@ -587,49 +693,191 @@ impl App { } fn schedule_preview(&mut self, ui: &mut egui::Ui) { - if let Some(a) = &self.analysis { - ui.heading(format!("Schedule preview · {}", a.sheet)); - if let Some(r) = &a.rundown { - ui.separator(); - ui.label(format!( - "{} entries · {} untimed gaps", - r.entries.len(), - r.gaps.len() - )); - egui::Grid::new("schedule").striped(true).show(ui, |ui| { - for label in ["Row", "Start", "End", "Duration", "Title"] { - ui.strong(label); + let Some(a) = &self.analysis else { + ui.label("Apply & recheck to update the schedule. Export is blocked until these decisions pass validation."); + return; + }; + ui.heading(format!("Schedule preview · {}", a.sheet)); + ui.horizontal_wrapped(|ui| { + ui.colored_label(PREVIEW_AMBER, "Unresolved: missing information"); + ui.colored_label(PREVIEW_RED, "Error: inconsistent or invalid data"); + }); + ui.label("Click a highlighted status to resolve it. ? means unknown; source-only rows are not timers."); + let table = self.tables.iter().find(|t| t.name == a.sheet); + let rows = preview_rows(a, table); + let mut resolve = None; + let mut details = None; + let backgrounds: Vec<_> = std::iter::once(None) + .chain(rows.iter().map(|row| { + if row + .issues + .iter() + .any(|i| !needs_information(&a.diagnostics[*i])) + { + Some(Color32::from_rgb(58, 30, 33)) + } else if row.source_only || !row.issues.is_empty() { + Some(Color32::from_rgb(52, 43, 27)) + } else { + None + } + })) + .collect(); + egui::Grid::new("schedule") + .with_row_color(move |row, style| { + backgrounds + .get(row) + .copied() + .flatten() + .or_else(|| (row > 0 && row % 2 == 0).then_some(style.visuals.faint_bg_color)) + }) + .show(ui, |ui| { + for label in [ + "Row", + "Start", + "End", + "Duration", + "Title / source contents", + "Status", + ] { + ui.strong(label); + } + ui.end_row(); + for row in &rows { + let error = row + .issues + .iter() + .any(|i| !needs_information(&a.diagnostics[*i])); + let color = if error { + PREVIEW_RED + } else if row.source_only || !row.issues.is_empty() { + PREVIEW_AMBER + } else { + ui.visuals().text_color() + }; + for text in [ + row.row.to_string(), + row.start.clone(), + row.end.clone(), + row.duration.clone(), + ] { + ui.colored_label(color, text); + } + ui.vertical(|ui| { + ui.set_width(260.0); + if ui + .add( + egui::Label::new(RichText::new(short(&row.title, 80)).color(color)) + .wrap() + .sense(egui::Sense::click()), + ) + .on_hover_text(&row.title) + .clicked() + { + details = Some(row.row); + } + if row.source_only { + ui.small("Source row · not a timer"); + } + if let Some(index) = row.issues.first() { + let message = a.diagnostics[*index] + .message + .split("\nRow preview:") + .next() + .unwrap_or_default(); + ui.label(RichText::new(short(message, 95)).small().color(color)) + .on_hover_text(message); + } + }); + if row.issues.is_empty() { + ui.label(if row.source_only { + "Unresolved" + } else if row.duration == "Marker" { + "Marker" + } else { + "Timed" + }) + .on_hover_text(&row.decisions); + } else { + let index = row + .issues + .iter() + .find(|i| !needs_information(&a.diagnostics[**i])) + .unwrap_or(&row.issues[0]); + let label = if error { + "Error · resolve" + } else { + "Unresolved · resolve" + }; + let explanation = row + .issues + .iter() + .map(|i| { + format!( + "{} — {}\n{}", + a.diagnostics[*i].code, + a.diagnostics[*i].location, + a.diagnostics[*i].message + ) + }) + .collect::>() + .join("\n\n"); + if ui + .button(RichText::new(label).color(color)) + .on_hover_text(explanation) + .clicked() + { + resolve = Some(*index); + } } ui.end_row(); - for e in &r.entries { - ui.label(e.source_rows.first().map_or(0, |s| s.row).to_string()); - ui.label(format!( - "{}{}", - display(e.start_ms), - if e.explicit_start { "" } else { " *" } - )); - ui.label(display(e.end_ms)); - ui.label(if e.kind == "marker" { - "Marker".into() - } else { - display(e.duration_ms) - }); - ui.label(&e.title).on_hover_text(e.decisions.join("\n")); - ui.end_row(); - } - }); - ui.label("* Inferred start. Hover a title for timing decisions."); - for g in &r.gaps { - ui.label(format!( - "Gap: {} → {} · {} (no timer)", - g.start_ms.map(display).unwrap_or("?".into()), - g.end_ms.map(display).unwrap_or("?".into()), - g.title - )); } + }); + ui.small("* Inferred start. Hover Timed for timing decisions. Highlighted rows retain their original position."); + // Sheet-level and gap diagnostics must remain reachable even without a source-row address. + for (i, d) in a + .diagnostics + .iter() + .enumerate() + .filter(|(i, _)| !rows.iter().any(|r| r.issues.contains(i))) + { + let color = if needs_information(d) { + PREVIEW_AMBER + } else { + PREVIEW_RED + }; + if ui + .button( + RichText::new(format!("{} · {} · resolve", d.code, d.location)).color(color), + ) + .on_hover_text(&d.message) + .clicked() + { + resolve = Some(i); } - } else { - ui.label("Apply & recheck to update the schedule. Export is blocked until these decisions pass validation."); + } + if let Some(r) = &a.rundown { + for g in &r.gaps { + ui.label(format!( + "Gap: {} → {} · {} (no timer)", + g.start_ms.map(display).unwrap_or("?".into()), + g.end_ms.map(display).unwrap_or("?".into()), + g.title + )); + } + } + if let Some(index) = resolve { + let stage = a.diagnostics[index].stage; + let header = a.mapping.as_ref().map_or(1, |m| m.header_row); + self.choose_issue(index); + self.review_view = if stage == 1 && self.selected_row.is_none_or(|r| r <= header) { + ReviewView::Columns + } else { + ReviewView::Rows + }; + } + if let Some(row) = details { + self.selected_row = Some(row); + self.row_details = true; } } } @@ -865,4 +1113,73 @@ mod tests { assert!(app.dirty); } } + #[test] + fn preview_includes_missing_rows_in_order_and_opens_their_issue() { + let mut app = sample(); + let a = app.analysis.as_ref().unwrap(); + let rows = preview_rows(a, Some(&app.tables[0])); + assert_eq!( + rows.iter().map(|r| r.row).collect::>(), + (2..=15).collect::>() + ); + assert_eq!(rows.iter().filter(|r| r.source_only).count(), 12); + assert_eq!(rows[1].title, "Stage direction 0"); + assert_eq!((&*rows[1].start, &*rows[1].duration), ("?", "?")); + assert!(needs_information(&a.diagnostics[rows[1].issues[0]])); + assert_eq!( + a.rundown.as_ref().unwrap().entries.len(), + 2, + "Preview must not invent IR events" + ); + app.review_view = ReviewView::Preview; + let ctx = egui::Context::default(); + configure_style(&ctx); + render(&mut app, &ctx, [860.0, 900.0], vec![]); + let labels = render(&mut app, &ctx, [860.0, 900.0], vec![]); + let resolve = labels + .iter() + .find(|(s, _)| s == "Unresolved · resolve") + .unwrap() + .1 + .center(); + click_size(&mut app, &ctx, resolve, [860.0, 900.0]); + assert!(app.review_view == ReviewView::Rows); + assert_eq!(app.selected_row, Some(3)); + } + + #[test] + fn preview_marks_invalid_timers_and_supports_failures_before_ir() { + let mut app = sample(); + let table = &mut app.tables[0]; + table.rows[1][1].text = "10 min".into(); + let profile = Profile { + untimed_as_notes: true, + ..Default::default() + }; + let a = pipeline::analyze(table, &profile, &AtomicBool::new(false), |_, _| {}); + let rows = preview_rows(&a, Some(table)); + assert_eq!(rows.len(), 2); + assert!(!rows[1].source_only); + assert!( + rows[1] + .issues + .iter() + .any(|i| !needs_information(&a.diagnostics[*i])) + ); + app.analysis = Some(a); + app.review_view = ReviewView::Preview; + let ctx = egui::Context::default(); + configure_style(&ctx); + render(&mut app, &ctx, [860.0, 900.0], vec![]); + let labels = render(&mut app, &ctx, [860.0, 900.0], vec![]); + assert!(labels.iter().any(|(s, _)| s == "Error · resolve")); + app.tables[0].rows[1][0].text = "invalid clock".into(); + let a = pipeline::analyze(&app.tables[0], &profile, &AtomicBool::new(false), |_, _| {}); + assert!(a.rundown.is_none()); + let rows = preview_rows(&a, Some(&app.tables[0])); + assert!(rows[0].source_only); + assert!(rows[0].title.contains("invalid clock")); + assert_eq!(rows[0].start, "?"); + assert!(!needs_information(&a.diagnostics[rows[0].issues[0]])); + } } diff --git a/docs/rundown-converter.md b/docs/rundown-converter.md index e276609..f05ef67 100644 --- a/docs/rundown-converter.md +++ b/docs/rundown-converter.md @@ -41,8 +41,13 @@ click **Analyze rundown**. Review has three focused views: fills the available space and keeps its column headings visible while scrolling. Choose a row meaning or enter a duration with units, such as `2 min`. Click a row number/title or **Full row…** to read all original cells with their headers. -- **Schedule preview** gives the resulting timers and gaps their own view. Draft - and out-of-date schedules are labeled; they cannot be exported. +- **Schedule preview** combines timed entries and unresolved source rows in their + original order. Amber rows need clarification; red rows contain invalid or + inconsistent data. Text status labels accompany the colors. Source-only rows + are explicitly not timers, and unknown values show `?`. Hover an issue for its + full explanation or click its status to resolve it. Click a title for original + cells. Sheet-level and gap issues remain listed beneath the table. Draft and + out-of-date schedules are labeled; they cannot be exported. - **Columns** shows inference evidence, per-column role dropdowns, and sample values. Selecting one duration column explicitly resolves duplicate TIMER headers. **Metadata** keeps a column as Ontime notes without treating its values