From 145a776039248a9460e9473e4bc9ef3d533b60c1 Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Mon, 14 Oct 2024 12:32:23 +0200 Subject: feat(videos): Provide a consistent display for the `Video` struct Before, `Video`s where colourized differently, just because the colourization was not standardized. It now is. --- src/select/selection_file/display.rs | 108 ----------------------------------- src/select/selection_file/mod.rs | 1 - 2 files changed, 109 deletions(-) delete mode 100644 src/select/selection_file/display.rs (limited to 'src/select/selection_file') diff --git a/src/select/selection_file/display.rs b/src/select/selection_file/display.rs deleted file mode 100644 index 0714015..0000000 --- a/src/select/selection_file/display.rs +++ /dev/null @@ -1,108 +0,0 @@ -// yt - A fully featured command line YouTube client -// -// Copyright (C) 2024 Benedikt Peetz -// SPDX-License-Identifier: GPL-3.0-or-later -// -// This file is part of Yt. -// -// You should have received a copy of the License along with this program. -// If not, see . - -use std::fmt::Write; - -use anyhow::{Context, Result}; -use chrono::DateTime; -use log::debug; - -use crate::{ - app::App, - select::selection_file::duration::Duration, - storage::video_database::{getters::get_video_opts, Video}, -}; - -macro_rules! c { - ($color:expr, $format:expr) => { - format!("\x1b[{}m{}\x1b[0m", $color, $format) - }; -} - -impl Video { - pub async fn to_select_file_display(&self, app: &App) -> Result { - let mut f = String::new(); - - let opts = get_video_opts(app, &self.extractor_hash) - .await - .with_context(|| format!("Failed to get video options for video: '{}'", self.title))? - .to_cli_flags(app); - let opts_white = if !opts.is_empty() { " " } else { "" }; - - let publish_date = if let Some(date) = self.publish_date { - DateTime::from_timestamp(date, 0) - .expect("This should not fail") - .format("%Y-%m-%d") - .to_string() - } else { - "[No release date]".to_owned() - }; - - let parent_subscription_name = if let Some(sub) = &self.parent_subscription_name { - sub.replace('"', "'") - } else { - "[No author]".to_owned() - }; - - debug!("Formatting video for selection file: {}", self.title); - write!( - f, - r#"{}{}{} {} "{}" "{}" "{}" "{}" "{}"{}"#, - self.status.as_command().trim(), - opts_white, - opts, - self.extractor_hash.into_short_hash(app).await?, - self.title.replace(['"', '„', '”'], "'"), - publish_date, - parent_subscription_name, - Duration::from(self.duration), - self.url.as_str().replace('"', "\\\""), - "\n" - )?; - - Ok(f) - } - - pub async fn to_color_display_owned(self, app: &App) -> Result { - self.to_color_display(app).await - } - pub async fn to_color_display(&self, app: &App) -> Result { - let mut f = String::new(); - - let publish_date = if let Some(date) = self.publish_date { - DateTime::from_timestamp(date, 0) - .expect("This should not fail") - .format("%Y-%m-%d") - .to_string() - } else { - "[No release date]".to_owned() - }; - - let parent_subscription_name = if let Some(sub) = &self.parent_subscription_name { - sub.replace('"', "'") - } else { - "[No author]".to_owned() - }; - - write!( - f, - r#"{} {} {} {} {} {}"#, - c!("31;1", self.status.as_command()), - c!("95;3", self.extractor_hash.into_short_hash(app).await?), - c!("32;1", self.title.replace(['"', '„', '”'], "'")), - c!("37;1", publish_date), - c!("34;1", parent_subscription_name), - c!("35;1", Duration::from(self.duration)), - ) - .expect("This write should always work"); - - Ok(f) - } -} diff --git a/src/select/selection_file/mod.rs b/src/select/selection_file/mod.rs index d228023..45809fa 100644 --- a/src/select/selection_file/mod.rs +++ b/src/select/selection_file/mod.rs @@ -13,7 +13,6 @@ use anyhow::{Context, Result}; use trinitry::Trinitry; -pub mod display; pub mod duration; pub fn process_line(line: &str) -> Result>> { -- cgit 1.4.1