Sort presentation and media filenames naturally by default

This commit is contained in:
Dandaedre 2026-11-03 10:46:41 +01:00
parent 637b5eb7e6
commit 06c1493d29
6 changed files with 185 additions and 15 deletions

View File

@ -133,11 +133,13 @@ pub fn probe(
}
}
pub fn assemble(source: &Path, mut videos: Vec<Video>, discovery: &archive::Discovery) -> Report {
videos.sort_by_key(|v| {
(
v.slides.first().copied().unwrap_or(usize::MAX),
v.name.clone(),
)
videos.sort_by(|a, b| {
a.slides
.first()
.copied()
.unwrap_or(usize::MAX)
.cmp(&b.slides.first().copied().unwrap_or(usize::MAX))
.then_with(|| crate::ordering::natural(a.name.as_bytes(), b.name.as_bytes()))
});
let mut playbacks = vec![];
for video in &videos {
@ -162,12 +164,15 @@ pub fn assemble(source: &Path, mut videos: Vec<Video>, discovery: &archive::Disc
}
playbacks.extend(settings.into_iter().map(|s| playback::effective(video, s)));
}
playbacks.sort_by_key(|p| {
(
p.settings.slide.unwrap_or(usize::MAX),
p.video.name.clone(),
p.settings.shape_id.clone(),
)
playbacks.sort_by(|a, b| {
a.settings
.slide
.unwrap_or(usize::MAX)
.cmp(&b.settings.slide.unwrap_or(usize::MAX))
.then_with(|| {
crate::ordering::natural(a.video.name.as_bytes(), b.video.name.as_bytes())
})
.then_with(|| a.settings.shape_id.cmp(&b.settings.shape_id))
});
Report {
groups: vec![],

View File

@ -15,7 +15,7 @@ fn children(path: &Path) -> Result<Vec<PathBuf>> {
let mut paths = fs::read_dir(path)?
.map(|e| e.map(|e| e.path()))
.collect::<std::io::Result<Vec<_>>>()?;
paths.sort_by(|a, b| a.file_name().cmp(&b.file_name()).then_with(|| a.cmp(b)));
paths.sort_by(|a, b| crate::ordering::paths(a, b));
Ok(paths)
}
fn video(path: &Path) -> bool {
@ -58,7 +58,7 @@ pub fn discover(options: &Options) -> Result<Vec<PathBuf>> {
sources.extend(groups);
}
}
sources.sort_by(|a, b| a.file_name().cmp(&b.file_name()).then_with(|| a.cmp(b)));
sources.sort_by(|a, b| crate::ordering::paths(a, b));
let mut seen = std::collections::BTreeSet::new();
sources.retain(|p| seen.insert(fs::canonicalize(p).unwrap_or_else(|_| p.clone())));
Ok(sources)

View File

@ -4,6 +4,7 @@ pub mod engine;
pub mod gui;
pub mod inputs;
pub mod model;
mod ordering;
#[cfg(feature = "pdf")]
pub mod pdf;
pub mod playback;

View File

@ -0,0 +1,64 @@
//! Natural filename ordering without integer size limits or locale dependencies.
use std::{cmp::Ordering, path::Path};
pub(crate) fn natural(left: &[u8], right: &[u8]) -> Ordering {
let (mut a, mut b) = (left, right);
while let (Some(&x), Some(&y)) = (a.first(), b.first()) {
if x.is_ascii_digit() && y.is_ascii_digit() {
let na = a.iter().take_while(|c| c.is_ascii_digit()).count();
let nb = b.iter().take_while(|c| c.is_ascii_digit()).count();
let da = &a[..na][a[..na].iter().take_while(|c| **c == b'0').count()..];
let db = &b[..nb][b[..nb].iter().take_while(|c| **c == b'0').count()..];
let order = da.len().cmp(&db.len()).then_with(|| da.cmp(db));
if order != Ordering::Equal {
return order;
}
a = &a[na..];
b = &b[nb..];
} else {
let order = x.cmp(&y);
if order != Ordering::Equal {
return order;
}
a = &a[1..];
b = &b[1..];
}
}
a.len().cmp(&b.len()).then_with(|| left.cmp(right))
}
pub(crate) fn paths(left: &Path, right: &Path) -> Ordering {
natural(
left.file_name().unwrap_or_default().as_encoded_bytes(),
right.file_name().unwrap_or_default().as_encoded_bytes(),
)
.then_with(|| left.cmp(right))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn numeric_chunks_spaces_padding_and_large_numbers() {
for (a, b) in [
("media2", "media10"),
("media 2", "media 10"),
("part2-media9", "part2-media10"),
("media0002", "media10"),
("media02", "media2"),
("media0", "media00"),
("media99999999999999999999", "media100000000000000000000"),
("Media2", "media2"),
("media2", "media2x"),
] {
assert_eq!(
natural(a.as_bytes(), b.as_bytes()),
Ordering::Less,
"{a} vs {b}"
);
assert_eq!(natural(b.as_bytes(), a.as_bytes()), Ordering::Greater);
assert_eq!(natural(a.as_bytes(), a.as_bytes()), Ordering::Equal);
}
}
}

View File

@ -675,3 +675,100 @@ fn folder_refuses_media_symlinks_and_source_overwrite() {
assert!(engine::run(&options, &AtomicBool::new(false), |_| {}).is_err());
assert_eq!(fs::read(video).unwrap(), b"source");
}
#[cfg(unix)]
#[test]
fn natural_order_applies_to_batch_inputs_folder_videos_and_ontime() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path().join("show");
fs::create_dir(&root).unwrap();
let mut inputs = vec![];
for name in ["talk10.pptx", "talk2.pptx", "talk 10", "talk 2"] {
let path = root.join(name);
if name.ends_with("pptx") {
deck(&path, &[(picture("2", ""), String::new())], true);
} else {
fs::create_dir(&path).unwrap();
for video in ["media10.mp4", "media2.mp4", "media 10.mp4", "media 2.mp4"] {
fs::write(path.join(video), b"video").unwrap();
}
}
inputs.push(path);
}
let mut options = Options {
source: root,
timings_only: true,
ffprobe: Some(fake_probe(dir.path())),
formats: vec!["ontime".into()],
..Default::default()
};
let expected = vec![
inputs[3].clone(),
inputs[2].clone(),
inputs[1].clone(),
inputs[0].clone(),
];
assert_eq!(pptx_reporter::inputs::discover(&options).unwrap(), expected);
let report = engine::run(&options, &AtomicBool::new(false), |_| {})
.unwrap()
.report
.unwrap();
assert_eq!(
report.playbacks[..4]
.iter()
.map(|p| p.video.name.as_str())
.collect::<Vec<_>>(),
vec![
"talk 2 / media 2.mp4",
"talk 2 / media 10.mp4",
"talk 2 / media2.mp4",
"talk 2 / media10.mp4"
]
);
let json = reports::ontime(&report);
let rundown = &json["rundowns"]["default"];
let groups = rundown["order"].as_array().unwrap();
assert_eq!(
rundown["entries"][groups[2].as_str().unwrap()]["title"],
"talk2.pptx"
);
options.source = inputs.remove(0);
options.additional_sources = inputs;
assert_eq!(pptx_reporter::inputs::discover(&options).unwrap(), expected);
}
#[test]
fn natural_media_names_do_not_override_powerpoint_slide_order() {
let videos = [(2, "media1.mp4"), (1, "media10.mp4"), (1, "media2.mp4")]
.into_iter()
.map(|(slide, name)| Video {
name: name.into(),
archive_path: name.into(),
slides: vec![slide],
duration_seconds: Some(1.0),
..Default::default()
})
.collect();
let report = engine::assemble(
Path::new("test.pptx"),
videos,
&archive::Discovery::default(),
);
let expected = vec!["media2.mp4", "media10.mp4", "media1.mp4"];
assert_eq!(
report
.videos
.iter()
.map(|v| v.name.as_str())
.collect::<Vec<_>>(),
expected
);
assert_eq!(
report
.playbacks
.iter()
.map(|p| p.video.name.as_str())
.collect::<Vec<_>>(),
expected
);
}

View File

@ -64,8 +64,11 @@ source names prefix video names in combined reports. JSON includes `groups`
with zero-based `start` (inclusive) and `end` (exclusive) playback indices.
A video folder uses each supported video's full duration as one slide, in
case-sensitive filename order. Use zero-padded names (`01`, `02`, …) to control
order; `10` sorts before `2`. Non-video files are ignored. Supported extensions:
natural filename order: `media2` precedes `media10`, and `media 2` precedes
`media 10`. This default also applies to presentations and subfolders in a batch,
and media names within the same PowerPoint slide. PowerPoint slide order is
preserved. Text remains case-sensitive; equal numeric values use the original
filename as a deterministic tie-breaker (for example, `media02` before `media2`). Non-video files are ignored. Supported extensions:
MP4, M4V, MOV, AVI, WMV, WebM, MPG, MPEG, MKV, ASF, 3GP and TS.
PowerPoint trims still apply to PPTX inputs; plain video files have no PowerPoint
trim or looping metadata. These are video timings, not slide-transition timings.