better
This commit is contained in:
+20
-15
@@ -1,4 +1,4 @@
|
||||
use std::collections::HashMap;
|
||||
use std::collections::BTreeMap;
|
||||
use std::env;
|
||||
use std::fs;
|
||||
use std::io;
|
||||
@@ -22,8 +22,8 @@ impl NoteKind {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
#[allow(dead_code)]
|
||||
struct Note {
|
||||
path: PathBuf,
|
||||
kind: NoteKind,
|
||||
@@ -32,30 +32,34 @@ struct Note {
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
struct Vault {
|
||||
notes: Vec<Note>, // nodes
|
||||
tags: HashMap<String, Vec<usize>>, // tag -> indices into `notes` (the edges)
|
||||
images: Vec<PathBuf>, // files found in ".images" folders
|
||||
notes: Vec<Note>, // nodes
|
||||
tags: BTreeMap<String, Vec<usize>>, // tag -> indices into `notes` (the edges)
|
||||
images: Vec<PathBuf>, // files found in ".images" folders
|
||||
}
|
||||
|
||||
fn scan_dir(root: &Path) -> io::Result<Vault> {
|
||||
let mut vault = Vault::default();
|
||||
walk(root, root, &mut vault)?;
|
||||
walk(root, root, false, &mut vault)?;
|
||||
Ok(vault)
|
||||
}
|
||||
|
||||
fn walk(root: &Path, dir: &Path, vault: &mut Vault) -> io::Result<()> {
|
||||
let in_images = dir.file_name().and_then(|n| n.to_str()) == Some(".images");
|
||||
fn walk(root: &Path, dir: &Path, in_images: bool, vault: &mut Vault) -> io::Result<()> {
|
||||
let mut paths: Vec<PathBuf> = fs::read_dir(dir)?
|
||||
.map(|e| e.map(|e| e.path()))
|
||||
.collect::<io::Result<_>>()?;
|
||||
paths.sort();
|
||||
|
||||
for entry in fs::read_dir(dir)? {
|
||||
let path = entry?.path();
|
||||
for path in paths {
|
||||
let meta = fs::symlink_metadata(&path)?;
|
||||
|
||||
if path.is_dir() {
|
||||
// skip hidden folders, except ".images"
|
||||
if meta.is_dir() {
|
||||
let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
|
||||
// skip hidden folders, except ".images"
|
||||
if name.starts_with('.') && name != ".images" {
|
||||
continue;
|
||||
}
|
||||
walk(root, &path, vault)?;
|
||||
// once inside ".images", stay "inside" for all nested folders
|
||||
walk(root, &path, in_images || name == ".images", vault)?;
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -109,9 +113,10 @@ fn main() -> io::Result<()> {
|
||||
for (tag, ids) in &vault.tags {
|
||||
println!("#{tag}");
|
||||
for &i in ids {
|
||||
println!(" {}", vault.notes[i].path.display());
|
||||
let note = &vault.notes[i];
|
||||
println!(" {} [{:?}]", note.path.display(), note.kind);
|
||||
}
|
||||
}
|
||||
println!("{} notes, {} images", vault.notes.len(), vault.images.len());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user