diff options
Diffstat (limited to 'crates/yt_dlp/src/lib.rs')
-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(()) |