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]]
|
||||
name = "aho-corasick"
|
||||
version = "0.6.9"
|
||||
|
64
src/lib.rs
64
src/lib.rs
@ -40,7 +40,6 @@ impl Opt {
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Page {
|
||||
position: Option<u32>, // FIXME: is it usefull or not ?
|
||||
prefix: String,
|
||||
number: String,
|
||||
suffix: String,
|
||||
@ -50,30 +49,24 @@ impl Page {
|
||||
fn new(prefix: String, number: String, suffix: String) -> Page {
|
||||
println!("N p:{} n: {} s: {}", prefix, number, suffix);
|
||||
Page {
|
||||
position: None,
|
||||
prefix: prefix,
|
||||
number: number,
|
||||
suffix: suffix,
|
||||
prefix,
|
||||
number,
|
||||
suffix,
|
||||
}
|
||||
}
|
||||
|
||||
fn set_position(&mut self, pos: u32) {
|
||||
self.position = Some(pos);
|
||||
}
|
||||
|
||||
fn original_filename(&self) -> String {
|
||||
println!("OF p:{} n: {} s: {}", 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!(
|
||||
"NF n: '{}' p: '{}' n: '{}' s: '{}'",
|
||||
number, self.prefix, self.number, self.suffix
|
||||
"NF p: '{}' n: '{}' s: '{}'",
|
||||
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,
|
||||
@ -85,14 +78,15 @@ impl Page {
|
||||
}
|
||||
|
||||
pub struct ComicBook {
|
||||
pad: Option<u64>,
|
||||
pages: Vec<Page>,
|
||||
is_binded: bool,
|
||||
}
|
||||
|
||||
impl ComicBook {
|
||||
pub fn new(files: ReadDir) -> ComicBook {
|
||||
let regex = Regex::new(r"\./(?P<prefix>\D*)(?P<number>\d*)(?P<suffix>.*)").unwrap();
|
||||
ComicBook {
|
||||
pad: None,
|
||||
pages: files
|
||||
.map(|entry| entry.unwrap().path())
|
||||
.filter(|entry| entry.is_file())
|
||||
@ -106,48 +100,32 @@ impl ComicBook {
|
||||
})
|
||||
.collect(),
|
||||
//~ book: path.iter().map(|name| Page::new(name)).collect(),
|
||||
is_binded: false,
|
||||
}
|
||||
}
|
||||
|
||||
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(), dbg!(pad));
|
||||
//if let Some(pos) = page.position {
|
||||
let original_file = page.original_filename();
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
// fs::rename(original_file, new_file).expect("RENAME FAILED");
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_pad_size(&self) -> Option<usize> {
|
||||
if !self.is_binded {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(self.pages.last().unwrap().number.len())
|
||||
Some(
|
||||
self.pages
|
||||
.iter()
|
||||
.max_by_key(|x| x.number.len())?
|
||||
.number
|
||||
.len(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user