use std::borrow::Cow;
use std::collections::{BTreeMap, HashMap};
use std::io;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicU32, Ordering};
use std::time::{Duration, Instant};
use anstyle::{AnsiColor, Style};
use choon::grid::Evidence;
use choon::{OpusUse, Reading, Verdict};
use comfy_table::{CellAlignment, ContentArrangement, LineStyle, Table, TableStyle};
use console::{Alignment, Term};
use indicatif::{MultiProgress, ProgressBar, ProgressDrawTarget, ProgressStyle};
use crate::{
Checked, NOT_READ, detailed, encoder, flag_words, flagged, khz, label, piped, piped_unread,
};
const VERDICTS: [Verdict; 5] = [
Verdict::Consistent,
Verdict::LikelyTranscode,
Verdict::MixedSources,
Verdict::PossiblyLossy,
Verdict::Inconclusive,
];
pub struct Row<'a> {
pub path: PathBuf,
pub root: Option<&'a Path>,
pub outcome: Result<Box<Checked>, String>,
}
#[derive(Clone, Copy)]
pub struct Look {
pub profiled: bool,
pub verbose: bool,
pub colour: bool,
pub width: u16,
}
#[derive(Clone, Copy, PartialEq)]
pub enum Counted {
Flagged(Verdict),
NotRead,
}
const COUNTED: [Counted; 4] = [
Counted::Flagged(Verdict::LikelyTranscode),
Counted::Flagged(Verdict::MixedSources),
Counted::Flagged(Verdict::PossiblyLossy),
Counted::NotRead,
];
pub fn worded(verdict: Verdict, colour: bool) -> String {
painted(Some(verdict), label(verdict), colour)
}
pub fn painted(verdict: Option<Verdict>, text: &str, colour: bool) -> String {
let tint = match verdict {
Some(Verdict::LikelyTranscode | Verdict::MixedSources) => Some(AnsiColor::Red),
Some(Verdict::PossiblyLossy) => Some(AnsiColor::Yellow),
Some(Verdict::Consistent) => Some(AnsiColor::Green),
Some(Verdict::Inconclusive) => None,
None => Some(AnsiColor::Magenta),
};
match tint.filter(|_| colour) {
Some(tint) => {
let style = Style::new().fg_color(Some(tint.into()));
format!("{style}{text}{style:#}")
}
None => text.to_string(),
}
}
pub fn legible(path: &Path) -> String {
let mut text = String::new();
for character in path.to_string_lossy().chars() {
if character.is_control() {
text.extend(character.escape_default());
} else {
text.push(character);
}
}
text
}
struct Flying {
file: usize,
name: String,
progress: Vec<Arc<AtomicU32>>,
bar: Option<ProgressBar>,
}
pub struct Bars {
multi: MultiProgress,
term: Term,
overall: Option<ProgressBar>,
total: Option<usize>,
done: usize,
counts: [usize; COUNTED.len()],
started: Instant,
more: Option<ProgressBar>,
flying: Vec<Flying>,
}
const BAR: usize = 20;
const MIN_BAR: usize = 13;
const BESIDE_NAME: usize = BAR + 8;
fn style(template: &str) -> ProgressStyle {
ProgressStyle::with_template(template)
.expect("the template is well formed")
.progress_chars("=> ")
}
impl Bars {
pub fn new(total: Option<usize>) -> Self {
let multi = MultiProgress::with_draw_target(ProgressDrawTarget::stdout());
let overall = (total != Some(1)).then(|| {
let overall = match total {
Some(total) => ProgressBar::new(total as u64).with_style(style("[{wide_bar}] {msg}")),
None => ProgressBar::no_length().with_style(style("{msg}")),
};
multi.add(overall)
});
let bars = Self {
multi,
term: Term::stdout(),
overall,
total,
done: 0,
counts: [0; COUNTED.len()],
started: Instant::now(),
more: None,
flying: Vec::new(),
};
bars.count();
bars
}
pub fn started(&mut self, file: usize, name: String, progress: Vec<Arc<AtomicU32>>) {
self.flying.push(Flying {
file,
name,
progress,
bar: None,
});
}
pub fn ended(&mut self, file: usize, counted: Option<Counted>) {
if let Some(at) = self.flying.iter().position(|flying| flying.file == file)
&& let Some(bar) = self.flying.remove(at).bar
{
remove(&self.multi, &bar);
}
self.done += 1;
if let Some(at) = COUNTED.iter().position(|kind| Some(*kind) == counted) {
self.counts[at] += 1;
}
self.count();
}
fn count(&self) {
let Some(overall) = &self.overall else {
return;
};
let mut said = match self.total {
Some(total) => format!("{} of {total}", self.done),
None => format!("{} done", self.done),
};
for (kind, count) in COUNTED.iter().zip(self.counts) {
let words = match kind {
Counted::Flagged(verdict) => label(*verdict),
Counted::NotRead => NOT_READ,
};
if count > 0 {
said += &format!(", {count} {words}");
}
}
if let Some(total) = self.total.filter(|_| self.done > 0) {
let per_second = self.done as f64 / self.started.elapsed().as_secs_f64();
let left = Duration::from_secs_f64((total - self.done) as f64 / per_second);
let timed = format!("{said}, ETA {}", length(left));
if timed.len() + MIN_BAR <= usize::from(self.term.size().1) {
said = timed;
}
}
overall.set_position(self.done as u64);
overall.set_message(said);
}
pub fn tick(&mut self) -> io::Result<()> {
let Self {
multi,
term,
overall,
more,
flying,
..
} = self;
let (multi, overall) = (&*multi, &*overall);
let add = |bar: ProgressBar| match overall {
Some(overall) => multi.insert_before(overall, bar),
None => multi.add(bar),
};
let (rows, columns) = term.size();
let room = room(rows, overall.is_some(), flying.len());
let without = flying.len().saturating_sub(room);
match (&*more, without) {
(Some(counted), 0) => {
remove(multi, counted);
*more = None;
}
(Some(counted), _) => counted.set_message(format!("+{without} more in flight")),
(None, 0) => {}
(None, _) => {
let counted = add(
ProgressBar::no_length()
.with_style(style("{msg}"))
.with_message(format!("+{without} more in flight")),
);
counted.tick();
*more = Some(counted);
}
}
let name_width = usize::from(columns)
.saturating_sub(BESIDE_NAME)
.clamp(8, 48);
for (at, flying) in flying.iter_mut().enumerate() {
if at >= room {
if let Some(bar) = flying.bar.take() {
remove(multi, &bar);
}
continue;
}
let bar = flying.bar.get_or_insert_with(|| {
let bar = ProgressBar::new(1000).with_style(style(&format!(
"{{prefix}} [{{bar:{BAR}}}] {{percent:>3}}%"
)));
match &*more {
Some(counted) => multi.insert_before(counted, bar),
None => add(bar),
}
});
let name = console::truncate_str(&flying.name, name_width, "...");
bar.set_prefix(console::pad_str(&name, name_width, Alignment::Left, None).into_owned());
let measured: u64 = flying
.progress
.iter()
.map(|measuring| u64::from(measuring.load(Ordering::Relaxed)))
.sum();
bar.set_position(measured / flying.progress.len().max(1) as u64);
}
term.write_str("\r")
}
pub fn complain(&self, line: &str) {
self.multi.suspend(|| eprintln!("{line}"));
}
pub fn finish(self) {
let bars = self.flying.into_iter().filter_map(|flying| flying.bar);
for bar in bars.chain(self.more).chain(self.overall) {
remove(&self.multi, &bar);
}
}
}
fn remove(multi: &MultiProgress, bar: &ProgressBar) {
bar.finish_and_clear();
multi.remove(bar);
}
fn room(rows: u16, overall: bool, flying: usize) -> usize {
let lines = usize::from(rows / 2)
.saturating_sub(usize::from(overall))
.max(1);
if flying > lines {
lines.saturating_sub(1).max(1)
} else {
lines
}
}
fn table(width: u16) -> Table {
let mut table = Table::new();
table
.load_style(TableStyle::new().header_separator(LineStyle::none().fill('-').junction(' ')))
.set_content_arrangement(ContentArrangement::Dynamic)
.set_width(width);
table
}
fn lines(table: &mut Table, indent: u16) -> String {
for (at, column) in table.column_iter_mut().enumerate() {
column.set_padding((if at == 0 { indent } else { 0 }, 2));
}
table.lines().fold(String::new(), |mut text, line| {
text += line.trim_end();
text += "\n";
text
})
}
fn length(time: Duration) -> String {
match time.as_secs() {
0..10 => format!("{:.1} s", time.as_secs_f64()),
seconds @ 10..60 => format!("{seconds} s"),
seconds => format!("{} min {} s", seconds / 60, seconds % 60),
}
}
pub fn readout(path: &Path, checked: &Checked, look: Look) -> String {
let claimed = &checked.claimed;
let mut format = vec![claimed.codec.clone(), format!("{} Hz", claimed.sample_rate)];
if let Some(bits) = claimed.bit_depth {
format.push(format!("{bits} bit"));
}
format.push(match claimed.channels {
1 => "1 channel".to_string(),
channels => format!("{channels} channels"),
});
if let Some(kbps) = checked.bitrate_kbps {
format.push(format!("{kbps} kbit/s"));
}
let seconds = checked.duration.round() as u64;
format.push(format!("{}:{:02}", seconds / 60, seconds % 60));
let format = format.join(", ");
let mut text = String::new();
for read in &checked.readings {
let reading = &read.reading;
text += &legible(path);
if look.profiled {
text += &format!(" [{}]", read.profile);
}
text += "\n";
let mut table = table(look.width);
let mut add = |label: &str, value: String| {
table.add_row([label, &value]);
};
add("Format", format.clone());
if let Some(coding) = OpusUse::describe(&checked.opus) {
add("Opus packets", coding);
}
if let Some((rate, multiple)) = checked.dsd_rate.zip(checked.dsd_multiple) {
add(
"DSD",
format!("DSD{multiple}, one-bit samples at {rate} Hz"),
);
}
if let Some(he_aac) = checked.he_aac {
add(
"AAC profile",
format!("{he_aac}, core at {} Hz", he_aac.core_rate),
);
}
add("Verdict", worded(reading.verdict, look.colour));
add("Grid", grid(checked, reading));
add(
"Cutoff",
reading
.cutoff_hz
.map_or_else(|| "none found".to_string(), khz),
);
if let Some(encoder) = encoder(reading) {
add("Encoder", encoder);
}
if reading.verdict != Verdict::Inconclusive {
add("Confidence", format!("{:.2}", reading.confidence));
}
if !reading.flags.is_empty() {
let flags: Vec<&str> = reading.flags.iter().copied().map(flag_words).collect();
add("Flags", flags.join(", "));
}
if look.verbose {
add("Summary", reading.summary.clone());
}
if !reading.limitations.is_empty() {
let limitations: Vec<String> = reading
.limitations
.iter()
.map(ToString::to_string)
.collect();
add("Limitations", limitations.join("\n"));
}
text += &lines(&mut table, 2);
text += "\n";
}
text
}
fn grid(checked: &Checked, reading: &Reading) -> String {
match (&reading.grid, &checked.grid) {
(Some(found), _) => match &found.evidence {
Evidence::Count(scan) => {
let quantizer = found.quantizer.as_ref().map_or_else(String::new, |steps| {
format!(", {} bands on whole quantizer steps", steps.whole)
});
format!(
"{} at offset {}, z {:.0}, {} of {} stretches agree{quantizer}",
found.codec,
scan.offset,
scan.z,
scan.segments_agreeing,
scan.segment_z.len()
)
}
Evidence::Part { segments, scan } => format!(
"{} at offset {}, z {:.0}, in {segments} of {} stretches",
found.codec,
scan.offset,
scan.z,
scan.segment_z.len()
),
Evidence::CountAt { rate, scan } => format!(
"{} at offset {} once at {} kHz, z {:.0}, {} of {} stretches agree",
found.codec,
scan.offset,
f64::from(*rate) / 1000.0,
scan.z,
scan.segments_agreeing,
scan.segment_z.len()
),
Evidence::Steps { offset, steps } => format!(
"{} at offset {offset}, {} of {} bands on whole quantizer steps",
found.codec, steps.whole, steps.tested
),
Evidence::AacSteps { offset, steps } => format!(
"{} at offset {offset}, {} of {} bands on whole quantizer steps",
found.codec, steps.whole, steps.tested
),
Evidence::Lattice(scan) => format!(
"{} at offset {}, {} frames on its energy lattice in the stretches, {} in the windows",
found.codec, scan.offset, scan.frames, scan.uncut_frames
),
Evidence::Shifted { shifted } => format!(
"{} at offset {}, {} frames on its energy lattice at another level in the stretches, \
{} in the windows",
found.codec, shifted.offset, shifted.frames, shifted.uncut_frames
),
Evidence::Unfiltered { unfiltered } => format!(
"{} at offset {}, {} of {} frames on its energy lattice under its pitch filter in the \
windows",
found.codec,
found.evidence.offset(),
unfiltered.frames,
unfiltered.searched
),
Evidence::Hybrid { hybrid } => format!(
"{} at offset {}, {} of {} hybrid frames on its energy lattice in the stretches, {} of \
{} in the windows",
found.codec,
hybrid.offset,
hybrid.frames,
hybrid.tested,
hybrid.uncut_frames,
hybrid.uncut_tested
),
Evidence::Short { short } => format!(
"{} at offset {}, {} frames of {} ms on its energy lattice in the stretches, {} in the \
windows",
found.codec,
short.offset,
short.frames,
short.milliseconds(),
short.uncut_frames
),
},
(None, Some(scan)) => {
let opus = scan.opus.as_ref().map_or_else(String::new, |opus| {
format!(
", Opus frames {} in the stretches, {} in the windows",
opus.frames, opus.uncut_frames
)
});
let aac = scan
.aac_sine
.as_ref()
.map_or(scan.aac.z, |sine| sine.z.max(scan.aac.z));
format!(
"none over the thresholds (MP3 z {:.1}, AAC z {aac:.1}{opus})",
scan.mp3.z
)
}
(None, None) => "not scanned".to_string(),
}
}
const TABLE_MIN_WIDTH: u16 = 60;
const NAME_MIN: usize = 12;
const INDENT: usize = 2;
const GAP: usize = 2;
pub fn tables(
rows: &[Row<'_>],
look: Look,
took: Duration,
measured: bool,
left: Option<&str>,
) -> String {
let Look {
profiled,
verbose,
colour,
width,
} = look;
let read: Vec<(&Path, &Checked)> = rows
.iter()
.filter_map(|row| Some((&*row.path, &**row.outcome.as_ref().ok()?)))
.collect();
let not_read: Vec<(&Path, &str)> = rows
.iter()
.filter_map(|row| Some((&*row.path, row.outcome.as_ref().err()?.as_str())))
.collect();
let mut sections = Vec::new();
if width < TABLE_MIN_WIDTH {
let mut lines = String::new();
for row in rows {
let path = legible(&row.path);
match &row.outcome {
Ok(checked) if verbose || checked.flag().is_some() => {
lines += &piped(path, checked, colour);
}
Ok(_) => continue,
Err(why) => lines += &piped_unread(path, why, colour),
}
lines += "\n";
}
sections.push(lines);
} else {
sections.push(listing(rows, look));
let mut unread = String::new();
if !not_read.is_empty() {
unread += "Not read\n--------\n";
for (path, why) in ¬_read {
unread += &format!("{}: {why}\n", legible(path));
}
}
sections.push(unread);
}
let mut detail = String::new();
if verbose {
let flagged = read.iter().filter(|(_, checked)| checked.flag().is_some());
for (path, checked) in flagged {
detail += &readout(path, checked, look);
}
}
sections.push(detail.trim_end().to_string() + "\n");
let profiles: Vec<&str> = read.first().map_or_else(Vec::new, |(_, checked)| {
checked
.readings
.iter()
.map(|read| read.profile.as_str())
.collect()
});
if !profiles.is_empty() {
let mut counts = table(width);
let mut header = vec!["Verdict"];
if profiled {
header.extend(&profiles);
} else {
header.push("Files");
}
counts.set_header(header);
for verdict in VERDICTS {
let mut row = vec![worded(verdict, colour)];
row.extend((0..profiles.len()).map(|profile| {
read
.iter()
.filter(|(_, checked)| checked.readings[profile].reading.verdict == verdict)
.count()
.to_string()
}));
counts.add_row(row);
}
right_align(&mut counts, 1);
sections.push(lines(&mut counts, 0));
}
let mut totals = table(width);
totals.add_row(["Files read", &read.len().to_string()]);
totals.add_row(["Files not read", ¬_read.len().to_string()]);
totals.add_row(["Wall time", &length(took)]);
if measured {
totals.add_row([
"Files per minute",
&format!("{:.0}", rows.len() as f64 / took.as_secs_f64() * 60.0),
]);
}
right_align(&mut totals, 1);
sections.push(lines(&mut totals, 0));
if let Some(left) = left {
sections.push(format!("Interrupted {left}\n"));
}
sections.retain(|section| !section.trim().is_empty());
sections.join("\n")
}
type Listed<'a> = (&'a Row<'a>, String);
enum Entry {
Heading(String),
Alike(String, String),
File(Vec<String>),
}
fn listing(rows: &[Row<'_>], look: Look) -> String {
let Look {
profiled,
verbose,
colour,
..
} = look;
let width = usize::from(look.width);
let none = Path::new("");
let mut named: HashMap<&Path, usize> = HashMap::new();
for row in rows.iter().filter(|row| row.root.is_none()) {
*named.entry(row.path.parent().unwrap_or(none)).or_default() += 1;
}
let name = |path: &Path| legible(Path::new(path.file_name().unwrap_or(path.as_os_str())));
let mut groups: BTreeMap<(Option<&Path>, &Path), Vec<Listed<'_>>> = BTreeMap::new();
for row in rows {
let (directory, name) = match row.root {
Some(root) => {
let within = row.path.strip_prefix(root).unwrap_or(&row.path);
(within.parent().unwrap_or(none), name(within))
}
None => match row.path.parent() {
Some(parent) if parent != none && named[parent] > 1 => (parent, name(&row.path)),
_ => (none, legible(&row.path)),
},
};
groups
.entry((row.root, directory))
.or_default()
.push((row, name));
}
let mut entries = Vec::new();
let mut under = None;
for ((root, directory), files) in &groups {
let heading = match (root, *directory == none) {
(None, true) => None,
(Some(root), true) => Some(heading(root)),
(_, false) => Some(heading(directory)),
};
let alike = heading
.as_ref()
.filter(|_| !verbose && !profiled)
.and_then(|_| alike(files, colour));
let lines: Vec<Entry> = files
.iter()
.flat_map(|(row, name)| cells(row, name, look))
.map(Entry::File)
.collect();
if alike.is_none() && lines.is_empty() {
continue;
}
if let Some(root) = root
&& under.replace(*root) != Some(*root)
&& *directory != none
{
entries.push(Entry::Heading(self::heading(root)));
}
match (heading, alike) {
(Some(heading), Some(alike)) => entries.push(Entry::Alike(heading, alike)),
(heading, _) => {
entries.extend(heading.map(Entry::Heading));
entries.extend(lines);
}
}
}
let mut header = vec!["Verdict"];
if verbose {
header.push("Grid");
}
header.extend(["Cutoff", "Encoder"]);
if profiled {
header.push("Profile");
}
let (file, columns) = ("File", header.len());
let mut widths: Vec<usize> = header.iter().map(|title| title.len()).collect();
let mut longest = file.len();
let mut listed = false;
for entry in &entries {
if let Entry::File(cells) = entry {
listed = true;
for (width, cell) in widths.iter_mut().zip(cells) {
*width = (*width).max(console::measure_text_width(cell));
}
longest = longest.max(console::measure_text_width(&cells[columns]));
}
}
let before: usize = INDENT + widths.iter().map(|width| width + GAP).sum::<usize>();
let name_width = longest.min(width.saturating_sub(before).max(NAME_MIN));
let line = |cells: &mut dyn Iterator<Item = &str>, name: &str| {
let mut line = " ".repeat(INDENT);
for (cell, width) in cells.zip(&widths) {
line += &console::pad_str(cell, width + GAP, Alignment::Left, None);
}
line + &cut(name, name_width) + "\n"
};
let mut text = String::new();
if listed {
text += &line(&mut header.iter().copied(), file);
let rules: Vec<String> = widths.iter().map(|width| "-".repeat(*width)).collect();
text += &line(
&mut rules.iter().map(String::as_str),
&"-".repeat(name_width),
);
}
for entry in &entries {
match entry {
Entry::Heading(directory) => {
text += &cut(directory, width);
text += "\n";
}
Entry::Alike(directory, alike) => {
let room = width.saturating_sub(GAP + console::measure_text_width(alike));
text += &format!("{} {alike}\n", cut(directory, room.max(NAME_MIN)));
}
Entry::File(cells) => {
text += &line(
&mut cells[..columns].iter().map(String::as_str),
&cells[columns],
);
}
}
}
text
}
fn heading(directory: &Path) -> String {
let mut heading = legible(directory);
if !heading.ends_with(['/', std::path::MAIN_SEPARATOR]) {
heading.push(std::path::MAIN_SEPARATOR);
}
heading
}
fn cells(row: &Row<'_>, name: &str, look: Look) -> Vec<Vec<String>> {
let Ok(checked) = &row.outcome else {
return Vec::new();
};
let readings = checked.readings.iter();
readings
.filter(|read| look.verbose || flagged(read.reading.verdict))
.map(|read| {
let reading = &read.reading;
let grid = reading.grid.as_ref();
let mut cells = vec![worded(reading.verdict, look.colour)];
if look.verbose {
cells.push(grid.map_or_else(String::new, |found| {
format!("{}, offset {}", found.codec, found.evidence.offset())
}));
}
cells.push(reading.cutoff_hz.map_or_else(String::new, khz));
cells.push(match (encoder(reading), grid) {
(Some(encoder), _) => encoder,
(None, Some(found)) if !look.verbose => found.codec.to_string(),
(None, _) => String::new(),
});
if look.profiled {
cells.push(read.profile.clone());
}
cells.push(name.to_string());
cells
})
.collect()
}
fn alike(files: &[Listed<'_>], colour: bool) -> Option<String> {
let readings: Vec<&Reading> = files
.iter()
.map(|(row, _)| match &row.outcome.as_ref().ok()?.readings[..] {
[read] => Some(&read.reading),
_ => None,
})
.collect::<Option<_>>()?;
let [first, rest @ ..] = &readings[..] else {
return None;
};
let read_as = |reading: &Reading| {
let codec = reading.grid.as_ref().map(|found| found.codec);
(reading.verdict, codec, encoder(reading))
};
if rest.is_empty()
|| !flagged(first.verdict)
|| rest.iter().any(|other| read_as(other) != read_as(first))
{
return None;
}
let mut found = Vec::new();
if let Some(grid) = &first.grid {
found.push(format!("{} grid", grid.codec));
}
let cutoffs = readings.iter().filter_map(|reading| reading.cutoff_hz);
let range = cutoffs.fold(None, |range: Option<(f64, f64)>, hz| {
let (low, high) = range.unwrap_or((hz, hz));
Some((low.min(hz), high.max(hz)))
});
if let Some((low, high)) = range {
let (low, high) = (khz(low), khz(high));
found.push(match low.strip_suffix(" kHz") {
Some(low_khz) if low != high => format!("cutoff {low_khz} to {high}"),
_ => format!("cutoff {high}"),
});
}
found.extend(encoder(first));
let count = readings.len();
let verdict = worded(first.verdict, colour);
Some(detailed(format!("{count} of {count} {verdict}"), &found))
}
fn cut(text: &str, width: usize) -> Cow<'_, str> {
if console::measure_text_width(text) <= width {
return Cow::Borrowed(text);
}
let kept = width.saturating_sub(3);
let columns = |character: char| console::measure_text_width(character.encode_utf8(&mut [0; 4]));
let fitting = |characters: &mut dyn Iterator<Item = (usize, char)>, mut room: usize| {
let mut last = None;
for (at, character) in characters {
let Some(left) = room.checked_sub(columns(character)) else {
break;
};
(room, last) = (left, Some((at, character)));
}
last
};
let head = fitting(&mut text.char_indices(), kept.div_ceil(2))
.map_or(0, |(at, character)| at + character.len_utf8());
let tail = fitting(&mut text.char_indices().rev(), kept / 2).map_or(text.len(), |(at, _)| at);
Cow::Owned(format!("{}...{}", &text[..head], &text[tail.max(head)..]))
}
fn right_align(table: &mut Table, first: usize) {
for column in table.column_iter_mut().skip(first) {
column.set_cell_alignment(CellAlignment::Right);
}
}
#[cfg(test)]
mod tests {
use std::path::Path;
use std::sync::Arc;
use std::time::Duration;
use choon::fixtures::{self, Bitrate, music, pink};
use choon::grid::{Codec, Family};
use choon::{Coverage, Verdict};
use super::{Look, Row, cut, legible, readout, room, tables};
use crate::{Checked, Plan, Thresholds, analyzed, encoder, khz};
fn honest_and_fake() -> (Box<Checked>, Box<Checked>) {
let dir = tempfile::tempdir().unwrap();
let honest = dir.path().join("honest.flac");
fixtures::write_flac(&music(1, 44_100, pink), 16, false, &honest);
let fake = dir.path().join("fake.flac");
let through = fixtures::through_mp3(&music(1, 44_100, pink), Bitrate::Kbps128, dir.path());
fixtures::write_flac(&through, 16, false, &fake);
let profiles = [("default".to_string(), Thresholds::default())];
let plan = Plan::new(&profiles, Coverage::Full, None, false, Arc::new(|| false));
let checked = |path: &Path| {
let (checked, _) = analyzed(path, &plan, &[Arc::default()]).unwrap();
Box::new(checked)
};
(checked(&honest), checked(&fake))
}
fn look(verbose: bool, width: u16) -> Look {
Look {
profiled: false,
verbose,
colour: false,
width,
}
}
fn uncoloured(text: &str) -> String {
let mut plain = String::new();
let mut rest = text;
while let Some(at) = rest.find("\u{1b}[") {
plain += &rest[..at];
let code = &rest[at..];
rest = &code[code.find('m').unwrap() + 1..];
}
plain + rest
}
#[test]
fn an_encoder_is_the_grids_codec_its_family_and_the_cutoffs_setting() {
let (_, fake) = honest_and_fake();
let reading = &fake.readings[0].reading;
let named = |codec, family| {
let mut reading = reading.clone();
let grid = reading.grid.as_mut().expect("the fake carries a grid");
(grid.codec, grid.family) = (codec, family);
encoder(&reading)
};
let mp3 = |family| named(Codec::Mp3, family);
assert_eq!(mp3(None).as_deref(), Some("MP3 ~128 kbps"));
assert_eq!(
mp3(Some(Family::Untagged)).as_deref(),
Some("MP3 ~128 kbps")
);
assert_eq!(
mp3(Some(Family::Lame)).as_deref(),
Some("MP3 (LAME) ~128 kbps")
);
assert_eq!(mp3(Some(Family::Helix)).as_deref(), Some("MP3 (Helix)"));
assert_eq!(
named(Codec::Aac, Some(Family::Apple)).as_deref(),
Some("AAC (Apple)")
);
assert_eq!(named(Codec::Aac, None), None);
let shared = |grid: bool| {
let mut reading = reading.clone();
let guess = reading
.encoder_guess
.as_mut()
.expect("the fake has a guess");
guess.profile.setting = "~320 kbps".to_string();
guess.profile.also = Some("AAC".to_string());
if !grid {
reading.grid = None;
}
encoder(&reading)
};
assert_eq!(shared(true).as_deref(), Some("MP3 ~320 kbps"));
assert_eq!(shared(false).as_deref(), Some("MP3 ~320 kbps or AAC"));
}
#[test]
fn a_control_character_in_a_name_is_written_as_its_escape() {
assert_eq!(
legible(Path::new("a\nb\u{1b}[31m \u{e9}.flac")),
"a\\nb\\u{1b}[31m \u{e9}.flac"
);
}
#[test]
fn a_readout_lines_up_its_values_and_wraps_them_under_themselves() {
let (_, fake) = honest_and_fake();
let plain = look(true, 60);
let text = readout(Path::new("album/fake.flac"), &fake, plain);
let lines: Vec<&str> = text.lines().collect();
assert_eq!(lines[0], "album/fake.flac");
assert!(
lines.contains(&" Verdict likely transcode"),
"{text}"
);
assert!(lines.contains(&" Encoder MP3 ~128 kbps"), "{text}");
assert!(
lines
.iter()
.any(|line| line.starts_with(" Grid MP3 at offset ")),
"{text}"
);
let summary = lines
.iter()
.position(|line| line.starts_with(" Summary"))
.unwrap();
assert!(lines[summary + 1].starts_with(" "), "{text}");
assert!(lines.iter().all(|line| line.len() <= 60), "{text}");
let coloured = Look {
colour: true,
..plain
};
let coloured = readout(Path::new("album/fake.flac"), &fake, coloured);
assert!(
coloured.contains("\u{1b}[31mlikely transcode\u{1b}[0m"),
"{coloured}"
);
assert_eq!(uncoloured(&coloured), text);
}
fn row<'a>(path: &str, root: Option<&'a str>, checked: &Checked) -> Row<'a> {
Row {
path: path.into(),
root: root.map(Path::new),
outcome: Ok(Box::new(checked.clone())),
}
}
fn listed(rows: &[Row<'_>], verbose: bool, width: u16) -> Vec<String> {
let took = Duration::from_secs(30);
let text = tables(rows, look(verbose, width), took, true, None);
let lines = text.lines().take_while(|line| !line.is_empty());
lines.map(str::to_string).collect()
}
#[test]
fn tables_list_the_flagged_and_the_unread_and_end_with_the_counts() {
let (honest, fake) = honest_and_fake();
let rows = [
row("album/ev\u{1b}[31mil\n.flac", None, &fake),
row("album/honest.flac", None, &honest),
Row {
path: "album/text.flac".into(),
root: None,
outcome: Err("the container was not recognized".to_string()),
},
];
let took = Duration::from_secs(30);
let plain = look(false, 100);
let text = tables(&rows, plain, took, true, None);
let lines: Vec<&str> = text.lines().collect();
let cells = |line: &str| -> Vec<String> {
let cells = line.split(" ").filter(|cell| !cell.is_empty());
cells.map(|cell| cell.trim().to_string()).collect()
};
assert_eq!(cells(lines[0]), ["Verdict", "Cutoff", "Encoder", "File"]);
assert!(
lines[1].starts_with(" ---------------- -------- "),
"{text}"
);
assert_eq!(lines[2], "album/");
let flagged = cells(lines[3]);
assert_eq!(flagged[0], "likely transcode");
assert_eq!(flagged[2..], ["MP3 ~128 kbps", "ev\\u{1b}[31mil\\n.flac"]);
assert!(!text.contains('\u{1b}'), "{text}");
assert_eq!(
lines[4..8],
[
"",
"Not read",
"--------",
"album/text.flac: the container was not recognized"
]
);
assert_eq!(
lines[8..],
[
"",
"Verdict Files",
"------------------ -------",
"consistent 1",
"likely transcode 1",
"mixed sources 0",
"possibly lossy 0",
"inconclusive 0",
"",
"Files read 2",
"Files not read 1",
"Wall time 30 s",
"Files per minute 6",
]
);
let left = Some("5 of 8 files left");
let cut_short = tables(&rows, plain, took, true, left);
assert_eq!(
cut_short,
format!("{text}\nInterrupted 5 of 8 files left\n")
);
let coloured = Look {
colour: true,
..plain
};
let coloured = tables(&rows, coloured, took, true, None);
assert!(
coloured.contains("\u{1b}[31mlikely transcode\u{1b}[0m"),
"{coloured}"
);
assert_eq!(uncoloured(&coloured), text);
let clean = [
row("a/1.flac", None, &honest),
row("a/2.flac", None, &honest),
];
let text = tables(&clean, plain, took, true, None);
assert!(text.starts_with("Verdict Files\n"), "{text}");
let again = tables(&clean, plain, Duration::from_millis(2), false, None);
assert_eq!(cells(again.lines().last().unwrap()), ["Wall time", "0.0 s"]);
assert!(!again.contains("per minute"), "{again}");
}
#[test]
fn files_are_listed_by_directory_from_the_directory_named() {
let (honest, fake) = honest_and_fake();
let mut cut_lower = fake.clone();
cut_lower.readings[0].reading.cutoff_hz = Some(15_500.0);
let mut possibly = fake.clone();
possibly.readings[0].reading.verdict = Verdict::PossiblyLossy;
let rows = [
row("loose/one.flac", None, &fake),
row("pair/a.flac", None, &fake),
row("pair/b.flac", None, &honest),
row("music/top.flac", Some("music"), &fake),
row("music/mixed/1.flac", Some("music"), &fake),
row("music/mixed/2.flac", Some("music"), &honest),
row("music/mixed/3.flac", Some("music"), &possibly),
row("music/same/1.flac", Some("music"), &fake),
row("music/same/2.flac", Some("music"), &cut_lower),
row("music/clean/1.flac", Some("music"), &honest),
row("other/deep/down/1.flac", Some("other/"), &fake),
];
let lines = listed(&rows, false, 100);
let names: Vec<&str> = lines[2..]
.iter()
.map(|line| match line.strip_prefix(" ") {
Some(file) => file.rsplit(" ").next().unwrap(),
None => line,
})
.collect();
let cutoff = khz(fake.readings[0].reading.cutoff_hz.unwrap());
let alike =
format!("same/ 2 of 2 likely transcode (MP3 grid, cutoff 15.5 to {cutoff}, MP3 ~128 kbps)");
assert_eq!(
names,
[
"loose/one.flac",
"pair/",
"a.flac",
"music/",
"top.flac",
"mixed/",
"1.flac",
"3.flac",
alike.as_str(),
"other/",
"deep/down/",
"1.flac",
]
);
assert!(
lines[0].starts_with(" Verdict Cutoff "),
"{lines:?}"
);
let mut unnamed = fake.clone();
let reading = &mut unnamed.readings[0].reading;
reading.grid.as_mut().unwrap().codec = Codec::Aac;
let lines = listed(&[row("one.flac", None, &unnamed)], false, 100);
assert!(lines[2].ends_with(" kHz AAC one.flac"), "{lines:?}");
let lines = listed(&rows, true, 100);
assert_eq!(lines.len(), 2 + 7 + rows.len(), "{lines:?}");
assert!(lines.iter().any(|line| line.contains(" MP3, offset ")));
assert!(lines.iter().all(|line| !line.contains(" of ")), "{lines:?}");
}
#[test]
fn the_list_fits_the_terminal_and_gives_way_to_lines_in_a_narrow_one() {
let (_, fake) = honest_and_fake();
let long = "a-recording-with-a-long-name-that-no-terminal-of-80-columns-holds.24.flac";
let rows = [
row(&format!("music/{long}"), Some("music"), &fake),
row("music/short.flac", Some("music"), &fake),
];
let lines = listed(&rows, true, 80);
assert!(lines.iter().all(|line| line.len() <= 80), "{lines:?}");
let cut_short = lines.iter().find(|line| line.contains("...")).unwrap();
assert_eq!(cut_short.len(), 80);
assert!(cut_short.ends_with(".24.flac"), "{cut_short}");
assert!(cut_short.contains(" a-recor"), "{cut_short}");
let lines = listed(&rows, false, 59);
assert_eq!(lines.len(), 2);
assert!(
lines[0].starts_with(&format!(
"music/{long}: likely transcode (MP3 grid, cutoff "
)),
"{lines:?}"
);
}
#[test]
fn a_cut_keeps_both_ends_within_the_width() {
assert_eq!(cut("short.flac", 10), "short.flac");
assert_eq!(cut("0123456789abcdef", 10), "0123...def");
assert_eq!(cut("0123456789abcdef", 9), "012...def");
assert_eq!(
cut("\u{97f3}\u{697d}\u{97f3}\u{697d}\u{97f3}\u{697d}.flac", 12),
"\u{97f3}\u{697d}...flac"
);
assert_eq!(cut("abcdef", 2), "...");
}
#[test]
fn the_bars_take_at_most_half_the_terminal() {
assert_eq!(room(24, true, 11), 11);
assert_eq!(room(24, true, 12), 10);
assert_eq!(room(24, false, 12), 12);
assert_eq!(room(24, false, 32), 11);
assert_eq!(room(1, true, 5), 1);
assert_eq!(room(0, false, 1), 1);
}
}