|
| 1 | +use std::path::PathBuf; |
| 2 | + |
| 3 | +use argh::{FromArgValue, FromArgs}; |
| 4 | +use dua::ByteFormat; |
| 5 | + |
| 6 | +pub enum CliByteFormat { |
| 7 | + Metric, |
| 8 | + Binary, |
| 9 | + Bytes, |
| 10 | + GB, |
| 11 | + GiB, |
| 12 | + MB, |
| 13 | + MiB, |
| 14 | +} |
| 15 | + |
| 16 | +impl FromArgValue for CliByteFormat { |
| 17 | + fn from_arg_value(value: &str) -> Result<Self, String> { |
| 18 | + use CliByteFormat::*; |
| 19 | + let value_lc = value.to_ascii_lowercase(); |
| 20 | + Ok(match value_lc.as_str() { |
| 21 | + "metric" => Metric, |
| 22 | + "binary" => Binary, |
| 23 | + "bytes" => Bytes, |
| 24 | + "gb" => GB, |
| 25 | + "gib" => GiB, |
| 26 | + "mb" => MB, |
| 27 | + "mib" => MiB, |
| 28 | + _ => return Err(format!("Invalid byte format: {}", value)), |
| 29 | + }) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +impl From<CliByteFormat> for ByteFormat { |
| 34 | + fn from(input: CliByteFormat) -> Self { |
| 35 | + use CliByteFormat::*; |
| 36 | + match input { |
| 37 | + Metric => ByteFormat::Metric, |
| 38 | + Binary => ByteFormat::Binary, |
| 39 | + Bytes => ByteFormat::Bytes, |
| 40 | + GB => ByteFormat::GB, |
| 41 | + GiB => ByteFormat::GiB, |
| 42 | + MB => ByteFormat::MB, |
| 43 | + MiB => ByteFormat::MiB, |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/// a tool to learn about disk usage, fast! |
| 49 | +#[derive(FromArgs)] |
| 50 | +#[argh(name = "dua")] |
| 51 | +pub struct Args { |
| 52 | + #[argh(subcommand)] |
| 53 | + pub command: Option<Command>, |
| 54 | + |
| 55 | + /// the amount of threads to use. Defaults to the amount of logical processors. |
| 56 | + /// Set to 1 to use only a single thread. |
| 57 | + #[argh(option, short = 't')] |
| 58 | + pub threads: Option<usize>, |
| 59 | + |
| 60 | + /// the format with which to print byte counts. |
| 61 | + /// Metric - uses 1000 as base (default) |
| 62 | + /// Binary - uses 1024 as base |
| 63 | + /// Bytes - plain bytes without any formatting |
| 64 | + /// GB - only gigabytes |
| 65 | + /// GiB - only gibibytes |
| 66 | + /// MB - only megabytes |
| 67 | + /// MiB - only mebibytes |
| 68 | + #[argh(option, short = 'f')] |
| 69 | + pub format: Option<CliByteFormat>, |
| 70 | + |
| 71 | + /// display apparent size instead of disk usage. |
| 72 | + #[argh(switch, short = 'A')] |
| 73 | + pub apparent_size: bool, |
| 74 | + |
| 75 | + /// count hard-linked files each time they are seen |
| 76 | + #[argh(switch, short = 'l')] |
| 77 | + pub count_hard_links: bool, |
| 78 | + |
| 79 | + /// if set, we will not cross filesystems or traverse mount points |
| 80 | + #[argh(switch, short = 'x')] |
| 81 | + pub stay_on_filesystem: bool, |
| 82 | + |
| 83 | + /// one or more input files or directories. If unset, we will use all entries in the current working directory. |
| 84 | + #[argh(positional)] |
| 85 | + pub input: Vec<PathBuf>, |
| 86 | +} |
| 87 | + |
| 88 | +#[derive(FromArgs)] |
| 89 | +#[argh(subcommand)] |
| 90 | +pub enum Command { |
| 91 | + Interactive(Interactive), |
| 92 | + InteractiveAlias(InteractiveAlias), |
| 93 | + Aggregate(Aggregate), |
| 94 | +} |
| 95 | + |
| 96 | +/// Launch the terminal user interface |
| 97 | +#[derive(FromArgs)] |
| 98 | +#[argh(subcommand, name = "interactive")] |
| 99 | +pub struct Interactive { |
| 100 | + /// one or more input files or directories. If unset, we will use all entries in the current working directory. |
| 101 | + #[argh(positional)] |
| 102 | + input: Vec<PathBuf>, |
| 103 | +} |
| 104 | + |
| 105 | +/// Alias for 'interactive' |
| 106 | +#[derive(FromArgs)] |
| 107 | +#[argh(subcommand, name = "i")] |
| 108 | +pub struct InteractiveAlias { |
| 109 | + #[argh(positional)] |
| 110 | + input: Vec<PathBuf>, |
| 111 | +} |
| 112 | + |
| 113 | +/// Aggregate the consumed space of one or more directories or files |
| 114 | +#[derive(FromArgs)] |
| 115 | +#[argh(subcommand, name = "aggregate")] |
| 116 | +pub struct Aggregate { |
| 117 | + /// if set, print additional statistics about the file traversal to stderr |
| 118 | + #[argh(switch)] |
| 119 | + stats: bool, |
| 120 | + /// if set, paths will be printed in their order of occurrence on the command-line. |
| 121 | + /// Otherwise they are sorted by their size in bytes, ascending. |
| 122 | + #[argh(switch)] |
| 123 | + no_sort: bool, |
| 124 | + /// if set, no total column will be computed for multiple inputs |
| 125 | + #[argh(switch)] |
| 126 | + no_total: bool, |
| 127 | + /// one or more input files or directories. If unset, we will use all entries in the current working directory. |
| 128 | + #[argh(positional)] |
| 129 | + input: Vec<PathBuf>, |
| 130 | +} |
0 commit comments