#!/usr/bin/env rust-script
use std::fs::{canonicalize, read_to_string};
use std::path::{Path, PathBuf};
use std::time::Instant;
use colored::Colorize as _;
use rayon::prelude::*;
const PATH: &str = "/home/barrett/Downloads/linuxhw_edid_repo";
fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::DEBUG)
.compact()
.init();
let path = canonicalize(PathBuf::from(PATH)).expect("canonicalize");
println!("checking at path `{}`", path.display());
let timer = Instant::now();
let paths = [path.join("Digital"), path.join("Analog")]
.par_iter()
.flat_map(|p| {
let inner: Vec<_> = jwalk::WalkDir::new(p)
.into_iter()
.flat_map(|entry| {
if let Ok(entry) = entry {
Some(entry.path())
} else {
None
}
})
.map(run) .collect();
inner
})
.collect::<Vec<Option<PathBuf>>>()
.into_iter()
.flatten()
.collect::<Vec<PathBuf>>();
println!(
"Completed in {:.2} seconds.\n",
Instant::now().duration_since(timer).as_secs_f32()
);
print_results(&paths);
Ok(())
}
fn run<P: AsRef<Path>>(entry_path: P) -> Option<PathBuf> {
let entry_path = entry_path.as_ref();
if let Some(edid) = edid_by_filename(entry_path) {
let _18b_data_blocks = [0x48, 0x5a, 0x6c];
let wanted = [0x00, 0x00, 0x00, 0xf7];
if _18b_data_blocks
.iter()
.any(|b| &edid[(*b)..(*b + wanted.len())] == &wanted)
{
return Some(entry_path.into());
}
}
None
}
#[tracing::instrument]
pub(crate) fn edid_by_filename(path: &Path) -> Option<Vec<u8>> {
let path = PathBuf::from(path);
let s = read_to_string(&path).ok()?;
if !s.contains("edid-decode (hex):") {
return None;
}
let v: Vec<&str> = s.split("----------------").collect();
let s = v.first()?.trim();
let s = s.replace("edid-decode (hex):", "");
let s = s.replace([' ', '\n', '\r'], "");
let hr = hex::decode(&s).inspect_err(|e| {
tracing::error!("couldn't turn into hex! (err: {e}) for the following hex:");
eprintln!("{s}")
});
hr.ok()
}
fn print_results(paths: &Vec<PathBuf>) {
if paths.is_empty() {
println!("{}", "Found no matching files!".red().on_black());
} else {
println!("Here's a list of all matching files: ");
for p in paths.chunks(2) {
if let Some(p0) = p.get(0) {
println!("{}", p0.display().to_string().white().on_black());
}
if let Some(p1) = p.get(1) {
println!("{}", p1.display().to_string().white().on_bright_black());
}
}
println!();
println!(
"{}{}{}",
"Found ".green().on_black(),
paths.len().to_string().bright_green().on_black(),
" matching files.".green().on_black()
);
}
}