src/global/item.rs

db597a03bc7a1786d5261e8a55628da30193c775
youtube-tui/src/global/item.rs history · blame · raw

112 lines · 3645 bytes

  1use std::fmt::Display;
  2
  3use super::functions::{date_text, secs_display_string, viewcount_text};
  4use invidious::structs::hidden::{PopularItem, SearchItem, TrendingVideo};
  5
  6// Items are things like a single video/playlist and channel
  7// they are displayed by the item info widget in `iteminfo.rs`
  8#[derive(Clone)]
  9pub enum Item {
 10    // minivideos are all videos that appeared without actually clicking into the video
 11    MiniVideo(MiniVideoItem),
 12}
 13
 14#[derive(Clone)]
 15pub struct MiniVideoItem {
 16    pub title: String,
 17    pub id: String,
 18    pub thumbnail_url: String,
 19    pub length: String,
 20    pub views: Option<String>,
 21    pub channel: String,
 22    pub channel_id: String,
 23    pub published: String,
 24    pub description: Option<String>,
 25}
 26
 27impl Display for Item {
 28    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 29        f.write_str(match self {
 30            Self::MiniVideo(video) => &video.title,
 31        })
 32    }
 33}
 34
 35impl Item {
 36    // the file save path for thumbnails (which is by their id)
 37    pub fn thumbnail_id(&self) -> &str {
 38        match self {
 39            Self::MiniVideo(video) => &video.id,
 40        }
 41    }
 42
 43    // cannot impl From<T> because it also need `image_index`
 44    pub fn from_trending_video(original: TrendingVideo, image_index: usize) -> Self {
 45        Self::MiniVideo(MiniVideoItem {
 46            title: original.title,
 47            id: original.id,
 48            thumbnail_url: original.thumbmails[image_index].url.clone(),
 49            length: secs_display_string(original.length),
 50            views: Some(viewcount_text(original.views)),
 51            channel: original.author,
 52            channel_id: original.author_id,
 53            published: format!(
 54                "{} [{}]",
 55                original.published_text,
 56                date_text(original.published)
 57            ),
 58            description: Some(original.description),
 59        })
 60    }
 61
 62    pub fn from_popular_item(original: PopularItem, image_index: usize) -> Self {
 63        Self::MiniVideo(MiniVideoItem {
 64            title: original.title,
 65            id: original.id,
 66            thumbnail_url: original.thumbnails[image_index].url.clone(),
 67            length: secs_display_string(original.length),
 68            views: Some(viewcount_text(original.views)),
 69            channel: original.author,
 70            channel_id: original.author_id,
 71            published: format!(
 72                "{} [{}]",
 73                original.published_text,
 74                date_text(original.published)
 75            ),
 76            description: None,
 77        })
 78    }
 79
 80    pub fn from_search_item(original: SearchItem, image_index: usize) -> Self {
 81        match original {
 82            SearchItem::Video {
 83                title,
 84                id,
 85                author,
 86                author_id,
 87                author_url: _,
 88                length,
 89                thumbnails,
 90                description,
 91                description_html: _,
 92                views,
 93                published,
 94                published_text,
 95                live: _,
 96                paid: _,
 97                premium: _,
 98            } => Self::MiniVideo(MiniVideoItem {
 99                title,
100                id,
101                thumbnail_url: thumbnails[image_index].url.clone(),
102                length: secs_display_string(length as u32),
103                views: Some(viewcount_text(views)),
104                channel: author,
105                channel_id: author_id,
106                published: format!("{} [{}]", published_text, date_text(published)),
107                description: Some(description),
108            }),
109            _ => todo!(),
110        }
111    }
112}