diff options
| -rw-r--r-- | crates/yt/src/commands/select/implm/fs_generators/mod.rs | 47 | ||||
| -rw-r--r-- | crates/yt/src/commands/select/implm/mod.rs | 16 | ||||
| -rw-r--r-- | crates/yt/src/commands/select/mod.rs | 47 | ||||
| -rw-r--r-- | crates/yt/src/storage/db/video/mod.rs | 2 |
4 files changed, 98 insertions, 14 deletions
diff --git a/crates/yt/src/commands/select/implm/fs_generators/mod.rs b/crates/yt/src/commands/select/implm/fs_generators/mod.rs index 10da032..a0f9098 100644 --- a/crates/yt/src/commands/select/implm/fs_generators/mod.rs +++ b/crates/yt/src/commands/select/implm/fs_generators/mod.rs @@ -24,7 +24,7 @@ use crate::{ commands::{ Command, select::{ - SelectCommand, SelectSplitSortKey, SelectSplitSortMode, + SelectCommand, SelectFileSortKey, SelectSplitSortKey, SortMode, implm::standalone::{self, handle_select_cmd}, }, }, @@ -48,7 +48,7 @@ pub(crate) async fn select_split( app: &App, done: bool, sort_key: SelectSplitSortKey, - sort_mode: SelectSplitSortMode, + sort_mode: SortMode, ) -> Result<()> { let temp_dir = tempfile::Builder::new() .prefix("yt_video_select-") @@ -81,7 +81,7 @@ pub(crate) async fn select_split( match sort_key { SelectSplitSortKey::Publisher => { - // PERFORMANCE: The clone here should not be neeed. <2025-06-15> + // PERFORMANCE: The clone here should not be needed. <2025-06-15> temp_vec.sort_by_key(|(name, _): &(String, Vec<Video>)| name.to_owned()); } SelectSplitSortKey::Videos => { @@ -90,10 +90,10 @@ pub(crate) async fn select_split( } match sort_mode { - SelectSplitSortMode::Asc => { + SortMode::Asc => { // Std's default mode is ascending. } - SelectSplitSortMode::Desc => { + SortMode::Desc => { temp_vec.reverse(); } } @@ -171,7 +171,13 @@ pub(crate) async fn select_split( Ok(()) } -pub(crate) async fn select_file(app: &App, done: bool, use_last_selection: bool) -> Result<()> { +pub(crate) async fn select_file( + app: &App, + done: bool, + sort_key: SelectFileSortKey, + sort_mode: SortMode, + use_last_selection: bool, +) -> Result<()> { let temp_file = tempfile::Builder::new() .prefix("yt_video_select-") .suffix(".yts") @@ -182,7 +188,34 @@ pub(crate) async fn select_file(app: &App, done: bool, use_last_selection: bool) if use_last_selection { fs::copy(&app.config.paths.last_selection_path, &temp_file)?; } else { - let matching_videos = get_videos(app, done).await?; + let mut matching_videos = get_videos(app, done).await?; + + match sort_key { + SelectFileSortKey::Priority => { + // The default sort is by priority + } + SelectFileSortKey::ReleaseDate => { + matching_videos.sort_by_key(|video| video.publish_date.unwrap_or_default()); + } + SelectFileSortKey::Author => matching_videos.sort_by_key(|video| { + video + .parent_subscription_name + .as_ref() + .map(|s| s.as_str()) + .unwrap_or("") + .to_owned() + }), + SelectFileSortKey::Title => matching_videos.sort_by_key(|video| video.title.clone()), + } + + match sort_mode { + SortMode::Asc => { + // The default is ascending + } + SortMode::Desc => { + matching_videos.reverse(); + } + } write_videos_to_file(app, temp_file.as_file(), &matching_videos).await?; } diff --git a/crates/yt/src/commands/select/implm/mod.rs b/crates/yt/src/commands/select/implm/mod.rs index f39c77f..5aaf4a1 100644 --- a/crates/yt/src/commands/select/implm/mod.rs +++ b/crates/yt/src/commands/select/implm/mod.rs @@ -21,15 +21,29 @@ impl SelectCommand { SelectCommand::File { done, use_last_selection, - } => Box::pin(fs_generators::select_file(app, done, use_last_selection)).await?, + sort_mode, + sort_key, + } => { + Box::pin(fs_generators::select_file( + app, + done, + sort_key, + sort_mode, + use_last_selection, + )) + .await? + } + SelectCommand::Split { done, sort_key, sort_mode, } => Box::pin(fs_generators::select_split(app, done, sort_key, sort_mode)).await?, + SelectCommand::Add { urls, start, stop } => { Box::pin(standalone::add::add(app, urls, start, stop)).await?; } + other => { let shared = other .clone() diff --git a/crates/yt/src/commands/select/mod.rs b/crates/yt/src/commands/select/mod.rs index db69238..085c244 100644 --- a/crates/yt/src/commands/select/mod.rs +++ b/crates/yt/src/commands/select/mod.rs @@ -32,6 +32,14 @@ pub(super) enum SelectCommand { #[arg(long, short)] done: bool, + /// Which value to use to sort videos by + #[arg(long, short = 'k', default_value_t)] + sort_key: SelectFileSortKey, + + /// Which mode to use for sorting. + #[arg(long, short = 'm', default_value_t)] + sort_mode: SortMode, + /// Use the last selection file (useful if you've spend time on it and want to get it again) #[arg(long, short, conflicts_with = "done")] use_last_selection: bool, @@ -49,7 +57,7 @@ pub(super) enum SelectCommand { /// Which mode to use for sorting. #[arg(default_value_t)] - sort_mode: SelectSplitSortMode, + sort_mode: SortMode, }, /// Add a video to the database @@ -110,6 +118,8 @@ impl Default for SelectCommand { Self::File { done: false, use_last_selection: false, + sort_mode: SortMode::default(), + sort_key: SelectFileSortKey::default(), } } } @@ -211,7 +221,7 @@ impl Display for SelectSplitSortKey { } #[derive(Default, ValueEnum, Clone, Copy, Debug)] -enum SelectSplitSortMode { +enum SortMode { /// Sort in ascending order (small -> big) #[default] Asc, @@ -220,11 +230,38 @@ enum SelectSplitSortMode { Desc, } -impl Display for SelectSplitSortMode { +impl Display for SortMode { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + match self { + SortMode::Asc => f.write_str("asc"), + SortMode::Desc => f.write_str("desc"), + } + } +} + +#[derive(Default, ValueEnum, Clone, Copy, Debug)] +enum SelectFileSortKey { + /// Sort by the priority value, given to a video + #[default] + Priority, + + /// Sort by a videos release date + ReleaseDate, + + /// Sort by the videos author + Author, + + /// Sort by the videos title + Title, +} + +impl Display for SelectFileSortKey { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { match self { - SelectSplitSortMode::Asc => f.write_str("asc"), - SelectSplitSortMode::Desc => f.write_str("desc"), + SelectFileSortKey::Priority => f.write_str("priority"), + SelectFileSortKey::ReleaseDate => f.write_str("release_date"), + SelectFileSortKey::Author => f.write_str("author"), + SelectFileSortKey::Title => f.write_str("title"), } } } diff --git a/crates/yt/src/storage/db/video/mod.rs b/crates/yt/src/storage/db/video/mod.rs index deeb82c..d8d712f 100644 --- a/crates/yt/src/storage/db/video/mod.rs +++ b/crates/yt/src/storage/db/video/mod.rs @@ -125,7 +125,7 @@ impl Display for Priority { } /// An UNIX time stamp. -#[derive(Debug, Default, Clone, Copy)] +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub(crate) struct TimeStamp { value: i64, } |
