src/items/iteminfo.rs
402 lines · 15138 bytes
1use tui::{
2 layout::Rect,
3 style::Style,
4 widgets::{Paragraph, Wrap},
5};
6use tui_additions::framework::FrameworkItem;
7#[cfg(any(feature = "sixel", feature = "halfblock"))]
8use viuer::{print_from_file, Config};
9
10use crate::{
11 config::{AppearanceConfig, MainConfig},
12 global::structs::{Item, Status, Task, Tasks},
13};
14
15/// an item info displays info of any `Item`s
16#[derive(Clone)]
17pub struct ItemInfo {
18 pub item: Option<Item>,
19 pub lazy_scroll: u16,
20}
21
22impl FrameworkItem for ItemInfo {
23 fn render(
24 &mut self,
25 frame: &mut tui::Frame<tui::backend::CrosstermBackend<std::io::Stdout>>,
26 framework: &mut tui_additions::framework::FrameworkClean,
27 area: tui::layout::Rect,
28 popup_render: bool,
29 _info: tui_additions::framework::ItemInfo,
30 ) {
31 if popup_render {
32 return;
33 }
34
35 let item = if let Some(item) = &self.item {
36 item
37 } else {
38 return;
39 };
40
41 let main_config = framework.data.global.get::<MainConfig>().unwrap();
42 let status = framework.data.global.get::<Status>().unwrap();
43
44 // 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
45 let scroll = if !status.render_image {
46 self.lazy_scroll
47 } else if cfg!(any(feature = "halfblock", feature = "sixel"))
48 && main_config.images.display()
49 && !status.popup_opened
50 {
51 #[cfg(any(feature = "sixel", feature = "halfblock"))]
52 let scroll;
53
54 #[cfg(not(any(feature = "sixel", feature = "halfblock")))]
55 let scroll = 0;
56
57 #[cfg(any(feature = "sixel", feature = "halfblock"))]
58 {
59 let thumbnail_path = home::home_dir()
60 .unwrap()
61 .join(".cache/youtube-tui/thumbnails/")
62 .join(item.thumbnail_id());
63 if thumbnail_path.exists() {
64 #[cfg(any(feature = "sixel", feature = "halfblock"))]
65 let config = Config {
66 // channel thumbnails are squares, limiting their maximum width can prevent the
67 // entire page being taken up by the image
68 width: Some(if let Item::MiniChannel(_) = item {
69 area.width / 2
70 } else if let Item::FullChannel(_) = item {
71 area.width / 4
72 } else {
73 area.width
74 } as u32),
75 x: area.x,
76 y: area.y as i16,
77 use_sixel: main_config.images.use_sixels(),
78 ..Default::default()
79 };
80
81 if let Ok((_, height)) = print_from_file(thumbnail_path, &config) {
82 scroll = height as u16;
83 framework
84 .data
85 .state
86 .get_mut::<Tasks>()
87 .unwrap()
88 .priority
89 .push(Task::LazyRendered);
90 } else {
91 scroll = self.lazy_scroll;
92 }
93 } else {
94 scroll = self.lazy_scroll;
95 }
96 }
97 scroll
98 } else {
99 self.lazy_scroll
100 };
101
102 self.lazy_scroll = scroll;
103
104 let appearance = framework.data.global.get::<AppearanceConfig>().unwrap();
105
106 // Each "span" contains a string and a Style, and they are one line max each
107 // A "text" is used for descriptions in video/playlist and channels, and starts a new line if the old one runs out
108 let (spans, text) = match item {
109 Item::MiniVideo(minivideo) => {
110 let mut out = (
111 vec![
112 (
113 String::from("[Video]"),
114 Style::default().fg(appearance.colors.item_info.tag),
115 ),
116 (
117 minivideo.title.clone(),
118 Style::default().fg(appearance.colors.item_info.title),
119 ),
120 ],
121 minivideo.description.as_ref().map(|description| {
122 (
123 description.clone(),
124 Style::default().fg(appearance.colors.item_info.description),
125 )
126 }),
127 );
128 if let Some(views) = &minivideo.views {
129 out.0.push((
130 format!("{views} views"),
131 Style::default().fg(appearance.colors.item_info.viewcount),
132 ));
133 }
134 out.0.push((
135 format!("Length: {}", minivideo.length),
136 Style::default().fg(appearance.colors.item_info.length),
137 ));
138 out.0.push((
139 format!("Uploaded by {}", minivideo.channel),
140 Style::default().fg(appearance.colors.item_info.author),
141 ));
142 if let Some(published) = &minivideo.published {
143 out.0.push((
144 format!("Published {published}"),
145 Style::default().fg(appearance.colors.item_info.published),
146 ));
147 }
148
149 out
150 }
151 Item::MiniPlaylist(miniplaylist) => (
152 vec![
153 (
154 String::from("[Playlist]"),
155 Style::default().fg(appearance.colors.item_info.tag),
156 ),
157 (
158 miniplaylist.title.clone(),
159 Style::default().fg(appearance.colors.item_info.title),
160 ),
161 (
162 format!("Created by by {}", miniplaylist.channel),
163 Style::default().fg(appearance.colors.item_info.author),
164 ),
165 (
166 format!(
167 "{} video{}",
168 miniplaylist.video_count,
169 if miniplaylist.video_count <= 1 {
170 ""
171 } else {
172 "s"
173 }
174 ),
175 Style::default().fg(appearance.colors.item_info.video_count),
176 ),
177 ],
178 None,
179 ),
180 Item::MiniChannel(minichannel) => (
181 vec![
182 (
183 String::from("[Channel]"),
184 Style::default().fg(appearance.colors.item_info.tag),
185 ),
186 (
187 minichannel.name.clone(),
188 Style::default().fg(appearance.colors.item_info.title),
189 ),
190 (
191 format!(
192 "{} subscriber{}",
193 minichannel.sub_count_text,
194 if minichannel.sub_count <= 1 { "" } else { "s" }
195 ),
196 Style::default().fg(appearance.colors.item_info.sub_count),
197 ),
198 (
199 format!(
200 "{} video{}",
201 minichannel.video_count,
202 if minichannel.video_count <= 1 {
203 ""
204 } else {
205 "s"
206 }
207 ),
208 Style::default().fg(appearance.colors.item_info.video_count),
209 ),
210 ],
211 Some((
212 minichannel.description.clone(),
213 Style::default().fg(appearance.colors.item_info.description),
214 )),
215 ),
216 Item::FullVideo(fullvideo) => (
217 vec![
218 (
219 String::from("[Video]"),
220 Style::default().fg(appearance.colors.item_info.tag),
221 ),
222 (
223 fullvideo.title.clone(),
224 Style::default().fg(appearance.colors.item_info.title),
225 ),
226 (
227 format!("{} views", fullvideo.views),
228 Style::default().fg(appearance.colors.item_info.viewcount),
229 ),
230 (
231 format!("{} likes", fullvideo.likes),
232 Style::default().fg(appearance.colors.item_info.likes),
233 ),
234 (
235 format!("Length: {}", fullvideo.length),
236 Style::default().fg(appearance.colors.item_info.length),
237 ),
238 (
239 format!(
240 "Uploaded by {} ({} subscribers)",
241 fullvideo.channel, fullvideo.sub_count
242 ),
243 Style::default().fg(appearance.colors.item_info.author),
244 ),
245 (
246 format!("Published {}", fullvideo.published),
247 Style::default().fg(appearance.colors.item_info.published),
248 ),
249 ],
250 Some((
251 fullvideo.description.clone(),
252 Style::default().fg(appearance.colors.item_info.description),
253 )),
254 ),
255 Item::FullPlaylist(fullplaylist) => (
256 vec![
257 (
258 String::from("[Playlist]"),
259 Style::default().fg(appearance.colors.item_info.tag),
260 ),
261 (
262 fullplaylist.title.clone(),
263 Style::default().fg(appearance.colors.item_info.title),
264 ),
265 (
266 format!("Created by by {}", fullplaylist.channel),
267 Style::default().fg(appearance.colors.item_info.author),
268 ),
269 (
270 format!(
271 "{} video{}",
272 fullplaylist.video_count,
273 if fullplaylist.video_count <= 1 {
274 ""
275 } else {
276 "s"
277 }
278 ),
279 Style::default().fg(appearance.colors.item_info.video_count),
280 ),
281 ],
282 Some((
283 fullplaylist.description.clone(),
284 Style::default().fg(appearance.colors.item_info.description),
285 )),
286 ),
287 Item::FullChannel(fullchannel) => (
288 vec![
289 (
290 if fullchannel.autogenerated {
291 String::from("[Auto generated]")
292 } else {
293 String::from("[Channel]")
294 },
295 Style::default().fg(appearance.colors.item_info.tag),
296 ),
297 (
298 fullchannel.name.clone(),
299 Style::default().fg(appearance.colors.item_info.title),
300 ),
301 (
302 format!("{} total views", fullchannel.total_views),
303 Style::default().fg(appearance.colors.item_info.viewcount),
304 ),
305 (
306 format!(
307 "{} subscriber{}",
308 fullchannel.sub_count_text,
309 if fullchannel.sub_count <= 1 { "" } else { "s" }
310 ),
311 Style::default().fg(appearance.colors.item_info.sub_count),
312 ),
313 (
314 format!("Created at {}", fullchannel.created),
315 Style::default().fg(appearance.colors.item_info.published),
316 ),
317 ],
318 Some((
319 fullchannel.description.clone(),
320 Style::default().fg(appearance.colors.item_info.description),
321 )),
322 ),
323 Item::Unknown(searchitem_transitional) => (
324 vec![(
325 format!("Unknown type `{}`", searchitem_transitional.r#type),
326 Style::default().fg(appearance.colors.text_error),
327 )],
328 Some((
329 serde_json::to_string(&searchitem_transitional).unwrap(),
330 Style::default().fg(appearance.colors.item_info.description),
331 )),
332 ),
333 Item::Page(b) => (
334 vec![(
335 if *b { "Next page" } else { "Previous page" }.to_string(),
336 Style::default().fg(appearance.colors.item_info.page_turner),
337 )],
338 None,
339 ),
340 };
341
342 let mut y = if scroll >= area.height { 0 } else { scroll } + area.y;
343 let bottom = area.bottom();
344
345 // puts each "span" in its own line
346 for (text, style) in spans.into_iter().take((bottom - y) as usize) {
347 let paragraph = Paragraph::new(text).style(style);
348 frame.render_widget(
349 paragraph,
350 Rect {
351 y,
352 height: 1,
353 ..area
354 },
355 );
356 y += 1;
357 }
358
359 if y > bottom || text.is_none() {
360 return;
361 }
362
363 // displays description only if its a non empty string
364 let (text, style) = text.unwrap();
365 if text.is_empty() {
366 return;
367 }
368 let paragraph = Paragraph::new(format!("Description:\n{text}"))
369 .style(style)
370 .wrap(Wrap { trim: true });
371 frame.render_widget(
372 paragraph,
373 Rect {
374 y,
375 height: bottom - y,
376 ..area
377 },
378 );
379 }
380
381 fn selectable(&self) -> bool {
382 true
383 }
384}
385
386impl Default for ItemInfo {
387 fn default() -> Self {
388 Self {
389 item: None,
390 lazy_scroll: 0,
391 }
392 }
393}
394
395impl ItemInfo {
396 pub fn new(item: Option<Item>) -> Self {
397 Self {
398 item,
399 ..Default::default()
400 }
401 }
402}