Remember and reuse column layouts across matching rundown headers
This commit is contained in:
parent
a32a40f416
commit
520144710f
|
|
@ -757,6 +757,45 @@ impl App {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn remember_columns(&mut self) -> anyhow::Result<()> {
|
||||
let directory = self
|
||||
.resolution_dir
|
||||
.as_ref()
|
||||
.ok_or_else(|| anyhow::anyhow!("No configuration directory available"))?;
|
||||
let table = self
|
||||
.tables
|
||||
.iter()
|
||||
.find(|t| t.name == self.sheet)
|
||||
.ok_or_else(|| anyhow::anyhow!("Read and select a sheet first"))?;
|
||||
let profile =
|
||||
resolution::column_profile(table, &serde_json::from_str::<Profile>(&self.profile)?)?;
|
||||
let path = resolution::column_path(directory, table, profile.header_row.unwrap())?;
|
||||
std::fs::create_dir_all(path.parent().unwrap())?;
|
||||
resolution::save(&profile, &path, std::path::Path::new(&self.input), true)?;
|
||||
self.status = "Column layout remembered for sheets with matching headers".into();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn reuse_columns(&mut self) -> anyhow::Result<()> {
|
||||
let directory = self
|
||||
.resolution_dir
|
||||
.as_ref()
|
||||
.ok_or_else(|| anyhow::anyhow!("No configuration directory available"))?;
|
||||
let table = self
|
||||
.tables
|
||||
.iter()
|
||||
.find(|t| t.name == self.sheet)
|
||||
.ok_or_else(|| anyhow::anyhow!("Read and select a sheet first"))?;
|
||||
let (path, row) = resolution::find_columns(directory, table)?
|
||||
.ok_or_else(|| anyhow::anyhow!("No remembered column layout matches these headers"))?;
|
||||
let mut profile = serde_json::from_str::<Profile>(&self.profile)?;
|
||||
resolution::apply_columns(&mut profile, resolution::load(&path)?, row);
|
||||
self.profile = serde_json::to_string_pretty(&profile)?;
|
||||
self.decisions_changed();
|
||||
self.status = "Column layout restored; apply & recheck".into();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn resolution_controls(&mut self, ui: &mut egui::Ui) {
|
||||
ui.label("Remember the decisions for this sheet, then reuse them next time.");
|
||||
let ready = !self.dirty
|
||||
|
|
@ -794,6 +833,71 @@ impl App {
|
|||
"No remembered resolution for this sheet yet."
|
||||
});
|
||||
ui.add_space(8.0);
|
||||
ui.separator();
|
||||
ui.strong("Column layout");
|
||||
ui.small("Reuse roles, duration handling and title options on sheets with the same headers. Row corrections stay separate.");
|
||||
let column_ready = self
|
||||
.tables
|
||||
.iter()
|
||||
.find(|t| t.name == self.sheet)
|
||||
.is_some_and(|table| {
|
||||
serde_json::from_str::<Profile>(&self.profile)
|
||||
.ok()
|
||||
.and_then(|profile| resolution::column_profile(table, &profile).ok())
|
||||
.is_some()
|
||||
});
|
||||
let column_lookup = self.resolution_dir.as_ref().and_then(|directory| {
|
||||
self.tables
|
||||
.iter()
|
||||
.find(|t| t.name == self.sheet)
|
||||
.map(|table| resolution::find_columns(directory, table))
|
||||
});
|
||||
let columns_match = column_lookup
|
||||
.as_ref()
|
||||
.and_then(|result| result.as_ref().ok())
|
||||
.and_then(|found| found.as_ref());
|
||||
if let Some(Err(error)) = &column_lookup {
|
||||
ui.colored_label(Color32::YELLOW, error.to_string());
|
||||
}
|
||||
ui.horizontal_wrapped(|ui| {
|
||||
if ui
|
||||
.add_enabled(
|
||||
column_ready && self.resolution_dir.is_some(),
|
||||
egui::Button::new("Remember column layout"),
|
||||
)
|
||||
.clicked()
|
||||
&& let Err(error) = self.remember_columns()
|
||||
{
|
||||
self.error = Some(error.to_string());
|
||||
}
|
||||
if ui
|
||||
.add_enabled(
|
||||
columns_match.is_some(),
|
||||
egui::Button::new("Reuse remembered columns"),
|
||||
)
|
||||
.clicked()
|
||||
&& let Err(error) = self.reuse_columns()
|
||||
{
|
||||
self.error = Some(error.to_string());
|
||||
}
|
||||
if let Some((path, _)) = &columns_match
|
||||
&& ui.button("Forget column layout").clicked()
|
||||
{
|
||||
match std::fs::remove_file(path) {
|
||||
Ok(()) => self.status = "Remembered column layout removed".into(),
|
||||
Err(error) => self.error = Some(error.to_string()),
|
||||
}
|
||||
}
|
||||
});
|
||||
ui.small(if columns_match.is_some() {
|
||||
"Matching column layout found. Reuse requires Apply & recheck."
|
||||
} else {
|
||||
"No matching column layout remembered yet."
|
||||
});
|
||||
if !column_ready {
|
||||
ui.small("Resolve column issues to remember this layout; schedule errors can be resolved later.");
|
||||
}
|
||||
ui.add_space(8.0);
|
||||
ui.collapsing("Advanced", |ui| {
|
||||
ui.horizontal_wrapped(|ui| {
|
||||
if ui.button("Load resolution…").clicked()
|
||||
|
|
@ -1668,6 +1772,61 @@ mod tests {
|
|||
frame(&mut app, 560.0, 400.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn column_memory_reuses_layout_before_schedule_is_resolved() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let source = dir.path().join("columns.csv");
|
||||
std::fs::write(
|
||||
&source,
|
||||
"Start,Timer,Timer,Title\n09:00,5 min,2 min,Opening\n09:02,5 min,2 min,Overlap\n",
|
||||
)
|
||||
.unwrap();
|
||||
let mut app = App {
|
||||
resolution_dir: Some(dir.path().join("memory")),
|
||||
..App::default()
|
||||
};
|
||||
app.set_input(source.clone());
|
||||
app.inspect();
|
||||
wait(&mut app);
|
||||
assert!(app.remember_columns().is_err());
|
||||
app.profile = r#"{"columns":{"duration":"B"},"duration_mode":"maximum","duration_split":true,"duration_add_margin":true,"duration_margin":"1 min","secondary_title_mode":"merge_previous","titles":{"2":"Edited"},"ontime":{"language":"fr"}}"#.into();
|
||||
app.analyze();
|
||||
wait(&mut app);
|
||||
assert!(!app.analysis.as_ref().unwrap().diagnostics.is_empty());
|
||||
assert!(app.remember().is_err());
|
||||
app.remember_columns().unwrap();
|
||||
// Different file, sheet name, body and header position still reuse the same layout.
|
||||
let other = dir.path().join("other.csv");
|
||||
std::fs::write(&other, "Other show,,,\nStart,Timer,Timer,Title\n10:00,5 min,2 min,New opening\n10:10,5 min,2 min,Next\n").unwrap();
|
||||
app.set_input(other);
|
||||
app.inspect();
|
||||
wait(&mut app);
|
||||
app.profile = r#"{"titles":{"4":"Keep my edit"},"ontime":{"language":"de"}}"#.into();
|
||||
app.reuse_columns().unwrap();
|
||||
assert!(app.dirty);
|
||||
let profile: Profile = serde_json::from_str(&app.profile).unwrap();
|
||||
assert_eq!(profile.header_row, Some(2));
|
||||
assert_eq!(profile.columns["duration"], "B");
|
||||
assert_eq!(profile.columns["start"], "A");
|
||||
assert_eq!(profile.duration_mode.as_deref(), Some("maximum"));
|
||||
assert!(profile.duration_split && profile.duration_add_margin);
|
||||
assert_eq!(profile.duration_margin.as_deref(), Some("1 min"));
|
||||
assert_eq!(
|
||||
profile.secondary_title_mode.as_deref(),
|
||||
Some("merge_previous")
|
||||
);
|
||||
assert_eq!(profile.titles.len(), 1);
|
||||
assert_eq!(profile.titles[&4], "Keep my edit");
|
||||
assert_eq!(profile.ontime.language, "de");
|
||||
app.analyze();
|
||||
wait(&mut app);
|
||||
assert!(
|
||||
app.analysis.as_ref().unwrap().diagnostics.is_empty(),
|
||||
"{:?}",
|
||||
app.analysis
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolve_save_reuse_and_revalidate_ambiguous_rundown() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
//! Portable clarification profiles and exact-sheet remembered resolutions.
|
||||
use crate::model::{Profile, Table};
|
||||
use crate::{
|
||||
detect,
|
||||
model::{Profile, Table, column_name},
|
||||
};
|
||||
use anyhow::{Result, ensure};
|
||||
use std::{
|
||||
io::Write,
|
||||
|
|
@ -18,6 +21,110 @@ pub fn remembered_path(directory: &Path, table: &Table) -> Result<PathBuf> {
|
|||
Ok(directory.join(format!("{id}.json")))
|
||||
}
|
||||
|
||||
/// Match header text and order, independent of event content and sheet name.
|
||||
pub fn column_path(directory: &Path, table: &Table, header_row: usize) -> Result<PathBuf> {
|
||||
let row = table
|
||||
.rows
|
||||
.get(
|
||||
header_row
|
||||
.checked_sub(1)
|
||||
.ok_or_else(|| anyhow::anyhow!("Invalid header row"))?,
|
||||
)
|
||||
.ok_or_else(|| anyhow::anyhow!("Header row is outside the sheet"))?;
|
||||
let mut headers: Vec<_> = row
|
||||
.iter()
|
||||
.map(|cell| {
|
||||
cell.text
|
||||
.split_whitespace()
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ")
|
||||
.to_lowercase()
|
||||
})
|
||||
.collect();
|
||||
while headers.last().is_some_and(String::is_empty) {
|
||||
headers.pop();
|
||||
}
|
||||
ensure!(
|
||||
headers.iter().filter(|h| !h.is_empty()).count() >= 2,
|
||||
"At least two named headers are needed to remember a column layout"
|
||||
);
|
||||
let id = uuid::Uuid::new_v5(&uuid::Uuid::NAMESPACE_OID, &serde_json::to_vec(&headers)?);
|
||||
Ok(directory.join("columns").join(format!("{id}.json")))
|
||||
}
|
||||
|
||||
/// Resolve columns without requiring a consistent complete schedule.
|
||||
pub fn column_profile(table: &Table, profile: &Profile) -> Result<Profile> {
|
||||
let mapping = detect::infer(table, profile)
|
||||
.map_err(|_| anyhow::anyhow!("Resolve column mapping issues before remembering columns"))?;
|
||||
let mut saved = Profile {
|
||||
header_row: Some(mapping.header_row),
|
||||
..Profile::default()
|
||||
};
|
||||
saved.columns.insert(
|
||||
if mapping.start_end {
|
||||
"start_end"
|
||||
} else {
|
||||
"start"
|
||||
}
|
||||
.into(),
|
||||
column_name(mapping.start),
|
||||
);
|
||||
saved
|
||||
.columns
|
||||
.insert("title".into(), column_name(mapping.title));
|
||||
for (role, column) in [
|
||||
("end", mapping.end),
|
||||
("duration", mapping.duration),
|
||||
("secondary_title", mapping.secondary_title),
|
||||
] {
|
||||
if let Some(column) = column {
|
||||
saved.columns.insert(role.into(), column_name(column));
|
||||
}
|
||||
}
|
||||
saved.column_usage = mapping.column_usage;
|
||||
saved.expected_headers = mapping.column_labels;
|
||||
saved.secondary_title_mode = mapping.secondary_title_mode;
|
||||
saved.duration_unit = mapping.duration_unit;
|
||||
saved.duration_mode = mapping.duration_mode;
|
||||
saved.duration_separator = mapping.duration_separator;
|
||||
saved.duration_split = mapping.duration_split;
|
||||
saved.duration_add_margin = mapping.duration_add_margin;
|
||||
saved.duration_margin = mapping.duration_margin;
|
||||
Ok(saved)
|
||||
}
|
||||
|
||||
pub fn find_columns(directory: &Path, table: &Table) -> Result<Option<(PathBuf, usize)>> {
|
||||
let mut found = None;
|
||||
for row in 1..=table.rows.len() {
|
||||
if let Ok(path) = column_path(directory, table, row)
|
||||
&& path.is_file()
|
||||
{
|
||||
ensure!(
|
||||
found.is_none(),
|
||||
"Several header rows match remembered layouts; select columns manually"
|
||||
);
|
||||
found = Some((path, row));
|
||||
}
|
||||
}
|
||||
Ok(found)
|
||||
}
|
||||
|
||||
/// Apply only column decisions; never transfer row edits or project preferences.
|
||||
pub fn apply_columns(current: &mut Profile, saved: Profile, header_row: usize) {
|
||||
current.header_row = Some(header_row);
|
||||
current.columns = saved.columns;
|
||||
current.column_usage = saved.column_usage;
|
||||
// Header signature already checked; expected_headers would reject harmless casing changes.
|
||||
current.expected_headers.clear();
|
||||
current.secondary_title_mode = saved.secondary_title_mode;
|
||||
current.duration_unit = saved.duration_unit;
|
||||
current.duration_mode = saved.duration_mode;
|
||||
current.duration_separator = saved.duration_separator;
|
||||
current.duration_split = saved.duration_split;
|
||||
current.duration_add_margin = saved.duration_add_margin;
|
||||
current.duration_margin = saved.duration_margin;
|
||||
}
|
||||
|
||||
pub fn load(path: &Path) -> Result<Profile> {
|
||||
Ok(serde_json::from_slice(&std::fs::read(path)?)?)
|
||||
}
|
||||
|
|
@ -54,6 +161,41 @@ pub fn save(profile: &Profile, path: &Path, source: &Path, overwrite: bool) -> R
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn column_matching_checks_order_and_handles_ambiguous_headers() {
|
||||
use crate::model::Cell;
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let mut table = Table {
|
||||
name: "One".into(),
|
||||
rows: vec![
|
||||
vec!["Start", "Duration", "Title"]
|
||||
.into_iter()
|
||||
.map(|s| Cell {
|
||||
text: s.into(),
|
||||
..Cell::default()
|
||||
})
|
||||
.collect(),
|
||||
],
|
||||
merges: vec![],
|
||||
};
|
||||
let path = column_path(dir.path(), &table, 1).unwrap();
|
||||
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
|
||||
std::fs::write(&path, "{}").unwrap();
|
||||
assert_eq!(
|
||||
find_columns(dir.path(), &table).unwrap(),
|
||||
Some((path.clone(), 1))
|
||||
);
|
||||
table.rows[0][0].text = " START ".into();
|
||||
assert_eq!(column_path(dir.path(), &table, 1).unwrap(), path);
|
||||
table.rows[0].swap(0, 1);
|
||||
assert!(find_columns(dir.path(), &table).unwrap().is_none());
|
||||
table.rows[0].swap(0, 1);
|
||||
table.rows.push(table.rows[0].clone());
|
||||
assert!(find_columns(dir.path(), &table).is_err());
|
||||
assert!(column_path(dir.path(), &table, 0).is_err());
|
||||
assert!(column_path(dir.path(), &table, 3).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn profiles_roundtrip_and_remember_only_identical_sheets() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
|
|
|
|||
|
|
@ -458,6 +458,22 @@ explicit reuse rather than automatic application. Changing file or sheet clears
|
|||
current decisions. Remembered files contain profile decisions and a hash in their
|
||||
filename, not a copy of the workbook; they remain local to this machine.
|
||||
|
||||
### Remember column layouts
|
||||
|
||||
In **Save resolutions…**, **Remember column layout** saves the resolved column
|
||||
roles, Notes/Metadata/Ignore choices, duration units/handling/split/margin settings,
|
||||
and secondary-title handling. It is available once columns are valid, even if
|
||||
there are schedule errors or pending row corrections.
|
||||
|
||||
**Reuse remembered columns** matches header text and order, ignoring casing and
|
||||
extra whitespace. Event content, sheet names and header row position may differ.
|
||||
It restores only column decisions: existing row edits and Ontime preferences are
|
||||
preserved, and row corrections from another rundown are never copied. Apply &
|
||||
recheck before export; the timing data is still validated across the whole column.
|
||||
Ambiguous matching header rows are not applied. **Forget column layout** removes
|
||||
this separate memory, stored under `resolutions/columns` in the same user
|
||||
configuration directory. Exact-sheet resolutions continue to work as before.
|
||||
|
||||
## Current limits
|
||||
|
||||
One selected sheet represents one sequential, single-day schedule. Parallel
|
||||
|
|
|
|||
Loading…
Reference in New Issue