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