From 1debeb77f7986de1b659dcfdc442de6415e1d9f5 Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Wed, 21 Aug 2024 10:49:23 +0200 Subject: chore: Initial Commit This repository was migrated out of my nixos-config. --- src/select/selection_file/display.rs | 103 +++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 src/select/selection_file/display.rs (limited to 'src/select/selection_file/display.rs') diff --git a/src/select/selection_file/display.rs b/src/select/selection_file/display.rs new file mode 100644 index 0000000..12d128c --- /dev/null +++ b/src/select/selection_file/display.rs @@ -0,0 +1,103 @@ +// 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::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? + .to_cli_flags(); + 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(), + 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 fn to_color_display(&self) -> 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!("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"); + + f + } +} -- cgit 1.4.1