diff options
author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2024-10-29 17:31:01 +0100 |
---|---|---|
committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2024-10-29 17:31:01 +0100 |
commit | 5c7a16758c956d1ff30e68f497a07bfeefae22bb (patch) | |
tree | 327f962a452acf942e6f9cfb6a53c075b8ab4b02 /crates/yt_dlp/src | |
parent | fix(yt_dlp/lib/progress_hook): Estimate `total_byte_size` better (diff) | |
download | yt-5c7a16758c956d1ff30e68f497a07bfeefae22bb.tar.gz yt-5c7a16758c956d1ff30e68f497a07bfeefae22bb.zip |
fix(yt_dlp/lib/progress_hook): Avoid overriding previous messages
Otherwise, the hook would simply cancel the already printed line.
Diffstat (limited to '')
-rw-r--r-- | crates/yt_dlp/src/lib.rs | 31 |
1 files changed, 23 insertions, 8 deletions
diff --git a/crates/yt_dlp/src/lib.rs b/crates/yt_dlp/src/lib.rs index 9d1a0c5..ddbce7d 100644 --- a/crates/yt_dlp/src/lib.rs +++ b/crates/yt_dlp/src/lib.rs @@ -13,6 +13,7 @@ #![allow(clippy::missing_errors_doc)] use std::env; +use std::io::stdout; use std::{fs::File, io::Write}; use std::{path::PathBuf, sync::Once}; @@ -107,6 +108,17 @@ pub fn progress_hook(py: Python<'_>, input: &Bound<'_, PyDict>) -> PyResult<()> return Ok(()); } + // ANSI ESCAPE CODES Wrappers {{{ + // see: https://en.wikipedia.org/wiki/ANSI_escape_code#Control_Sequence_Introducer_commands + const CSI: &str = "\x1b["; + fn clear_whole_line() { + print!("{CSI}2K"); + } + fn move_to_col(x: usize) { + print!("{CSI}{x}G"); + } + // }}} + let input: Map<String, Value> = serde_json::from_str(&json_dumps( py, input @@ -243,12 +255,11 @@ pub fn progress_hook(py: Python<'_>, input: &Bound<'_, PyDict>) -> PyResult<()> } }; - print!("\x1b[1F"); // Move one line up, to allow the `println` after it to print a newline - print!("\x1b[2K"); // Clear whole line. - print!("\x1b[1G"); // Move cursor to column 1. + clear_whole_line(); + move_to_col(1); - println!( - "'{}' [{}/{} at {}] -> [{}/{} {}]", + print!( + "'{}' [{}/{} at {}] -> [{}/{} {}] ", c!("34;1", get_title(true)), c!("33;1", Duration::from(Some(elapsed))), c!("33;1", Duration::from(Some(eta))), @@ -257,14 +268,18 @@ pub fn progress_hook(py: Python<'_>, input: &Bound<'_, PyDict>) -> PyResult<()> c!("31;1", format_bytes(total_bytes)), c!("36;1", format!("{:.02}%", percent)) ); + stdout().flush()?; } "finished" => { - println!("Finished downloading: '{}'", c!("34;1", get_title(false))); + println!( + "-> Finished downloading: '{}'", + c!("34;1", get_title(true)) + ); } "error" => { - panic!("Error whilst downloading: {}", get_title(true)) + panic!("-> Error while downloading: {}", get_title(true)) } - other => panic!("{other} is not a valid state!"), + other => unreachable!("'{other}' should not be a valid state!"), }; Ok(()) |