src/items/iteminfo.rs

db597a03bc7a1786d5261e8a55628da30193c775
youtube-tui/src/items/iteminfo.rs history · blame · raw

162 lines · 5284 bytes

  1use tui::{
  2    layout::Rect,
  3    style::Style,
  4    widgets::{Paragraph, Wrap},
  5};
  6use tui_additions::framework::FrameworkItem;
  7use viuer::{print_from_file, Config};
  8
  9use crate::{
 10    config::{AppearanceConfig, MainConfig},
 11    global::item::Item,
 12};
 13
 14// an item info displays info of any `Item`s
 15#[derive(Clone)]
 16pub struct ItemInfo {
 17    pub item: Option<Item>,
 18}
 19
 20impl FrameworkItem for ItemInfo {
 21    fn render(
 22        &mut self,
 23        frame: &mut tui::Frame<tui::backend::CrosstermBackend<std::io::Stdout>>,
 24        framework: &mut tui_additions::framework::FrameworkClean,
 25        area: tui::layout::Rect,
 26        popup_render: bool,
 27        _info: tui_additions::framework::ItemInfo,
 28    ) {
 29        if popup_render {
 30            return;
 31        }
 32
 33        let item = if let Some(item) = &self.item {
 34            item
 35        } else {
 36            return;
 37        };
 38
 39        let main_config = framework.data.global.get::<MainConfig>().unwrap();
 40        let appearance = framework.data.global.get::<AppearanceConfig>().unwrap();
 41
 42        // The scroll (space above) text info will be the height of the image, but if the image fail to display, the scroll will be 0
 43        let scroll = if main_config.images.display() {
 44            let thumbnail_path = home::home_dir()
 45                .unwrap()
 46                .join(".cache/youtube-tui/thumbnails/")
 47                .join(item.thumbnail_id());
 48            if thumbnail_path.exists() {
 49                let config = Config {
 50                    width: Some(area.width as u32),
 51                    x: area.x,
 52                    y: area.y as i16,
 53                    use_sixel: main_config.images.use_sixels(),
 54                    ..Default::default()
 55                };
 56                if let Ok((_, height)) = print_from_file(thumbnail_path, &config) {
 57                    height as u16
 58                } else {
 59                    0
 60                }
 61            } else {
 62                0
 63            }
 64        } else {
 65            0
 66        };
 67
 68        // Each "span" contains a string and a Style, and they are one line max each
 69        // A "text" is used for descriptions in video/playlist and channels, and starts a new line if the old one runs out
 70        let (spans, text) = match item {
 71            Item::MiniVideo(minivideo) => {
 72                let mut out = (
 73                    vec![
 74                        (
 75                            String::from("[Video]"),
 76                            Style::default().fg(appearance.colors.item_info.tag),
 77                        ),
 78                        (
 79                            minivideo.title.clone(),
 80                            Style::default().fg(appearance.colors.item_info.title),
 81                        ),
 82                        (
 83                            format!("Uploaded by {}", minivideo.channel),
 84                            Style::default().fg(appearance.colors.item_info.author),
 85                        ),
 86                    ],
 87                    if let Some(description) = &minivideo.description {
 88                        Some((
 89                            description.clone(),
 90                            Style::default().fg(appearance.colors.item_info.description),
 91                        ))
 92                    } else {
 93                        None
 94                    },
 95                );
 96                if let Some(views) = &minivideo.views {
 97                    out.0.push((
 98                        format!("{} views", views),
 99                        Style::default().fg(appearance.colors.item_info.viewcount),
100                    ));
101                }
102                out.0.extend(vec![
103                    (
104                        format!("Length: {}", minivideo.length),
105                        Style::default().fg(appearance.colors.item_info.length),
106                    ),
107                    (
108                        format!("Published {}", minivideo.published),
109                        Style::default().fg(appearance.colors.item_info.published),
110                    )
111                ].into_iter());
112                out
113            }
114        };
115
116        // puts each "span" in its own line
117        let mut y = if scroll >= area.height { 0 } else { scroll } + area.y;
118        let bottom = area.bottom();
119
120        for (text, style) in spans.into_iter().take((bottom - y) as usize) {
121            let paragraph = Paragraph::new(text).style(style);
122            frame.render_widget(
123                paragraph,
124                Rect {
125                    y,
126                    height: 1,
127                    ..area
128                },
129            );
130            y += 1;
131        }
132
133        if y > bottom || text.is_none() {
134            return;
135        }
136
137        // renders the "text"
138        let (text, style) = text.unwrap();
139        if text.len() == 0 {
140            return;
141        }
142        let paragraph = Paragraph::new(format!("Description:\n{}", text)).style(style).wrap(Wrap { trim: true });
143        frame.render_widget(
144            paragraph,
145            Rect {
146                y,
147                height: bottom - y,
148                ..area
149            },
150        );
151    }
152
153    fn selectable(&self) -> bool {
154        true
155    }
156}
157
158impl Default for ItemInfo {
159    fn default() -> Self {
160        Self { item: None }
161    }
162}