tabula rasa

This commit is contained in:
🪞👃🪞 2025-03-02 18:00:19 +02:00
commit 2b855f43d7
9 changed files with 1601 additions and 0 deletions

123
src/main.rs Normal file
View file

@ -0,0 +1,123 @@
#![feature(os_str_display)]
use tek_tui::*;
use tek_tui::tek_input::*;
use tek_tui::tek_output::*;
use crate::crossterm::event::{Event, KeyEvent, KeyCode, KeyModifiers, KeyEventState, KeyEventKind};
use clap::{arg, command, value_parser, ArgAction, Command};
use walkdir::WalkDir;
use std::sync::{Arc, RwLock};
use std::path::{Path, PathBuf};
use std::env::current_dir;
mod keys;
mod view;
type Usually<T> = std::result::Result<T, Box<dyn std::error::Error>>;
type Perhaps<T> = Usually<Option<T>>;
fn cli () -> clap::Command {
command!()
.arg(arg!([path] "Path to root directory").value_parser(value_parser!(PathBuf)))
}
struct Taggart {
root: PathBuf,
paths: Vec<Entry>,
cursor: usize,
offset: usize,
}
#[derive(Ord, Eq, PartialEq, PartialOrd, Default)]
struct Entry {
path: PathBuf,
is_dir: bool,
is_mus: bool,
is_img: bool,
depth: usize,
}
fn main () -> Usually<()> {
let args = cli().get_matches();
let path = args.get_one::<PathBuf>("path");
let state = Arc::new(RwLock::new(Taggart::new(path)?));
Tui::new()?.run(&state)
}
impl Taggart {
fn new (root: Option<&impl AsRef<Path>>) -> Usually<Self> {
let root = if let Some(root) = root {
root.as_ref().into()
} else {
current_dir()?
};
let mut paths = vec![];
for entry in WalkDir::new(&root)
.into_iter()
.filter_entry(|e|!e
.file_name()
.to_str()
.map(|s|s.starts_with("."))
.unwrap_or(false))
{
let entry = entry?;
if entry.depth() == 0 {
continue
}
let depth = entry.depth();
let path = entry.into_path();
paths.push(Entry {
path: path.strip_prefix(&root)?.into(),
is_dir: path.is_dir(),
is_mus: false,
is_img: false,
depth
});
}
paths.sort();
Ok(Self {
root,
paths,
cursor: 0,
offset: 0,
})
}
}
//pub enum Entry {
//Dir {
//path: PathBuf,
//name: OsString,
//entries: Vec<Box<FileTree>>,
//},
//File {
//path: PathBuf,
//name: OsString,
//}
//}
//impl Entry {
//fn new (path: &impl AsRef<Path>) -> Usually<Self> {
//let mut paths = vec![];
//for entry in WalkDir::new(&root)
//.into_iter()
//.filter_entry(|e|!e
//.file_name()
//.to_str()
//.map(|s|s.starts_with("."))
//.unwrap_or(false))
//{
//let path = entry?.into_path().strip_prefix(&root)?.into();
//paths.push(path);
//}
//paths.sort();
//}
//}
//struct FileTree {
//path: PathBuf,
//name: OsString,
//entries: Vec<Box<FileTree>>,
//}
//impl FileTree {
//}