diff options
author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2025-06-13 21:12:39 +0200 |
---|---|---|
committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2025-06-13 21:12:39 +0200 |
commit | efc35b5bd76bf4e4aab6750ead45713a79e851f9 (patch) | |
tree | 586e750cabcff02370f17d0b1655767b92dd1bac | |
parent | fix(yt/watch/playlist): Workaround terminals, that treat 0 as 1 (diff) | |
download | yt-efc35b5bd76bf4e4aab6750ead45713a79e851f9.zip |
refactor(yt): Consolidate the multiple ANSI escape code wrapper functions
Diffstat (limited to '')
-rw-r--r-- | yt/src/ansi_escape_codes.rs | 26 | ||||
-rw-r--r-- | yt/src/download/progress_hook.rs | 16 | ||||
-rw-r--r-- | yt/src/main.rs | 1 | ||||
-rw-r--r-- | yt/src/update/updater.rs | 24 | ||||
-rw-r--r-- | yt/src/watch/playlist.rs | 16 |
5 files changed, 37 insertions, 46 deletions
diff --git a/yt/src/ansi_escape_codes.rs b/yt/src/ansi_escape_codes.rs new file mode 100644 index 0000000..ae1805d --- /dev/null +++ b/yt/src/ansi_escape_codes.rs @@ -0,0 +1,26 @@ +// see: https://en.wikipedia.org/wiki/ANSI_escape_code#Control_Sequence_Introducer_commands +const CSI: &str = "\x1b["; +pub fn erase_in_display_from_cursor() { + print!("{CSI}0J"); +} +pub fn cursor_up(number: usize) { + // HACK(@bpeetz): The default is `1` and running this command with a + // number of `0` results in it using the default (i.e., `1`) <2025-03-25> + if number != 0 { + print!("{CSI}{number}A"); + } +} + +pub fn clear_whole_line() { + eprint!("{CSI}2K"); +} +pub fn move_to_col(x: usize) { + eprint!("{CSI}{x}G"); +} + +pub fn hide_cursor() { + eprint!("{CSI}?25l"); +} +pub fn show_cursor() { + eprint!("{CSI}?25h"); +} diff --git a/yt/src/download/progress_hook.rs b/yt/src/download/progress_hook.rs index 65156e7..db58225 100644 --- a/yt/src/download/progress_hook.rs +++ b/yt/src/download/progress_hook.rs @@ -7,7 +7,10 @@ use bytes::Bytes; use log::{Level, log_enabled}; use yt_dlp::mk_python_function; -use crate::select::selection_file::duration::MaybeDuration; +use crate::{ + ansi_escape_codes::{clear_whole_line, move_to_col}, + select::selection_file::duration::MaybeDuration, +}; // #[allow(clippy::too_many_lines)] // #[allow(clippy::missing_panics_doc)] @@ -26,17 +29,6 @@ pub fn progress_hook( 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() { - eprint!("{CSI}2K"); - } - fn move_to_col(x: usize) { - eprint!("{CSI}{x}G"); - } - // }}} - macro_rules! get { (@interrogate $item:ident, $type_fun:ident, $get_fun:ident, $name:expr) => {{ let a = $item.get($name).expect(concat!( diff --git a/yt/src/main.rs b/yt/src/main.rs index 093d0e1..39f52f4 100644 --- a/yt/src/main.rs +++ b/yt/src/main.rs @@ -33,6 +33,7 @@ use tokio::{ use crate::{cli::Command, storage::subscriptions}; +pub mod ansi_escape_codes; pub mod app; pub mod cli; pub mod unreachable; diff --git a/yt/src/update/updater.rs b/yt/src/update/updater.rs index 900fba7..8da654b 100644 --- a/yt/src/update/updater.rs +++ b/yt/src/update/updater.rs @@ -20,7 +20,11 @@ use log::{Level, debug, error, log_enabled}; use serde_json::json; use yt_dlp::{InfoJson, YoutubeDLOptions, json_cast, json_get}; -use crate::{app::App, storage::subscriptions::Subscription}; +use crate::{ + ansi_escape_codes::{clear_whole_line, move_to_col}, + app::App, + storage::subscriptions::Subscription, +}; use super::process_subscription; @@ -71,24 +75,6 @@ impl<'a> Updater<'a> { &self, sub: &'a Subscription, ) -> Result<Vec<(&'a Subscription, InfoJson)>> { - // TODO(@bpeetz): Deduplicate with the progress_hook. <2025-06-13> ) - // 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() { - eprint!("{CSI}2K"); - } - fn move_to_col(x: usize) { - eprint!("{CSI}{x}G"); - } - // fn hide_cursor() { - // eprint!("{CSI}?25l"); - // } - // fn show_cursor() { - // eprint!("{CSI}?25h"); - // } - // }}} - let yt_dlp = YoutubeDLOptions::new() .set("playliststart", 1) .set("playlistend", self.max_backlog) diff --git a/yt/src/watch/playlist.rs b/yt/src/watch/playlist.rs index 5be46ce..ff383d0 100644 --- a/yt/src/watch/playlist.rs +++ b/yt/src/watch/playlist.rs @@ -11,6 +11,7 @@ use std::path::Path; use crate::{ + ansi_escape_codes::{cursor_up, erase_in_display_from_cursor}, app::App, storage::video_database::{Video, VideoStatus, get, notify::wait_for_db_write}, }; @@ -31,21 +32,6 @@ fn cache_values(video: &Video) -> (&Path, bool) { } } -// ANSI ESCAPE CODES Wrappers {{{ -// see: https://en.wikipedia.org/wiki/ANSI_escape_code#Control_Sequence_Introducer_commands -const CSI: &str = "\x1b["; -fn erase_in_display_from_cursor() { - print!("{CSI}0J"); -} -fn cursor_up(number: usize) { - // HACK(@bpeetz): The default is `1` and running this command with a - // number of `0` results in it using the default (i.e., `1`) <2025-03-25> - if number != 0 { - print!("{CSI}{number}A"); - } -} -// }}} - /// # Panics /// Only if internal assertions fail. pub async fn playlist(app: &App, watch: bool) -> Result<()> { |