diff options
Diffstat (limited to 'src/select')
-rw-r--r-- | src/select/cmds.rs | 6 | ||||
-rw-r--r-- | src/select/mod.rs | 12 | ||||
-rw-r--r-- | src/select/selection_file/display.rs | 108 | ||||
-rw-r--r-- | src/select/selection_file/mod.rs | 1 |
4 files changed, 12 insertions, 115 deletions
diff --git a/src/select/cmds.rs b/src/select/cmds.rs index b45cc48..6e71607 100644 --- a/src/select/cmds.rs +++ b/src/select/cmds.rs @@ -19,6 +19,7 @@ use crate::{ VideoOptions, VideoStatus, }, update::video_entry_to_video, + videos::display::format_video::FormatVideo, }; use anyhow::{bail, Context, Result}; @@ -57,7 +58,10 @@ pub async fn handle_select_cmd( entry: yt_dlp::wrapper::info_json::InfoJson, ) -> Result<()> { let video = video_entry_to_video(entry, None)?; - println!("{}", video.to_color_display(app).await?); + println!( + "{}", + (&video.to_formatted_video(app).await?.colorize()).to_line_display() + ); add_video(app, video).await?; Ok(()) diff --git a/src/select/mod.rs b/src/select/mod.rs index 2663a04..ca7a203 100644 --- a/src/select/mod.rs +++ b/src/select/mod.rs @@ -20,6 +20,7 @@ use crate::{ cli::CliArgs, constants::HELP_STR, storage::video_database::{getters::get_videos, VideoStatus}, + videos::display::format_video::FormatVideo, }; use anyhow::{bail, Context, Result}; @@ -63,23 +64,24 @@ pub async fn select(app: &App, done: bool, use_last_selection: bool) -> Result<( // Warmup the cache for the display rendering of the videos. // Otherwise the futures would all try to warm it up at the same time. if let Some(vid) = matching_videos.first() { - let _ = vid.to_select_file_display(app).await?; + let _ = vid.to_formatted_video(app).await?; } let mut edit_file = BufWriter::new(&temp_file); join_all( matching_videos - .iter() - .map(|vid| async { vid.to_select_file_display(app).await }) + .into_iter() + .map(|vid| async { vid.to_formatted_video_owned(app).await }) .collect::<Vec<_>>(), ) .await .into_iter() .try_for_each(|line| -> Result<()> { - let line = line?; + let formatted_line = (&line?).to_select_file_display(); + edit_file - .write_all(line.as_bytes()) + .write_all(formatted_line.as_bytes()) .expect("This write should not fail"); Ok(()) 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>>> { |