use clap::Clap;
use dua::ByteFormat as LibraryByteFormat;
use std::path::PathBuf;
use std::str::FromStr;
#[derive(PartialEq, Debug)]
pub enum ByteFormat {
Metric,
Binary,
Bytes,
GB,
GiB,
MB,
MiB,
}
impl FromStr for ByteFormat {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"metric" | "Metric" => ByteFormat::Metric,
"binary" | "Binary" => ByteFormat::Binary,
"bytes" | "Bytes" => ByteFormat::Bytes,
"GB" | "Gb" | "gb" => ByteFormat::GB,
"GiB" | "gib" => ByteFormat::GiB,
"MB" | "Mb" | "mb" => ByteFormat::MB,
"MiB" | "mib" => ByteFormat::MiB,
_ => return Err(format!("Invalid byte format: {:?}", s)),
})
}
}
impl ByteFormat {
const VARIANTS: &'static [&'static str] =
&["metric", "binary", "bytes", "MB", "MiB", "GB", "GiB"];
}
impl From<ByteFormat> for LibraryByteFormat {
fn from(input: ByteFormat) -> Self {
match input {
ByteFormat::Metric => LibraryByteFormat::Metric,
ByteFormat::Binary => LibraryByteFormat::Binary,
ByteFormat::Bytes => LibraryByteFormat::Bytes,
ByteFormat::GB => LibraryByteFormat::GB,
ByteFormat::GiB => LibraryByteFormat::GiB,
ByteFormat::MB => LibraryByteFormat::MB,
ByteFormat::MiB => LibraryByteFormat::MiB,
}
}
}
#[derive(Debug, Clap)]
#[clap(name = "dua", about = "A tool to learn about disk usage, fast!", version = clap::crate_version!())]
#[clap(setting = clap::AppSettings::ColoredHelp)]
#[clap(setting = clap::AppSettings::GlobalVersion)]
#[clap(override_usage = "dua [FLAGS] [OPTIONS] [SUBCOMMAND] [input]...")]
pub struct Args {
#[clap(subcommand)]
pub command: Option<Command>,
#[clap(short = 't', long = "threads", default_value = "0")]
pub threads: usize,
#[clap(short = 'f', long, case_insensitive = true, possible_values(&ByteFormat::VARIANTS))]
pub format: Option<ByteFormat>,
#[clap(short = 'A', long)]
pub apparent_size: bool,
#[clap(short = 'l', long)]
pub count_hard_links: bool,
#[clap(short = 'x', long)]
pub stay_on_filesystem: bool,
#[clap(parse(from_os_str))]
pub input: Vec<PathBuf>,
}
#[derive(Debug, Clap)]
pub enum Command {
#[cfg(any(feature = "tui-unix", feature = "tui-crossplatform"))]
#[clap(name = "interactive", visible_alias = "i")]
Interactive {
#[clap(parse(from_os_str))]
input: Vec<PathBuf>,
},
#[clap(name = "aggregate", visible_alias = "a")]
Aggregate {
#[clap(long = "stats")]
statistics: bool,
#[clap(long)]
no_sort: bool,
#[clap(long)]
no_total: bool,
#[clap(parse(from_os_str))]
input: Vec<PathBuf>,
},
}