From 16619fb7f78d8ea03fd9b6e32f8de92c05eb18fd Mon Sep 17 00:00:00 2001 From: Zykino Date: Fri, 5 Apr 2019 17:51:32 +0200 Subject: [PATCH] add structopt as dependance --- Cargo.lock | 189 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/lib.rs | 96 +++++++++++++++++++------- src/main.rs | 17 +++-- 4 files changed, 274 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b327c97..9d4b589 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,16 +6,62 @@ dependencies = [ "memchr 2.1.3 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "ansi_term" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "atty" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "bitflags" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "cfg-if" version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "clap" +version = "2.32.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", + "atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "comic-book-binder" version = "0.1.0" dependencies = [ "regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "structopt 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "heck" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-segmentation 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -37,6 +83,35 @@ dependencies = [ "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "proc-macro2" +version = "0.4.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "quote" +version = "0.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "redox_syscall" +version = "0.1.51" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "redox_termios" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "redox_syscall 0.1.51 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "regex" version = "1.1.0" @@ -57,6 +132,59 @@ dependencies = [ "ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "strsim" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "structopt" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)", + "structopt-derive 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "structopt-derive" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "syn" +version = "0.15.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "termion" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.51 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "textwrap" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "thread_local" version = "0.3.6" @@ -70,19 +198,80 @@ name = "ucd-util" version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "unicode-segmentation" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "unicode-width" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "unicode-xid" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "utf8-ranges" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "vec_map" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [metadata] "checksum aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1e9a933f4e58658d7b12defcf96dc5c720f20832deebe3e0a19efd3b6aaeeb9e" +"checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +"checksum atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9a7d5b8723950951411ee34d271d99dddcc2035a16ab25310ea2c8cfd4369652" +"checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12" "checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4" +"checksum clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b957d88f4b6a63b9d70d5f454ac8011819c6efa7727858f458ab71c756ce2d3e" +"checksum heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205" "checksum lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1" "checksum libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)" = "e962c7641008ac010fa60a7dfdc1712449f29c44ef2d4702394aea943ee75047" "checksum memchr 2.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e1dd4eaac298c32ce07eb6ed9242eda7d82955b9170b7d6db59b2e02cc63fcb8" +"checksum proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)" = "4d317f9caece796be1980837fd5cb3dfec5613ebdb04ad0956deea83ce168915" +"checksum quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)" = "cdd8e04bd9c52e0342b406469d494fcb033be4bdbe5c606016defbb1681411e1" +"checksum redox_syscall 0.1.51 (registry+https://github.com/rust-lang/crates.io-index)" = "423e376fffca3dfa06c9e9790a9ccd282fafb3cc6e6397d01dbf64f9bacc6b85" +"checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" "checksum regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "37e7cbbd370869ce2e8dff25c7018702d10b21a20ef7135316f8daecd6c25b7f" "checksum regex-syntax 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "8c2f35eedad5295fdf00a63d7d4b238135723f92b434ec06774dad15c7ab0861" +"checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550" +"checksum structopt 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "670ad348dc73012fcf78c71f06f9d942232cdd4c859d4b6975e27836c3efc0c3" +"checksum structopt-derive 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "ef98172b1a00b0bec738508d3726540edcbd186d50dfd326f2b1febbb3559f04" +"checksum syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)" = "f92e629aa1d9c827b2bb8297046c1ccffc57c99b947a680d3ccff1f136a3bee9" +"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" +"checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6" "checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" "checksum ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "535c204ee4d8434478593480b8f86ab45ec9aae0e83c568ca81abf0fd0e88f86" +"checksum unicode-segmentation 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "aa6024fc12ddfd1c6dbc14a80fa2324d4568849869b779f6bd37e5e4c03344d1" +"checksum unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526" +"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" "checksum utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "796f7e48bef87609f7ade7e06495a87d5cd06c7866e6a5cbfceffc558a243737" +"checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" +"checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0" +"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml index 929814b..ed394b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,3 +13,4 @@ path = "src/main.rs" [dependencies] regex = "1" +structopt = "0.2" diff --git a/src/lib.rs b/src/lib.rs index d0424eb..8c4b5e8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,13 +1,46 @@ -extern crate regex; - -use regex::Regex; - -use std::fs::{self, ReadDir}; +use std::ffi::OsString; +use std::fs::ReadDir; use std::path::PathBuf; +use regex::Regex; +use structopt::StructOpt; + +#[derive(Debug, StructOpt)] +#[structopt(rename_all = "kebab-case")] +pub struct Opt { + /// Path of the comic folder to bind + /// + /// If the recursive option is set, consider the folder given as a library containing multiples + /// folders. It will not treat the given folder as a comic in itself. + // TODO: check if I really do this or not when implementing the recursive option + pub comic_folder: PathBuf, + + // TODO: implement the recursivity + /// Recursively treat each child folder as a it's own comic to bind + #[structopt(short, long)] + pub recursive: bool, + // TODO: implement the prefix + // /// Prefix to use for the files + // /// + // /// This is usefull when the images contains a number before their page number + // /// For example: when the title contains the name of the comic with its season: fooS02-42.png + // #[structopt(short, long)] + // pub prefix: &str, +} + +impl Opt { + pub fn new(args: I) -> Self + where + I: IntoIterator, + T: Into + Clone, + { + Opt::from_iter(args) + } +} + #[derive(Debug)] -pub struct Page { - position: Option, +struct Page { + position: Option, // FIXME: is it usefull or not ? prefix: String, number: String, suffix: String, @@ -24,7 +57,7 @@ impl Page { } } - fn set_position(&mut self, pos: u64) { + fn set_position(&mut self, pos: u32) { self.position = Some(pos); } @@ -33,11 +66,21 @@ impl Page { format!("{}{}{}", self.prefix, self.number, self.suffix) } - fn new_filename(&self, number: &str) -> String { - println!("NF n: {} p:{} n: {} s: {}", number, self.prefix, self.number, self.suffix); - format!("{}-{}{}{}", number, self.prefix, self.number, self.suffix) - //~ TODO I think I will need this on in the end - //~ format!("{}{}{}", self.prefix, number, self.suffix) + fn new_filename(&self, number: &str, pad: usize) -> String { + println!( + "NF n: '{}' p: '{}' n: '{}' s: '{}'", + number, self.prefix, self.number, self.suffix + ); + // format!("{:0pad$}-{}{}{}", number, self.prefix, self.number, self.suffix, pad = pad) + // TODO I think I will need this on in the end + // FIXME position is bad + format!( + "{}{:0pad$}{}", + self.prefix, + self.number.parse::().unwrap(), + self.suffix, + pad = pad + ) } } @@ -57,7 +100,7 @@ impl ComicBook { let caps = regex.captures(entry.to_str().unwrap()).unwrap(); Page::new( String::from(caps.name("prefix").map_or("", |c| c.as_str())), - String::from(&caps["number"]), // FIXME => ignore the file? + String::from(&caps["number"]), // FIXME => ignore the file? String::from(caps.name("suffix").map_or("", |c| c.as_str())), ) }) @@ -69,19 +112,16 @@ impl ComicBook { pub fn bind(&mut self) { self.sort(); + let pad = self.get_pad_size().unwrap(); for page in self.pages.iter() { if let Some(pos) = page.position { let original_file = page.original_filename(); - let new_file = page.new_filename(&pos.to_string()); + let new_file = page.new_filename(&pos.to_string(), dbg!(pad)); - println!("{} X {}", original_file, new_file); + println!("{} -> {}", original_file, dbg!(new_file)); - fs::rename( - original_file, - new_file, - ) - .expect("RENAME FAILED"); + // fs::rename(original_file, new_file).expect("RENAME FAILED"); } } } @@ -90,17 +130,25 @@ impl ComicBook { //~ self.book.sort(); self.pages.sort_by(|a, b| { a.number - .parse::() + .parse::() .unwrap() - .cmp(&b.number.parse::().unwrap()) + .cmp(&b.number.parse::().unwrap()) }); self.is_binded = true; // FIXME: Do not use enumerate but the parsed value: there may be gaps in the collection for (i, page) in self.pages.iter_mut().enumerate() { - page.set_position(i as u64); + page.set_position(i as u32); } } + + fn get_pad_size(&self) -> Option { + if !self.is_binded { + return None; + } + + Some(self.pages.last().unwrap().number.len()) + } } #[cfg(test)] diff --git a/src/main.rs b/src/main.rs index 4c34398..9d53e02 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,24 @@ use cbb; use cbb::ComicBook; +use cbb::Opt; -use std::fs; use std::env; +use std::fs; fn main() { - // TODO: get the dir from the args - let dir = "examples-test/int"; + let opt = Opt::new(env::args()); + dbg!(&opt); - env::set_current_dir(dir).unwrap(); + if opt.recursive + /*|| opt.prefix*/ + { + unimplemented!(); + } + + env::set_current_dir(opt.comic_folder).unwrap(); let mut book = ComicBook::new(fs::read_dir(".").unwrap()); book.bind(); - //~ println!("{:?}", book); + // dbg!(book); }