summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Thiel <sebastian.thiel@icloud.com>2020-07-02 07:45:53 +0800
committerSebastian Thiel <sebastian.thiel@icloud.com>2020-07-02 07:53:46 +0800
commitd787a9c5b8ccadae678c985b05ecc328d62df8f3 (patch)
tree52ecd89e02b0e4e727be5963b58a0888e95b646f
parent8040d5c50df32b6b19b775a88bdc9616fbfe8980 (diff)
First version of options struct based on Argh
-rw-r--r--Cargo.lock30
-rw-r--r--Cargo.toml1
-rw-r--r--src/main.rs1
-rw-r--r--src/options_argh.rs130
4 files changed, 162 insertions, 0 deletions
diff --git a/Cargo.lock b/Cargo.lock
index f187d42..03d7d4e 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -25,6 +25,35 @@ dependencies = [
]
[[package]]
+name = "argh"
+version = "0.1.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ca1877e24cecacd700d469066e0160c4f8497cc5635367163f50c8beec820154"
+dependencies = [
+ "argh_derive",
+ "argh_shared",
+]
+
+[[package]]
+name = "argh_derive"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e742194e0f43fc932bcb801708c2b279d3ec8f527e3acda05a6a9f342c5ef764"
+dependencies = [
+ "argh_shared",
+ "heck",
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "argh_shared"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e1ba68f4276a778591e36a0c348a269888f3a177c8d2054969389e3b59611ff5"
+
+[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -186,6 +215,7 @@ checksum = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198"
name = "dua-cli"
version = "2.7.0"
dependencies = [
+ "argh",
"atty",
"byte-unit",
"failure",
diff --git a/Cargo.toml b/Cargo.toml
index 189651e..0e1c6a6 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -13,6 +13,7 @@ include = ["src/**/*", "Cargo.*", "LICENSE", "README.md", "CHANGELOG.md", "!**/*
failure = "0.1.1"
failure-tools = "4.0.2"
structopt = "0.3"
+argh = "0.1.3"
jwalk = "0.5.0"
byte-unit = "4"
termion = "1.5.2"
diff --git a/src/main.rs b/src/main.rs
index 2a3936a..0eb80a6 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -16,6 +16,7 @@ use tui_react::Terminal;
mod interactive;
mod options;
+mod options_argh;
fn run() -> Result<(), Error> {
use options::Command::*;
diff --git a/src/options_argh.rs b/src/options_argh.rs
new file mode 100644
index 0000000..688931b
--- /dev/null
+++ b/src/options_argh.rs
@@ -0,0 +1,130 @@
+use std::path::PathBuf;
+
+use argh::{FromArgValue, FromArgs};
+use dua::ByteFormat;
+
+pub enum CliByteFormat {
+ Metric,
+ Binary,
+ Bytes,
+ GB,
+ GiB,
+ MB,
+ MiB,
+}
+
+impl FromArgValue for CliByteFormat {
+ fn from_arg_value(value: &str) -> Result<Self, String> {
+ use CliByteFormat::*;
+ let value_lc = value.to_ascii_lowercase();
+ Ok(match value_lc.as_str() {
+ "metric" => Metric,
+ "binary" => Binary,
+ "bytes" => Bytes,
+ "gb" => GB,
+ "gib" => GiB,
+ "mb" => MB,
+ "mib" => MiB,
+ _ => return Err(format!("Invalid byte format: {}", value)),
+ })
+ }
+}
+
+impl From<CliByteFormat> for ByteFormat {
+ fn from(input: CliByteFormat) -> Self {
+ use CliByteFormat::*;
+ match input {
+ Metric => ByteFormat::Metric,
+ Binary => ByteFormat::Binary,
+ Bytes => ByteFormat::Bytes,
+ GB => ByteFormat::GB,
+ GiB => ByteFormat::GiB,
+ MB => ByteFormat::MB,
+ MiB => ByteFormat::MiB,
+ }
+ }
+}
+
+/// a tool to learn about disk usage, fast!
+#[derive(FromArgs)]
+#[argh(name = "dua")]
+pub struct Args {
+ #[argh(subcommand)]
+ pub command: Option<Command>,
+
+ /// the amount of threads to use. Defaults to the amount of logical processors.
+ /// Set to 1 to use only a single thread.
+ #[argh(option, short = 't')]
+ pub threads: Option<usize>,
+
+ /// the format with which to print byte counts.
+ /// Metric - uses 1000 as base (default)
+ /// Binary - uses 1024 as base
+ /// Bytes - plain bytes without any formatting
+ /// GB - only gigabytes
+ /// GiB - only gibibytes
+ /// MB - only megabytes
+ /// MiB - only mebibytes
+ #[argh(option, short = 'f')]
+ pub format: Option<CliByteFormat>,
+
+ /// display apparent size instead of disk usage.
+ #[argh(switch, short = 'A')]
+ pub apparent_size: bool,
+
+ /// count hard-linked files each time they are seen
+ #[argh(switch, short = 'l')]
+ pub count_hard_links: bool,
+
+ /// if set, we will not cross filesystems or traverse mount points
+ #[argh(switch, short = 'x')]
+ pub stay_on_filesystem: bool,
+
+ /// one or more input files or directories. If unset, we will use all entries in the current working directory.
+ #[argh(positional)]
+ pub input: Vec<PathBuf>,
+}
+
+#[derive(FromArgs)]
+#[argh(subcommand)]
+pub enum Command {
+ Interactive(Interactive),
+ InteractiveAlias(InteractiveAlias),
+ Aggregate(Aggregate),
+}
+
+/// Launch the terminal user interface
+#[derive(FromArgs)]
+#[argh(subcommand, name = "interactive")]
+pub struct Interactive {
+ /// one or more input files or directories. If unset, we will use all entries in the current working directory.
+ #[argh(positional)]
+ input: Vec<PathBuf>,
+}
+
+/// Alias for 'interactive'
+#[derive(FromArgs)]
+#[argh(subcommand, name = "i")]
+pub struct InteractiveAlias {
+ #[argh(positional)]
+ input: Vec<PathBuf>,
+}
+
+/// Aggregate the consumed space of one or more directories or files
+#[derive(FromArgs)]
+#[argh(subcommand, name = "aggregate")]
+pub struct Aggregate {
+ /// if set, print additional statistics about the file traversal to stderr
+ #[argh(switch)]
+ stats: bool,
+ /// if set, paths will be printed in their order of occurrence on the command-line.
+ /// Otherwise they are sorted by their size in bytes, ascending.
+ #[argh(switch)]
+ no_sort: bool,
+ /// if set, no total column will be computed for multiple inputs
+ #[argh(switch)]
+ no_total: bool,
+ /// one or more input files or directories. If unset, we will use all entries in the current working directory.
+ #[argh(positional)]
+ input: Vec<PathBuf>,
+}