about summary refs log tree commit diff stats
path: root/src/cli.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/cli.rs')
-rw-r--r--src/cli.rs47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/cli.rs b/src/cli.rs
index f3f4b7e..a61a57e 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -10,6 +10,7 @@
 
 use std::path::PathBuf;
 
+use anyhow::{bail, Error};
 use chrono::NaiveDate;
 use clap::{ArgAction, Args, Parser, Subcommand};
 use url::Url;
@@ -47,6 +48,11 @@ pub enum Command {
         /// Forcefully re-download all cached videos (i.e. delete the cache path, then download).
         #[arg(short, long)]
         force: bool,
+
+        /// The maximum size the download dir should have. Beware that the value must be given in
+        /// bytes.
+        #[arg(short, long, default_value = "3 GiB", value_parser = byte_parser)]
+        max_cache_size: u64,
     },
 
     /// Watch the already cached (and selected) videos
@@ -102,6 +108,47 @@ pub enum Command {
     },
 }
 
+fn byte_parser(s: &str) -> Result<u64, Error> {
+    const B: u64 = 1;
+
+    const KIB: u64 = 1024 * B;
+    const MIB: u64 = 1024 * KIB;
+    const GIB: u64 = 1024 * MIB;
+
+    const KB: u64 = 1000 * B;
+    const MB: u64 = 1000 * KB;
+    const GB: u64 = 1000 * MB;
+
+    let s = s
+        .chars()
+        .filter(|elem| !elem.is_whitespace())
+        .collect::<String>();
+
+    let number: u64 = s
+        .chars()
+        .take_while(|x| x.is_numeric())
+        .collect::<String>()
+        .parse()?;
+    let extension = s.chars().skip_while(|x| x.is_numeric()).collect::<String>();
+
+    let output = match extension.to_lowercase().as_str() {
+        "" => number,
+        "b" => number * B,
+        "kib" => number * KIB,
+        "mib" => number * MIB,
+        "gib" => number * GIB,
+        "kb" => number * KB,
+        "mb" => number * MB,
+        "gb" => number * GB,
+        other => bail!(
+            "Your extension '{}' is not yet supported. Only KB,MB,GB or KiB,MiB,GiB are supported",
+            other
+        ),
+    };
+
+    Ok(output)
+}
+
 impl Default for Command {
     fn default() -> Self {
         Self::Select {