extern crate picleo;
use std::fmt;
use std::fs;
use std::io::{self, BufRead};
use std::path::PathBuf;
use anyhow::Result;
use clap::Parser;
use picleo::selectable::Selectable;
use picleo::picker::Picker;
#[derive(Debug, Clone)]
struct DisplayPath(PathBuf);
impl fmt::Display for DisplayPath {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0.display())
}
}
impl From<PathBuf> for DisplayPath {
fn from(path: PathBuf) -> Self {
DisplayPath(path)
}
}
impl AsRef<PathBuf> for DisplayPath {
fn as_ref(&self) -> &PathBuf {
&self.0
}
}
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
#[arg(name = "DIRS")]
dirs: Vec<PathBuf>,
}
fn main() -> Result<()> {
let args = Args::parse();
if !args.dirs.is_empty() {
let mut picker = Picker::<DisplayPath>::new();
picker.inject_items(|i|
for dir in args.dirs {
if let Ok(entries) = fs::read_dir(&dir) {
for entry in entries.flatten() {
let path = entry.path();
i.push(Selectable::new(DisplayPath(path).clone()), |item, columns| columns[0] = item.to_string().into());
}
}
}
);
match picker.run() {
Ok(paths) => {
for path in paths {
println!("{}", path.0.display())
}
}
Err(err) => {
println!("{err:?}");
return Err(anyhow::anyhow!("{:?}", err));
}
}
} else {
let mut picker = Picker::<String>::new();
picker.inject_items(|i| {
for line in io::stdin().lock().lines().map_while(Result::ok) {
i.push(Selectable::new(line.clone()), |item, columns| {
columns[0] = item.to_string().into()
});
}
});
match picker.run() {
Ok(lines) => {
for line in lines {
println!("{}", line)
}
}
Err(err) => {
println!("{err:?}");
return Err(anyhow::anyhow!("{:?}", err));
}
}
}
Ok(())
}