window
This commit is contained in:
Generated
+4448
File diff suppressed because it is too large
Load Diff
+2
-1
@@ -4,6 +4,7 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
eframe = "0.37.0"
|
||||||
|
|
||||||
|
|
||||||
[llvm]
|
[llvm]
|
||||||
@@ -11,4 +12,4 @@ download-ci-llvm = true # download prebuilt LLVM instead of compiling it (save
|
|||||||
|
|
||||||
[rust]
|
[rust]
|
||||||
debug = false
|
debug = false
|
||||||
incremental = true
|
incremental = true
|
||||||
|
|||||||
+217
-16
@@ -1,8 +1,81 @@
|
|||||||
use std::collections::BTreeMap;
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use eframe::egui::{self, Align2, Color32, FontId, Pos2, Sense, Stroke, Vec2};
|
||||||
|
use std::f32::consts::PI;
|
||||||
|
|
||||||
|
struct App {
|
||||||
|
vault: Vault,
|
||||||
|
pos: Vec<Pos2>, // node positions in "world" space
|
||||||
|
pan: Vec2,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl App {
|
||||||
|
fn new(vault: Vault) -> Self {
|
||||||
|
let n = vault.notes.len();
|
||||||
|
let pos = (0..n)
|
||||||
|
.map(|i| {
|
||||||
|
let a = 2.0 * PI * i as f32 / n as f32;
|
||||||
|
Pos2::new(300.0 * a.cos(), 300.0 * a.sin())
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
Self { vault, pos, pan: Vec2::ZERO }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn radius(&self, i: usize) -> f32 {
|
||||||
|
10.0 + 4.0 * self.vault.links.in_degree(i).min(5) as f32 // the size knob
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl eframe::App for App {
|
||||||
|
fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
|
||||||
|
egui::CentralPanel::default().show(ui, |ui| {
|
||||||
|
let (resp, painter) =
|
||||||
|
ui.allocate_painter(ui.available_size(), Sense::click_and_drag());
|
||||||
|
|
||||||
|
// drag background to pan
|
||||||
|
self.pan += resp.drag_delta();
|
||||||
|
let center = resp.rect.center().to_vec2() + self.pan;
|
||||||
|
let to_screen = |p: Pos2| p + center;
|
||||||
|
|
||||||
|
// 1. edges (with arrowheads, trimmed to the node circles)
|
||||||
|
let n = self.vault.notes.len();
|
||||||
|
for a in 0..n {
|
||||||
|
for b in self.vault.links.links_from(a).filter(|&b| b != a) {
|
||||||
|
let (pa, pb) = (to_screen(self.pos[a]), to_screen(self.pos[b]));
|
||||||
|
let dir = (pb - pa).normalized();
|
||||||
|
let start = pa + dir * self.radius(a);
|
||||||
|
let end = pb - dir * self.radius(b);
|
||||||
|
painter.arrow(start, end - start, Stroke::new(1.5, Color32::GRAY));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. nodes + labels
|
||||||
|
let hover = resp.hover_pos();
|
||||||
|
for i in 0..n {
|
||||||
|
let p = to_screen(self.pos[i]);
|
||||||
|
let r = self.radius(i);
|
||||||
|
let hovered = hover.map_or(false, |h| h.distance(p) <= r);
|
||||||
|
let color = if hovered { Color32::LIGHT_BLUE } else { Color32::from_rgb(90, 130, 200) };
|
||||||
|
|
||||||
|
painter.circle_filled(p, r, color);
|
||||||
|
painter.text(
|
||||||
|
p + Vec2::new(0.0, r + 4.0),
|
||||||
|
Align2::CENTER_TOP,
|
||||||
|
&self.vault.notes[i].title, // guessing `title` again
|
||||||
|
FontId::proportional(13.0),
|
||||||
|
Color32::WHITE,
|
||||||
|
);
|
||||||
|
|
||||||
|
if hovered && resp.clicked() {
|
||||||
|
println!("open note {i}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
enum NoteKind {
|
enum NoteKind {
|
||||||
@@ -22,27 +95,147 @@ impl NoteKind {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
struct Note {
|
struct Note {
|
||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
kind: NoteKind,
|
kind: NoteKind,
|
||||||
tags: Vec<String>, // folder names above this note
|
tags: Vec<String>,
|
||||||
|
raw: String,
|
||||||
|
title: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Note {
|
||||||
|
fn title(&self) -> String {
|
||||||
|
self.path
|
||||||
|
.file_stem()
|
||||||
|
.and_then(|s| s.to_str())
|
||||||
|
.unwrap_or("?")
|
||||||
|
.to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
struct AdjMatrix {
|
||||||
|
n: usize,
|
||||||
|
data: Vec<bool>, // row-major: data[from * n + to]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl AdjMatrix {
|
||||||
|
|
||||||
|
fn in_degree(&self, to: usize) -> usize {
|
||||||
|
(0..self.n).filter(|&from| self.has_edge(from, to)).count()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// print the matrix as 0/1 grid
|
||||||
|
fn print_matrix(&self) {
|
||||||
|
// header row with column indices
|
||||||
|
print!(" ");
|
||||||
|
for to in 0..self.n {
|
||||||
|
print!("{:>2} ", to);
|
||||||
|
}
|
||||||
|
println!();
|
||||||
|
|
||||||
|
for from in 0..self.n {
|
||||||
|
print!("{:>2} ", from);
|
||||||
|
for to in 0..self.n {
|
||||||
|
let c = if self.has_edge(from, to) { 1 } else { 0 };
|
||||||
|
print!("{:>2} ", c);
|
||||||
|
}
|
||||||
|
println!();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// print as a list: "0 -> 2, 5"
|
||||||
|
fn print_links(&self) {
|
||||||
|
for from in 0..self.n {
|
||||||
|
let targets: Vec<String> =
|
||||||
|
self.links_from(from).map(|t| t.to_string()).collect();
|
||||||
|
if !targets.is_empty() {
|
||||||
|
println!("{} -> {}", from, targets.join(", "));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new(n: usize) -> Self {
|
||||||
|
Self { n, data: vec![false; n * n] }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// directed edge: `from` links to `to`
|
||||||
|
fn add_edge(&mut self, from: usize, to: usize) {
|
||||||
|
self.data[from * self.n + to] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn has_edge(&self, from: usize, to: usize) -> bool {
|
||||||
|
self.data[from * self.n + to]
|
||||||
|
}
|
||||||
|
|
||||||
|
/// all notes that `from` links to
|
||||||
|
fn links_from(&self, from: usize) -> impl Iterator<Item = usize> + '_ {
|
||||||
|
(0..self.n).filter(move |&to| self.has_edge(from, to))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
struct Vault {
|
struct Vault {
|
||||||
notes: Vec<Note>, // nodes
|
notes: Vec<Note>,
|
||||||
tags: BTreeMap<String, Vec<usize>>, // tag -> indices into `notes` (the edges)
|
tags: HashMap<String, Vec<usize>>,
|
||||||
images: Vec<PathBuf>, // files found in ".images" folders
|
images: Vec<PathBuf>,
|
||||||
|
links: AdjMatrix, // links.has_edge(a, b): note a links to note b
|
||||||
}
|
}
|
||||||
|
|
||||||
fn scan_dir(root: &Path) -> io::Result<Vault> {
|
fn scan_dir(root: &Path) -> io::Result<Vault> {
|
||||||
let mut vault = Vault::default();
|
let mut vault = Vault::default();
|
||||||
walk(root, root, false, &mut vault)?;
|
walk(root, root, false, &mut vault)?;
|
||||||
|
vault.links = AdjMatrix::new(vault.notes.len());
|
||||||
|
scan_links(&mut vault)?;
|
||||||
Ok(vault)
|
Ok(vault)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn scan_links(vault: &mut Vault) -> io::Result<()> {
|
||||||
|
// file stem -> note index, for resolving [[targets]]
|
||||||
|
let mut by_stem: HashMap<String, usize> = HashMap::new();
|
||||||
|
for (i, n) in vault.notes.iter().enumerate() {
|
||||||
|
if let Some(stem) = n.path.file_stem().and_then(|s| s.to_str()) {
|
||||||
|
// first note wins if two folders have the same file name
|
||||||
|
by_stem.entry(stem.to_lowercase()).or_insert(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for from in 0..vault.notes.len() {
|
||||||
|
for target in extract_links(&vault.notes[from].raw) {
|
||||||
|
if let Some(&to) = by_stem.get(&target.to_lowercase()) {
|
||||||
|
if to != from {
|
||||||
|
vault.links.add_edge(from, to);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn extract_links(text: &str) -> Vec<&str> {
|
||||||
|
let mut targets = Vec::new();
|
||||||
|
let mut rest = text;
|
||||||
|
|
||||||
|
while let Some((_, after_open)) = rest.split_once("[[") {
|
||||||
|
match after_open.split_once("]]") {
|
||||||
|
Some((target, after_close)) => {
|
||||||
|
targets.push(target.trim());
|
||||||
|
rest = after_close;
|
||||||
|
}
|
||||||
|
None => break, // unclosed "[[", nothing more to find
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
targets
|
||||||
|
}
|
||||||
|
|
||||||
fn walk(root: &Path, dir: &Path, in_images: bool, vault: &mut Vault) -> io::Result<()> {
|
fn walk(root: &Path, dir: &Path, in_images: bool, vault: &mut Vault) -> io::Result<()> {
|
||||||
let mut paths: Vec<PathBuf> = fs::read_dir(dir)?
|
let mut paths: Vec<PathBuf> = fs::read_dir(dir)?
|
||||||
.map(|e| e.map(|e| e.path()))
|
.map(|e| e.map(|e| e.path()))
|
||||||
@@ -51,7 +244,6 @@ fn walk(root: &Path, dir: &Path, in_images: bool, vault: &mut Vault) -> io::Resu
|
|||||||
|
|
||||||
for path in paths {
|
for path in paths {
|
||||||
let meta = fs::symlink_metadata(&path)?;
|
let meta = fs::symlink_metadata(&path)?;
|
||||||
|
|
||||||
if meta.is_dir() {
|
if meta.is_dir() {
|
||||||
let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
|
let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
|
||||||
// skip hidden folders, except ".images"
|
// skip hidden folders, except ".images"
|
||||||
@@ -93,12 +285,23 @@ fn walk(root: &Path, dir: &Path, in_images: bool, vault: &mut Vault) -> io::Resu
|
|||||||
for tag in &tags {
|
for tag in &tags {
|
||||||
vault.tags.entry(tag.clone()).or_default().push(idx);
|
vault.tags.entry(tag.clone()).or_default().push(idx);
|
||||||
}
|
}
|
||||||
vault.notes.push(Note { path, kind, tags });
|
|
||||||
|
let title = path
|
||||||
|
.file_stem()
|
||||||
|
.and_then(|s| s.to_str())
|
||||||
|
.unwrap_or("?")
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
let raw = fs::read_to_string(&path).unwrap_or_default();
|
||||||
|
vault.notes.push(Note { path, kind, tags, raw, title });
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
fn main() -> io::Result<()> {
|
fn main() -> io::Result<()> {
|
||||||
let dir = env::args().nth(1).unwrap_or_else(|| ".".to_string());
|
let dir = env::args().nth(1).unwrap_or_else(|| ".".to_string());
|
||||||
let root = Path::new(&dir);
|
let root = Path::new(&dir);
|
||||||
@@ -109,14 +312,12 @@ fn main() -> io::Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let vault = scan_dir(root)?;
|
let vault = scan_dir(root)?;
|
||||||
|
vault.links.print_links(); // optional console dump
|
||||||
|
|
||||||
for (tag, ids) in &vault.tags {
|
eframe::run_native(
|
||||||
println!("#{tag}");
|
"Vault",
|
||||||
for &i in ids {
|
eframe::NativeOptions::default(),
|
||||||
let note = &vault.notes[i];
|
Box::new(|_cc| Ok(Box::new(App::new(vault)))),
|
||||||
println!(" {} [{:?}]", note.path.display(), note.kind);
|
)
|
||||||
}
|
.map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user