Skip to content

Commit

Permalink
[chore] Optimize progress bar display
Browse files Browse the repository at this point in the history
  • Loading branch information
iqiziqi committed Mar 3, 2023
1 parent 59728d0 commit bd45915
Showing 1 changed file with 32 additions and 70 deletions.
102 changes: 32 additions & 70 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ use ncmdump::Ncmdump;
#[cfg(feature = "qmcdump")]
use ncmdump::QmcDump;

const PROGRESS_STYLE_RUN: &str = "[{elapsed_precise:.blue}] [{bar:40.cyan}] {pos:>10!.cyan}/{len:<10!.blue} | {percent:>3!}% | {msg}";
const PROGRESS_STYLE_DUMP: &str = "[{elapsed_precise:.blue}] [{bar:40.cyan}] {bytes:>10!.cyan}/{total_bytes:<10!.blue} | {percent:>3!}% | {bytes_per_sec}";
const PROGRESS_STYLE_BAR: &str = "=> ";
const PROGRESS_STYLE_RUN: &str = "[{bar:40.cyan}] | {percent:>3!}% | {per_sec}";
const PROGRESS_STYLE_DUMP: &str = "[{bar:40.cyan}] | {percent:>3!}% | {msg}";

enum FileType {
#[cfg(feature = "ncmdump")]
Expand All @@ -28,11 +27,11 @@ enum FileType {
#[derive(Clone, Debug, Error)]
enum Error {
#[error("Can't resolve the path")]
PathError,
Path,
#[error("Invalid file format")]
FormatError,
Format,
#[error("No file can be converted")]
NoFileError,
NoFile,
}

#[derive(Debug, Parser)]
Expand All @@ -46,10 +45,6 @@ struct Command {
/// Default it's the same directory with input file.
#[arg(short = 'o', long = "output")]
output: Option<String>,

/// Verbosely list files processing.
#[arg(short = 'v', long = "verbose")]
verbose: bool,
}

struct Wrapper {
Expand Down Expand Up @@ -106,10 +101,10 @@ impl NcmdumpCli {
output: &Option<String>,
) -> Result<PathBuf> {
let parent = match output {
None => file_path.parent().ok_or(Error::PathError)?,
None => file_path.parent().ok_or(Error::Path)?,
Some(p) => Path::new(p),
};
let file_name = file_path.file_stem().ok_or(Error::PathError)?;
let file_name = file_path.file_stem().ok_or(Error::Path)?;
let path = parent.join(file_name).with_extension(format);
Ok(path)
}
Expand All @@ -131,19 +126,16 @@ impl NcmdumpCli {
Ok(paths)
}

fn get_info(&self, paths: Vec<PathBuf>, progress: &ProgressBar) -> Vec<Wrapper> {
fn get_info(&self, paths: Vec<PathBuf>) -> Vec<Wrapper> {
let mut result = Vec::new();
for path in paths {
progress.set_message(path.file_name().unwrap().to_str().unwrap().to_string());
if let Ok(item) = Wrapper::from_path(path) {
match item.format {
FileType::Other => {}
_ => result.push(item),
}
};
progress.inc(1)
}
progress.finish();
result
}

Expand All @@ -168,12 +160,12 @@ impl NcmdumpCli {
FileType::Ncm => self.get_data(Ncmdump::from_reader(file)?, progress),
#[cfg(feature = "qmcdump")]
FileType::Qmc => self.get_data(QmcDump::from_reader(file)?, progress),
FileType::Other => Err(Error::FormatError.into()),
FileType::Other => Err(Error::Format.into()),
}?;
let ext = match data[..4] {
[0x66, 0x4C, 0x61, 0x43] => Ok("flac"),
[0x49, 0x44, 0x33, _] => Ok("mp3"),
_ => Err(Error::FormatError),
_ => Err(Error::Format),
}?;
let output_file = self.get_output(&item.path, ext, &self.command.output)?;
let mut target = File::options().create(true).write(true).open(output_file)?;
Expand All @@ -183,68 +175,38 @@ impl NcmdumpCli {

fn start(&self) -> Result<()> {
if self.command.matchers.is_empty() {
return Err(Error::NoFileError.into());
return Err(Error::NoFile.into());
}
let paths = self.get_paths()?;
let items = self.get_info(paths);
if items.is_empty() {
return Err(Error::NoFile.into());
}

let progress_style_run =
ProgressStyle::with_template(PROGRESS_STYLE_RUN)?.progress_chars(PROGRESS_STYLE_BAR);
let progress_style_dump =
ProgressStyle::with_template(PROGRESS_STYLE_DUMP)?.progress_chars(PROGRESS_STYLE_BAR);
let progress_style_run = ProgressStyle::with_template(PROGRESS_STYLE_RUN)?;
let progress_style_dump = ProgressStyle::with_template(PROGRESS_STYLE_DUMP)?;

let paths = self.get_paths()?;
let progress_info = self
let progress_run = self
.progress
.add(ProgressBar::new(paths.len() as u64))
.with_style(progress_style_run.clone());
let items = self.get_info(paths, &progress_info);

match items.len() {
0 => return Err(Error::NoFileError.into()),
1 => {
let item = items.get(0).unwrap();
let progress = self
.progress
.add(ProgressBar::new(item.size).with_style(progress_style_dump));
self.dump(item, &progress)?;
if self.command.verbose {
self.progress
.println(format!("Converting file {}\t complete!", item.name))?;
}
}
_ => {
let progress_run = self
.progress
.add(ProgressBar::new(items.len() as u64).with_style(progress_style_run));
let progress_dump = self
.progress
.add(ProgressBar::new(1).with_style(progress_style_dump));

for item in items {
progress_run.set_message(item.name.clone());
progress_dump.reset();
progress_dump.set_length(item.size);
match self.dump(&item, &progress_dump) {
Ok(_) => {
if self.command.verbose {
self.progress.println(format!(
"Converting file {}\t complete!",
item.name
))?;
}
progress_run.inc(1);
}
Err(e) => println!("{:?}", e),
}
}
progress_run.finish();
.add(ProgressBar::new(items.len() as u64).with_style(progress_style_run));

for item in items {
let current = self.progress.insert(
0,
ProgressBar::new(item.size).with_style(progress_style_dump.clone()),
);
current.set_message(item.name.clone());
match self.dump(&item, &current) {
Ok(_) => progress_run.inc(1),
Err(e) => println!("{:?}", e),
}
}
progress_run.finish();

Ok(())
}
}

fn main() -> Result<()> {
let app = NcmdumpCli::from_command(Command::parse());
app.start()
NcmdumpCli::from_command(Command::parse()).start()
}

0 comments on commit bd45915

Please sign in to comment.