// 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::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?
.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
}
}