look for the widest number instead of trying to sort the files
This commit is contained in:
parent
16619fb7f7
commit
4f7ecbf9ff
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -1,3 +1,5 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "aho-corasick"
|
name = "aho-corasick"
|
||||||
version = "0.6.9"
|
version = "0.6.9"
|
||||||
|
64
src/lib.rs
64
src/lib.rs
@ -40,7 +40,6 @@ impl Opt {
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Page {
|
struct Page {
|
||||||
position: Option<u32>, // FIXME: is it usefull or not ?
|
|
||||||
prefix: String,
|
prefix: String,
|
||||||
number: String,
|
number: String,
|
||||||
suffix: String,
|
suffix: String,
|
||||||
@ -50,30 +49,24 @@ impl Page {
|
|||||||
fn new(prefix: String, number: String, suffix: String) -> Page {
|
fn new(prefix: String, number: String, suffix: String) -> Page {
|
||||||
println!("N p:{} n: {} s: {}", prefix, number, suffix);
|
println!("N p:{} n: {} s: {}", prefix, number, suffix);
|
||||||
Page {
|
Page {
|
||||||
position: None,
|
prefix,
|
||||||
prefix: prefix,
|
number,
|
||||||
number: number,
|
suffix,
|
||||||
suffix: suffix,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_position(&mut self, pos: u32) {
|
|
||||||
self.position = Some(pos);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn original_filename(&self) -> String {
|
fn original_filename(&self) -> String {
|
||||||
println!("OF p:{} n: {} s: {}", self.prefix, self.number, self.suffix);
|
println!("OF p:{} n: {} s: {}", self.prefix, self.number, self.suffix);
|
||||||
format!("{}{}{}", self.prefix, self.number, self.suffix)
|
format!("{}{}{}", self.prefix, self.number, self.suffix)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new_filename(&self, number: &str, pad: usize) -> String {
|
fn new_filename(&self, pad: usize) -> String {
|
||||||
println!(
|
println!(
|
||||||
"NF n: '{}' p: '{}' n: '{}' s: '{}'",
|
"NF p: '{}' n: '{}' s: '{}'",
|
||||||
number, self.prefix, self.number, self.suffix
|
self.prefix, self.number, self.suffix
|
||||||
);
|
);
|
||||||
// format!("{:0pad$}-{}{}{}", number, self.prefix, self.number, self.suffix, pad = pad)
|
// format!("{:0pad$}-{}{}{}", number, self.prefix, self.number, self.suffix, pad = pad)
|
||||||
// TODO I think I will need this on in the end
|
// TODO I think I will need this on in the end
|
||||||
// FIXME position is bad
|
|
||||||
format!(
|
format!(
|
||||||
"{}{:0pad$}{}",
|
"{}{:0pad$}{}",
|
||||||
self.prefix,
|
self.prefix,
|
||||||
@ -85,14 +78,15 @@ impl Page {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct ComicBook {
|
pub struct ComicBook {
|
||||||
|
pad: Option<u64>,
|
||||||
pages: Vec<Page>,
|
pages: Vec<Page>,
|
||||||
is_binded: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ComicBook {
|
impl ComicBook {
|
||||||
pub fn new(files: ReadDir) -> ComicBook {
|
pub fn new(files: ReadDir) -> ComicBook {
|
||||||
let regex = Regex::new(r"\./(?P<prefix>\D*)(?P<number>\d*)(?P<suffix>.*)").unwrap();
|
let regex = Regex::new(r"\./(?P<prefix>\D*)(?P<number>\d*)(?P<suffix>.*)").unwrap();
|
||||||
ComicBook {
|
ComicBook {
|
||||||
|
pad: None,
|
||||||
pages: files
|
pages: files
|
||||||
.map(|entry| entry.unwrap().path())
|
.map(|entry| entry.unwrap().path())
|
||||||
.filter(|entry| entry.is_file())
|
.filter(|entry| entry.is_file())
|
||||||
@ -106,48 +100,32 @@ impl ComicBook {
|
|||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
//~ book: path.iter().map(|name| Page::new(name)).collect(),
|
//~ book: path.iter().map(|name| Page::new(name)).collect(),
|
||||||
is_binded: false,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn bind(&mut self) {
|
pub fn bind(&mut self) {
|
||||||
self.sort();
|
|
||||||
let pad = self.get_pad_size().unwrap();
|
let pad = self.get_pad_size().unwrap();
|
||||||
|
|
||||||
for page in self.pages.iter() {
|
for page in self.pages.iter() {
|
||||||
if let Some(pos) = page.position {
|
//if let Some(pos) = page.position {
|
||||||
let original_file = page.original_filename();
|
let original_file = page.original_filename();
|
||||||
let new_file = page.new_filename(&pos.to_string(), dbg!(pad));
|
let new_file = page.new_filename(dbg!(pad));
|
||||||
|
|
||||||
println!("{} -> {}", original_file, dbg!(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");
|
||||||
}
|
//}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn sort(&mut self) {
|
|
||||||
//~ self.book.sort();
|
|
||||||
self.pages.sort_by(|a, b| {
|
|
||||||
a.number
|
|
||||||
.parse::<u32>()
|
|
||||||
.unwrap()
|
|
||||||
.cmp(&b.number.parse::<u32>().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 u32);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_pad_size(&self) -> Option<usize> {
|
fn get_pad_size(&self) -> Option<usize> {
|
||||||
if !self.is_binded {
|
Some(
|
||||||
return None;
|
self.pages
|
||||||
}
|
.iter()
|
||||||
|
.max_by_key(|x| x.number.len())?
|
||||||
Some(self.pages.last().unwrap().number.len())
|
.number
|
||||||
|
.len(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user