mirror of
https://codeberg.org/unspeaker/perch.git
synced 2025-12-06 17:46:42 +01:00
132 lines
3.2 KiB
Rust
132 lines
3.2 KiB
Rust
#![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,
|
|
column: usize,
|
|
size: Measure<TuiOut>,
|
|
}
|
|
#[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();
|
|
let (is_dir, is_mus, is_img) = if path.is_dir() {
|
|
(true, false, false)
|
|
} else {
|
|
(false, false, false)
|
|
};
|
|
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,
|
|
column: 0,
|
|
size: Measure::new(),
|
|
})
|
|
}
|
|
}
|
|
|
|
//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 {
|
|
|
|
//}
|