mod item;
mod item_size;
mod sort_opt;
mod table;
mod utils;
mod walk_dir;
use clap::Parser;
use owo_colors::OwoColorize;
use std::fs;
use std::path;
use std::io::ErrorKind;
use crate::item::Item;
use crate::sort_opt::SortOpt;
use crate::utils::get_file_size;
use crate::walk_dir::{print_dir_size, print_dir_size_with_files};
#[derive(Parser, Debug, Clone)]
pub struct Args {
path: path::PathBuf,
#[clap(short = 'd', long)]
sort_files_desc: bool,
#[clap(short = 'a', long)]
sort_files_asc: bool,
#[clap(short = 'i', long)]
include_hidden: bool,
#[clap(short = 'l', long)]
list_files: bool,
#[clap(short = 'L', long)]
list_all: bool,
#[clap(short = 'g', long)]
include_gitignored: bool,
#[clap(short = 'e', long, num_args=1.., value_delimiter=' ')]
exclude_dirs: Vec<path::PathBuf>,
#[clap(short = 'n', long)]
num_files: Option<usize>,
#[clap(short = 'D', long)]
only_dirs: bool,
#[clap(short = 'R', long)]
recursive_dirs: bool,
#[clap(short = 's', long)]
show_lines: bool,
}
fn main() {
let mut args = Args::parse();
if args.path.is_dir() {
let buf = fs::read_dir(&args.path);
if let Some(num) = args.num_files {
if num <= 0 || num > 100 {
println!("{}", "sz: error: invalid number of files to list".red());
println!("{}", "sz: number of files must be between 1 and 100".blue());
return;
}
}
if let Err(e) = buf {
match e.kind() {
ErrorKind::NotFound => {
println!(
"sz: error while reading path: `{}` not found",
args.path.display()
);
}
_ => println!("sz: error while reading path: {}", e),
}
}
if args.list_files || args.list_all {
let sort_opt = SortOpt::from_args(&args);
print_dir_size_with_files(&mut args, sort_opt);
} else {
print_dir_size(args);
}
} else {
let file = Item::new(
String::from(args.path.file_name().unwrap().to_str().unwrap()),
get_file_size(args.path.as_path()),
);
println!("{}", file);
}
}