feat: Improve UI by printing errors in a separate queue, on exit and small touch-ups (#101)

This commit is contained in:
grufkork 2024-09-04 17:22:38 +02:00 committed by GitHub
parent 4e2905f370
commit f0a8720853
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -155,6 +155,7 @@ async fn start() {
let mut download_states = let mut download_states =
vec![DownloadState::None; downloader.get_downloads().await.len()]; vec![DownloadState::None; downloader.get_downloads().await.len()];
let mut messages = vec![]; let mut messages = vec![];
let mut errors = vec![];
'outer: loop { 'outer: loop {
print!("\x1b[2J\x1b[1;1H"); print!("\x1b[2J\x1b[1;1H");
@ -162,6 +163,7 @@ async fn start() {
let mut num_completed = 0; let mut num_completed = 0;
let mut num_err = 0; let mut num_err = 0;
let mut num_skipped = 0;
let mut num_downloading = 0; let mut num_downloading = 0;
let mut num_waiting = 0; let mut num_waiting = 0;
@ -180,19 +182,28 @@ async fn start() {
DownloadState::Downloading(_, _) => (), DownloadState::Downloading(_, _) => (),
DownloadState::Post => (), DownloadState::Post => (),
DownloadState::Done => messages.push(format!( DownloadState::Done => messages.push(format!(
"{time_elapsed: >5} | {}: {}", " {} | {}: {}",
secs_to_hrs_min_sec(time_elapsed as i32),
"Downloaded".green(), "Downloaded".green(),
download.title download.title
)), )),
DownloadState::Error(e) => messages.push(format!( DownloadState::Error(e) => {
"{time_elapsed: >5} | {}: {}", let msg = format!(
" {} | {}: {}",
secs_to_hrs_min_sec(time_elapsed as i32),
if e == &SpotifyError::AlreadyDownloaded { if e == &SpotifyError::AlreadyDownloaded {
e.to_string().yellow() e.to_string().yellow()
} else { } else {
e.to_string().red() e.to_string().red()
}, },
download.title download.title
)), );
if e == &SpotifyError::AlreadyDownloaded {
messages.push(msg);
} else {
errors.push(msg);
}
}
}; };
} }
@ -217,8 +228,13 @@ async fn start() {
num_waiting += 1; num_waiting += 1;
None None
} }
DownloadState::Error(_) => { DownloadState::Error(e) => {
if e == &SpotifyError::AlreadyDownloaded {
num_skipped += 1;
} else {
num_err += 1; num_err += 1;
}
None None
} }
DownloadState::Done => { DownloadState::Done => {
@ -231,16 +247,19 @@ async fn start() {
} }
} }
while messages.len() > 8 { while messages.len() > 10 {
messages.remove(0); messages.remove(0);
} }
println!(" {bold}\x1b[0;34m- DownOnSpot v{VERSION} -\x1b[0m{bold_off}\n"); println!(" {bold}\x1b[0;34m- DownOnSpot v{VERSION} -\x1b[0m{bold_off}\n");
println!("Time elapsed: {}", secs_to_min_sec(time_elapsed as i32)); println!(
"Time elapsed: {}",
secs_to_hrs_min_sec(time_elapsed as i32)
);
println!( println!(
"Time remaining: {}\n", "Time remaining: {}\n",
secs_to_min_sec( secs_to_hrs_min_sec(
(time_elapsed as f32 (time_elapsed as f32
/ (progress_sum + num_completed as f32 + num_err as f32) / (progress_sum + num_completed as f32 + num_err as f32)
* (num_waiting as f32 + num_downloading as f32 - progress_sum)) * (num_waiting as f32 + num_downloading as f32 - progress_sum))
@ -253,21 +272,36 @@ async fn start() {
"Time".underline(), "Time".underline(),
"Event".underline() "Event".underline()
); );
for message in &messages { for message in messages.iter().rev() {
println!("{}", message); println!("{}", message);
} }
if !errors.is_empty() {
println!(
"\n {bold} {} {}{bold_off}",
"Time".underline(),
"Error".underline()
);
for error in errors.iter().rev().take(5) {
println!("{}", error);
}
}
println!("\n\n {}", "Current downloads:".underline().bold()); println!("\n\n {}", "Current downloads:".underline().bold());
println!("{}", current_download_view); println!("{}", current_download_view);
println!( println!(
"\n {bold}Waiting | {} | {} | Total{bold_off}", "\n{bold}{}|{}|{}|{}| Total{bold_off}",
"Err/Skip".red(), " Waiting ",
" Failed ".red(),
" Skipped ".yellow(),
" Done ".green() " Done ".green()
); );
println!( println!(
" {: <8}| {: <9}| {: <5}| {}", " {: <8}| {: <8}| {: <8}| {: <8}| {}",
num_waiting, num_waiting,
num_err, num_err,
num_skipped,
num_completed, num_completed,
download_states.len() download_states.len()
); );
@ -279,10 +313,18 @@ async fn start() {
task::sleep(refresh).await task::sleep(refresh).await
} }
println!( println!(
"Finished download(s) in {}.", "Finished download(s) in {}.",
secs_to_min_sec(time_elapsed as i32) secs_to_hrs_min_sec(time_elapsed as i32)
); );
if !errors.is_empty() {
println!("\n\n All Errors:");
for error in errors {
println!("{}", error);
}
}
} }
Err(e) => { Err(e) => {
error!("{} {}", "Handling input failed:".red(), e) error!("{} {}", "Handling input failed:".red(), e)
@ -290,6 +332,8 @@ async fn start() {
} }
} }
fn secs_to_min_sec(secs: i32) -> String { fn secs_to_hrs_min_sec(secs: i32) -> String {
format!("{:0>2}m{:0>2}s", secs / 60, secs % 60) format!("{:0>2}:{:0>2}:{:0>2}", secs / 360, secs / 60, secs % 60)
} }
// !cargo b --release