pub mod colors;
pub mod entry;
use crate::{colors::*, entry::Entry};
use std::{env, error::Error, fs, path::Path};
pub fn init() -> Result<(), Box<dyn Error>> {
let mut args = env::args();
args.next();
let start_path = env::current_dir()?;
let mut paths = Vec::new();
if args.len() == 0 {
paths.push(String::from("."))
}
for arg in args {
paths.push(arg);
}
let mut len = paths.len();
for path in &paths {
env::set_current_dir(&start_path)?;
if len != paths.len() {
println!();
}
if !Path::new(path).exists() {
eprintln!(
"{}Path doesn't exist. Path: {}{}{}",
RED, GREEN, path, RESET
);
continue;
}
if !Path::new(path).is_dir() {
eprintln!(
"{}Path is not a directory. Path: {}{}{}",
RED, BLUE, path, RESET
);
continue;
}
env::set_current_dir(path)?;
if paths.len() > 1 {
println!(
"{}{}{}{}",
BOLD,
BLUE,
get_ending(&format!("{:?}", env::current_dir()?)),
RESET
);
}
len -= 1;
output()?;
}
Ok(())
}
fn output() -> Result<(), Box<dyn Error>> {
let entries = get_entries()?;
for entry in entries {
println!("{}", entry.print)
}
Ok(())
}
fn get_entries() -> Result<Vec<Entry>, Box<dyn Error>> {
let mut entries = Vec::new();
for path in fs::read_dir(env::current_dir()?)? {
entries.push(Entry::from_path(&path?.path())?);
}
Ok(entries)
}
pub fn get_ending(full: &String) -> String {
let indexes: Vec<_> = full.match_indices('/').map(|(i, _)| i).collect();
let last_index = indexes[indexes.len() - 1];
let ending = &full[last_index + 1..full.len() - 1];
ending.to_string()
}