diff options
author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2024-10-14 12:32:23 +0200 |
---|---|---|
committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2024-10-14 12:34:56 +0200 |
commit | 145a776039248a9460e9473e4bc9ef3d533b60c1 (patch) | |
tree | 7b2a948ae1f08335eba477c26bf1d5e83cdac24b /src/select/selection_file | |
parent | fix(downloader): Don't display changed cache size on first run (diff) | |
download | yt-145a776039248a9460e9473e4bc9ef3d533b60c1.tar.gz yt-145a776039248a9460e9473e4bc9ef3d533b60c1.zip |
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.
Diffstat (limited to '')
-rw-r--r-- | src/select/selection_file/display.rs | 108 | ||||
-rw-r--r-- | src/select/selection_file/mod.rs | 1 |
2 files changed, 0 insertions, 109 deletions
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 <benedikt.peetz@b-peetz.de> -// 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 <https://www.gnu.org/licenses/gpl-3.0.txt>. - -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<String> { - 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<String> { - self.to_color_display(app).await - } - pub async fn to_color_display(&self, app: &App) -> Result<String> { - 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<Option<Vec<String>>> { |