src/items/singleitem.rs

789 lines · 31628 bytes

  1use super::ItemInfo;
  2use crate::{
  3    config::{AppearanceConfig, CommandsConfig, KeyBindingsConfig, MainConfig, Provider},
  4    global::{
  5        functions::download_all_images,
  6        structs::{
  7            InvidiousClient, Item, KeyAction, Message, Page, SingleItemPage, Status, Task, Tasks,
  8            WatchHistory,
  9        },
 10    },
 11};
 12use std::{collections::HashMap, thread};
 13use tui::{layout::Constraint, style::Style};
 14use tui_additions::{
 15    framework::FrameworkItem,
 16    widgets::{Grid, TextList},
 17};
 18
 19impl Default for SingleItem {
 20    fn default() -> Self {
 21        Self {
 22            item: None,
 23            iteminfo: ItemInfo::default(),
 24            grid: Grid::new(
 25                vec![Constraint::Percentage(30), Constraint::Percentage(70)],
 26                vec![Constraint::Percentage(100)],
 27            )
 28            .unwrap(),
 29            r#type: SingleItemType::None,
 30        }
 31    }
 32}
 33
 34/// main item in the `SingleItem(_)` page
 35#[derive(Clone)]
 36pub struct SingleItem {
 37    pub item: Option<Item>,
 38    pub iteminfo: ItemInfo,
 39    pub grid: Grid,
 40    pub r#type: SingleItemType,
 41}
 42
 43/// variations that the struct can hold
 44#[derive(Clone)]
 45pub enum SingleItemType {
 46    None,
 47    Video(SingleVideoItem),
 48    Playlist(Box<SinglePlaylistItem>),
 49}
 50
 51#[derive(Clone)]
 52pub struct SingleVideoItem {
 53    pub textlist: TextList,
 54    pub commands: Vec<(String, String)>,
 55    pub provider: Provider,
 56}
 57
 58#[derive(Clone)]
 59pub struct SinglePlaylistItem {
 60    pub commands_view: TextList,
 61    pub videos_view: TextList,
 62    pub commands: Vec<(String, String)>,
 63    pub is_commands_view: bool,
 64    pub hovered_video: ItemInfo,
 65    pub provider: Provider,
 66}
 67
 68impl SingleVideoItem {
 69    pub fn new(commands: &CommandsConfig, mainconfig: &MainConfig) -> Self {
 70        Self {
 71            textlist: TextList::default()
 72                .items(&commands.video.iter().map(|command| &command.0).collect())
 73                .unwrap(),
 74            commands: commands
 75                .video
 76                .clone()
 77                .into_iter()
 78                .map(|(display, command)| (display, command))
 79                .collect(),
 80            provider: mainconfig.provider,
 81        }
 82    }
 83
 84    /// updates the %switch-provider% command in the list
 85    pub fn update_provider(&mut self) {
 86        self.commands
 87            .iter()
 88            .enumerate()
 89            .for_each(|(index, (display, command))| {
 90                if command.as_str() == "%switch-provider%" {
 91                    self.textlist.items[index] =
 92                        display.replace("${provider}", self.provider.as_str());
 93                }
 94            })
 95    }
 96
 97    pub fn update_appearance(
 98        &mut self,
 99        appearance: &AppearanceConfig,
100        iteminfo: &tui_additions::framework::ItemInfo,
101    ) {
102        self.textlist.set_border_type(appearance.borders);
103        self.textlist
104            .set_style(Style::default().fg(appearance.colors.text));
105
106        if iteminfo.selected {
107            self.textlist
108                .set_cursor_style(Style::default().fg(appearance.colors.outline_hover));
109            self.textlist
110                .set_selected_style(Style::default().fg(appearance.colors.text_special));
111        } else {
112            self.textlist
113                .set_cursor_style(Style::default().fg(appearance.colors.outline_secondary));
114            self.textlist
115                .set_selected_style(Style::default().fg(appearance.colors.text_secondary));
116        }
117    }
118
119    /// creates a hashmap from `self`, containing info of the current item
120    pub fn inflate_map(&self, mainconfig: &MainConfig, item: &Item) -> HashMap<String, String> {
121        let video_item = item.fullvideo().unwrap();
122
123        HashMap::from([
124            (
125                String::from("url"),
126                match self.provider {
127                    Provider::Invidious => format!(
128                        "'{}/watch?v={}'",
129                        mainconfig.invidious_instance, video_item.id
130                    ),
131                    Provider::YouTube => format!("'https://youtu.be/{}'", video_item.id),
132                    // Provider::Piped => {
133                    //     format!("'{}/watch?v={}'", mainconfig.piped_instance, video_item.id)
134                    // }
135                },
136            ),
137            (
138                String::from("embed-url"),
139                match self.provider {
140                    Provider::Invidious => format!(
141                        "'{}/embed/{}'",
142                        mainconfig.invidious_instance, video_item.id
143                    ),
144                    Provider::YouTube => format!("'https://youtube.com/embed/{}'", video_item.id),
145                    // Provider::Piped => {
146                    //     format!("'{}/embed/{}'", mainconfig.piped_instance, video_item.id)
147                    // }
148                },
149            ),
150        ])
151    }
152
153    /// add "env variables" from `self.inflate_map` to an existing hashmap
154    pub fn inflate(
155        &self,
156        mut env: HashMap<String, String>,
157        mainconfig: &MainConfig,
158        item: &Item,
159    ) -> HashMap<String, String> {
160        env.extend(self.inflate_map(mainconfig, item));
161        env
162    }
163}
164
165impl SinglePlaylistItem {
166    pub fn new(
167        commands: &CommandsConfig,
168        mainconfig: &MainConfig,
169        playlist_items: &[Item],
170    ) -> Self {
171        let hovered_video = ItemInfo::new(if playlist_items.is_empty() {
172            None
173        } else {
174            Some(playlist_items[0].clone())
175        });
176        Self {
177            commands_view: TextList::default()
178                .items(&commands.playlist.iter().map(|command| &command.0).collect())
179                .unwrap(),
180            videos_view: TextList::default()
181                .items(&{
182                    let mut items = vec!["Switch view"];
183                    items.extend(
184                        playlist_items
185                            .iter()
186                            .map(|item| item.minivideo().unwrap().title.as_str()),
187                    );
188                    items
189                })
190                .unwrap(),
191            commands: commands
192                .playlist
193                .clone()
194                .into_iter()
195                .map(|(display, command)| (display, command))
196                .collect(),
197            hovered_video,
198            is_commands_view: true,
199            provider: mainconfig.provider,
200        }
201    }
202
203    pub fn update_appearance(
204        &mut self,
205        appearance: &AppearanceConfig,
206        iteminfo: &tui_additions::framework::ItemInfo,
207        grid: &mut Grid,
208    ) {
209        if self.is_commands_view {
210            grid.widths = vec![Constraint::Percentage(30), Constraint::Percentage(70)];
211            self.commands_view.set_border_type(appearance.borders);
212            self.commands_view
213                .set_style(Style::default().fg(appearance.colors.text));
214
215            if iteminfo.selected {
216                self.commands_view
217                    .set_cursor_style(Style::default().fg(appearance.colors.outline_hover));
218                self.commands_view
219                    .set_selected_style(Style::default().fg(appearance.colors.text_special));
220            } else {
221                self.commands_view
222                    .set_cursor_style(Style::default().fg(appearance.colors.outline_secondary));
223                self.commands_view
224                    .set_selected_style(Style::default().fg(appearance.colors.text_secondary));
225            }
226        } else {
227            grid.widths = if self.videos_view.selected == 0 {
228                vec![Constraint::Percentage(30), Constraint::Percentage(70)]
229            } else {
230                vec![
231                    Constraint::Percentage(30),
232                    Constraint::Percentage(40),
233                    Constraint::Percentage(30),
234                ]
235            };
236            self.videos_view.set_border_type(appearance.borders);
237            self.videos_view
238                .set_style(Style::default().fg(appearance.colors.text));
239
240            if iteminfo.selected {
241                self.videos_view
242                    .set_cursor_style(Style::default().fg(appearance.colors.outline_hover));
243                self.videos_view
244                    .set_selected_style(Style::default().fg(appearance.colors.text_special));
245            } else {
246                self.videos_view
247                    .set_cursor_style(Style::default().fg(appearance.colors.outline_secondary));
248                self.videos_view
249                    .set_selected_style(Style::default().fg(appearance.colors.text_secondary));
250            }
251        }
252    }
253
254    /// updates the %switch-provider% command in the list
255    pub fn update_provider(&mut self) {
256        self.commands
257            .iter()
258            .enumerate()
259            .for_each(|(index, (display, command))| {
260                if command.as_str() == "%switch-provider%" {
261                    self.commands_view.items[index] =
262                        display.replace("${provider}", self.provider.as_str());
263                }
264            })
265    }
266
267    /// creates a hashmap from `self`, containing info of the current item
268    pub fn inflate_map(&self, mainconfig: &MainConfig, item: &Item) -> HashMap<String, String> {
269        let playlist_item = item.fullplaylist().unwrap();
270        HashMap::from([
271            (
272                String::from("url"),
273                match self.provider {
274                    Provider::YouTube => {
275                        format!("https://www.youtube.com/playlist?list={}", playlist_item.id)
276                    }
277                    Provider::Invidious => format!(
278                        "{}/playlist?list={}",
279                        mainconfig.invidious_instance, playlist_item.id
280                    ),
281                    // Provider::Piped => format!(
282                    //     "{}/playlist?list={}",
283                    //     mainconfig.piped_instance, playlist_item.id
284                    // ),
285                },
286            ),
287            (
288                String::from("all-videos"),
289                match self.provider {
290                    Provider::YouTube => playlist_item
291                        .videos
292                        .iter()
293                        .map(|video| {
294                            format!("'https://youtu.be/{}'", video.minivideo().unwrap().id)
295                        })
296                        .collect::<Vec<_>>()
297                        .join(" "),
298                    Provider::Invidious => playlist_item
299                        .videos
300                        .iter()
301                        .map(|video| {
302                            format!(
303                                "'{}/watch?v={}'",
304                                mainconfig.invidious_instance,
305                                video.minivideo().unwrap().id
306                            )
307                        })
308                        .collect::<Vec<_>>()
309                        .join(" "),
310                    // Provider::Piped => playlist_item
311                    //     .videos
312                    //     .iter()
313                    //     .map(|video| {
314                    //         format!(
315                    //             "'{}/watch?v={}'",
316                    //             mainconfig.piped_instance,
317                    //             video.minivideo().id
318                    //         )
319                    //     })
320                    //     .collect::<Vec<_>>()
321                    //     .join(" "),
322                },
323            ),
324        ])
325    }
326
327    /// add "env variables" from `self.inflate_map` to an existing hashmap
328    pub fn inflate(
329        &self,
330        mut env: HashMap<String, String>,
331        mainconfig: &MainConfig,
332        item: &Item,
333    ) -> HashMap<String, String> {
334        env.extend(self.inflate_map(mainconfig, item));
335        env
336    }
337}
338
339impl SingleItemType {
340    pub fn is_none(&self) -> bool {
341        matches!(self, Self::None)
342    }
343
344    pub fn update_appearance(
345        &mut self,
346        appearance: &AppearanceConfig,
347        iteminfo: &tui_additions::framework::ItemInfo,
348        grid: &mut Grid,
349    ) {
350        match self {
351            Self::None => {}
352            Self::Playlist(playlistitem) => {
353                playlistitem.update_appearance(appearance, iteminfo, grid)
354            }
355            Self::Video(videoitem) => videoitem.update_appearance(appearance, iteminfo),
356        }
357    }
358}
359
360impl SingleItem {
361    fn update_appearance(
362        &mut self,
363        appearance: &AppearanceConfig,
364        iteminfo: &tui_additions::framework::ItemInfo,
365    ) {
366        self.grid.set_border_type(appearance.borders);
367
368        if iteminfo.selected {
369            self.grid
370                .set_border_style(Style::default().fg(appearance.colors.outline_selected));
371        } else if iteminfo.hover {
372            self.grid
373                .set_border_style(Style::default().fg(appearance.colors.outline_hover));
374        } else {
375            self.grid
376                .set_border_style(Style::default().fg(appearance.colors.outline));
377        }
378
379        self.r#type
380            .update_appearance(appearance, iteminfo, &mut self.grid);
381    }
382
383    pub fn update_provider(&mut self) {
384        match &mut self.r#type {
385            SingleItemType::Video(singlevideoitem) => singlevideoitem.update_provider(),
386            SingleItemType::Playlist(singleplaylistitem) => singleplaylistitem.update_provider(),
387            SingleItemType::None => {}
388        }
389    }
390}
391
392impl FrameworkItem for SingleItem {
393    fn render(
394        &mut self,
395        frame: &mut tui::Frame<tui::backend::CrosstermBackend<std::io::Stdout>>,
396        framework: &mut tui_additions::framework::FrameworkClean,
397        area: tui::layout::Rect,
398        popup_render: bool,
399        info: tui_additions::framework::ItemInfo,
400    ) {
401        if popup_render {
402            return;
403        }
404
405        let appearance = framework.data.global.get::<AppearanceConfig>().unwrap();
406        self.update_appearance(appearance, &info);
407
408        let chunks = self.grid.chunks(area).unwrap()[0].clone();
409
410        frame.render_widget(self.grid.clone(), area);
411
412        // only continue if `self.item` is Some
413        if self.item.is_none() {
414            return;
415        }
416
417        match &mut self.r#type {
418            SingleItemType::Video(typeinfo) => {
419                // 2 by 1 grid, item info in the first cell and textlist at the second
420                self.iteminfo
421                    .render(frame, framework, chunks[0], popup_render, info);
422                typeinfo.textlist.set_height(chunks[1].height);
423                frame.render_widget(typeinfo.textlist.clone(), chunks[1]);
424            }
425            SingleItemType::Playlist(typeinfo) => {
426                // 3 by 1 grid if hovering a video inside the playlist
427                // if not then 2 by 1
428                //
429                // item info in the first cell, textlists in the second, hovering video on 3rd (if
430                // present)
431                self.iteminfo
432                    .render(frame, framework, chunks[0], popup_render, info);
433                if typeinfo.is_commands_view {
434                    typeinfo.commands_view.set_height(chunks[1].height);
435                    frame.render_widget(typeinfo.commands_view.clone(), chunks[1]);
436                } else {
437                    typeinfo.videos_view.set_height(chunks[1].height);
438                    frame.render_widget(typeinfo.videos_view.clone(), chunks[1]);
439
440                    if typeinfo.videos_view.selected != 0 {
441                        typeinfo.hovered_video.render(
442                            frame,
443                            framework,
444                            chunks[2],
445                            popup_render,
446                            info,
447                        );
448                    }
449                }
450            }
451            SingleItemType::None => {}
452        }
453    }
454
455    fn load_item(
456        &mut self,
457        framework: &mut tui_additions::framework::FrameworkClean,
458        _info: tui_additions::framework::ItemInfo,
459    ) -> Result<(), Box<dyn std::error::Error>> {
460        *self = Self::default();
461
462        let page = framework.data.state.get::<Page>().unwrap();
463
464        let r#type = if let Page::SingleItem(r#type) = page {
465            r#type
466        } else {
467            unreachable!("item `SingleItem` cannot be used in {page:?}")
468        };
469
470        let client = &framework.data.global.get::<InvidiousClient>().unwrap().0;
471        let mainconfig = framework.data.global.get::<MainConfig>().unwrap();
472
473        // load items using the invidious api
474        // gets the item that it needs to load from `data.state.Page`
475        self.item = match r#type {
476            SingleItemPage::Video(id) => {
477                self.r#type = SingleItemType::Video(SingleVideoItem::new(
478                    framework.data.global.get::<CommandsConfig>().unwrap(),
479                    framework.data.global.get::<MainConfig>().unwrap(),
480                ));
481                let video = Item::from_full_video(client.video(id, None)?, mainconfig.image_index);
482                if mainconfig.images.display() {
483                    download_all_images(vec![(&video).into()]);
484                }
485                Some(video)
486            }
487            SingleItemPage::Playlist(id) => {
488                let playlist =
489                    Item::from_full_playlist(client.playlist(id, None)?, mainconfig.image_index);
490                let videos = &playlist.fullplaylist()?.videos;
491                self.r#type = SingleItemType::Playlist(
492                    SinglePlaylistItem::new(
493                        framework.data.global.get::<CommandsConfig>().unwrap(),
494                        framework.data.global.get::<MainConfig>().unwrap(),
495                        videos,
496                    )
497                    .into(),
498                );
499
500                if mainconfig.images.display() {
501                    download_all_images({
502                        let mut items = videos.iter().map(|item| item.into()).collect::<Vec<_>>();
503                        items.extend([(&playlist).into()].into_iter());
504                        items
505                    });
506                }
507
508                Some(playlist)
509            }
510        };
511        self.iteminfo.item = self.item.clone();
512        // need to update provider every time the item loads or else it will display `${provider}`
513        // instead of the actual provider (e.g. `YouTube`)
514        self.update_provider();
515
516        if let Some(item) = &self.item {
517            if item.is_unknown() {
518                return Ok(());
519            }
520
521            let item = item.clone();
522            let max_watch_history = mainconfig.max_watch_history;
523
524            // push to watch history
525            let watch_history = framework.data.global.get_mut::<WatchHistory>().unwrap();
526            watch_history.push(item, max_watch_history);
527            watch_history.save()?;
528        }
529
530        Ok(())
531    }
532
533    fn key_event(
534        &mut self,
535        framework: &mut tui_additions::framework::FrameworkClean,
536        key: crossterm::event::KeyEvent,
537        info: tui_additions::framework::ItemInfo,
538    ) -> Result<(), Box<dyn std::error::Error>> {
539        // discard any key inputs if `self.is_none()` becuase nothing can happen if self is none
540        if self.r#type.is_none() {
541            return Ok(());
542        }
543
544        let action = if let Some(keyactions) = framework
545            .data
546            .global
547            .get::<KeyBindingsConfig>()
548            .unwrap()
549            .0
550            .get(&key.code)
551        {
552            if let Some(action) = keyactions.get(&key.modifiers.bits()) {
553                *action
554            } else {
555                return Ok(());
556            }
557        } else {
558            return Ok(());
559        };
560
561        let updated = match &mut self.r#type {
562            SingleItemType::Video(singlevideoitem) => match action {
563                // move the cursor in the textlist, only update the screen if it is changed
564                KeyAction::MoveUp => singlevideoitem.textlist.up().is_ok(),
565                KeyAction::MoveDown => singlevideoitem.textlist.down().is_ok(),
566                KeyAction::MoveLeft => singlevideoitem.textlist.first().is_ok(),
567                KeyAction::MoveRight => singlevideoitem.textlist.last().is_ok(),
568                KeyAction::Select => {
569                    let command_string = singlevideoitem.commands
570                        [singlevideoitem.textlist.selected]
571                        .1
572                        .clone();
573
574                    match command_string.as_str() {
575                        // checks for special cases that the items should consume the command
576                        // instead of running it
577                        "%switch-provider%" => {
578                            singlevideoitem.provider.rotate();
579                            singlevideoitem.update_provider();
580                            *framework.data.global.get_mut::<Message>().unwrap() =
581                                Message::Success(format!(
582                                    "Switched provider to {}",
583                                    singlevideoitem.provider.as_str()
584                                ));
585                        }
586                        _ => {
587                            let mainconfig = framework.data.global.get::<MainConfig>().unwrap();
588                            // joins the env from the one in mainconfig and video info
589                            let env = singlevideoitem.inflate(
590                                mainconfig.env.clone(),
591                                mainconfig,
592                                self.item.as_ref().unwrap(),
593                            );
594                            let command_string = apply_env(command_string, &env);
595
596                            *framework.data.global.get_mut::<Message>().unwrap() =
597                                Message::Success(command_string.clone());
598
599                            // this allows creating commands from string
600                            let mut command = execute::command(command_string);
601
602                            // run the command in a new thread so it doesn't freeze the current one
603                            thread::spawn(move || {
604                                let _ = command.output();
605                            });
606                        }
607                    }
608
609                    true
610                }
611                _ => false,
612            },
613            SingleItemType::Playlist(singleplaylistitem) => {
614                // there are 2 possible states in a playlist item
615                // they are handelled separately
616                if singleplaylistitem.is_commands_view {
617                    match action {
618                        KeyAction::MoveUp => singleplaylistitem.commands_view.up().is_ok(),
619                        KeyAction::MoveDown => singleplaylistitem.commands_view.down().is_ok(),
620                        KeyAction::MoveLeft => singleplaylistitem.commands_view.first().is_ok(),
621                        KeyAction::MoveRight => singleplaylistitem.commands_view.last().is_ok(),
622                        KeyAction::Select => {
623                            let command_string = singleplaylistitem.commands
624                                [singleplaylistitem.commands_view.selected]
625                                .1
626                                .clone();
627
628                            // checks for special cases
629                            match command_string.as_str() {
630                                "%switch-view%" => {
631                                    singleplaylistitem.is_commands_view =
632                                        !singleplaylistitem.is_commands_view;
633                                    self.update_appearance(
634                                        framework.data.global.get::<AppearanceConfig>().unwrap(),
635                                        &info,
636                                    );
637                                    *framework.data.global.get_mut::<Message>().unwrap() =
638                                        Message::Success(String::from("Switched view"));
639                                }
640                                "%switch-provider%" => {
641                                    singleplaylistitem.provider.rotate();
642                                    singleplaylistitem.update_provider();
643                                }
644                                // same as before if string is not a special case then the run the command
645                                _ => {
646                                    let mainconfig =
647                                        framework.data.global.get::<MainConfig>().unwrap();
648                                    let env = singleplaylistitem.inflate(
649                                        mainconfig.env.clone(),
650                                        mainconfig,
651                                        self.item.as_ref().unwrap(),
652                                    );
653                                    let command_string = apply_env(command_string, &env);
654
655                                    *framework.data.global.get_mut::<Message>().unwrap() =
656                                        Message::Success(command_string.clone());
657                                    let mut command = execute::command(command_string);
658
659                                    thread::spawn(move || {
660                                        let _ = command.output();
661                                    });
662                                }
663                            };
664
665                            true
666                        }
667                        _ => false,
668                    }
669                } else {
670                    match action {
671                        // checks if it is updated, if it is and selected is not 0 (is hovering on
672                        // a video), then also need to update the iteminfo
673                        KeyAction::MoveUp => {
674                            let updated = singleplaylistitem.videos_view.up().is_ok();
675                            if updated && singleplaylistitem.videos_view.selected != 0 {
676                                singleplaylistitem.hovered_video.item = Some(
677                                    self.item.as_ref().unwrap().fullplaylist()?.videos
678                                        [singleplaylistitem.videos_view.selected - 1]
679                                        .clone(),
680                                );
681                            } else {
682                                // going from a hovering video to not hovering will make the image
683                                // stay on the screen, therefore it needs to be removed by clearing
684                                // the screen
685                                framework
686                                    .data
687                                    .state
688                                    .get_mut::<Tasks>()
689                                    .unwrap()
690                                    .priority
691                                    .push(Task::ClearPage);
692                            }
693                            updated
694                        }
695                        KeyAction::MoveDown => {
696                            let updated = singleplaylistitem.videos_view.down().is_ok();
697                            if updated && singleplaylistitem.videos_view.selected != 0 {
698                                singleplaylistitem.hovered_video.item = Some(
699                                    self.item.as_ref().unwrap().fullplaylist()?.videos
700                                        [singleplaylistitem.videos_view.selected - 1]
701                                        .clone(),
702                                );
703                            }
704                            updated
705                        }
706                        KeyAction::MoveLeft => {
707                            let updated = singleplaylistitem.videos_view.first().is_ok();
708                            if updated && singleplaylistitem.videos_view.selected != 0 {
709                                singleplaylistitem.hovered_video.item = Some(
710                                    self.item.as_ref().unwrap().fullplaylist()?.videos
711                                        [singleplaylistitem.videos_view.selected - 1]
712                                        .clone(),
713                                );
714                            }
715                            updated
716                        }
717                        KeyAction::MoveRight => {
718                            let updated = singleplaylistitem.videos_view.last().is_ok();
719                            if updated && singleplaylistitem.videos_view.selected != 0 {
720                                singleplaylistitem.hovered_video.item = Some(
721                                    self.item.as_ref().unwrap().fullplaylist()?.videos
722                                        [singleplaylistitem.videos_view.selected - 1]
723                                        .clone(),
724                                );
725                            }
726                            updated
727                        }
728                        KeyAction::Select => {
729                            if singleplaylistitem.videos_view.selected == 0 {
730                                singleplaylistitem.is_commands_view =
731                                    !singleplaylistitem.is_commands_view;
732                                self.update_appearance(
733                                    framework.data.global.get::<AppearanceConfig>().unwrap(),
734                                    &info,
735                                );
736                                *framework.data.global.get_mut::<Message>().unwrap() =
737                                    Message::Success(String::from("Switched view"));
738                            } else {
739                                framework
740                                    .data
741                                    .state
742                                    .get_mut::<Tasks>()
743                                    .unwrap()
744                                    .priority
745                                    .push(Task::LoadPage(Page::SingleItem(SingleItemPage::Video(
746                                        self.item.as_ref().unwrap().fullplaylist()?.videos
747                                            [singleplaylistitem.videos_view.selected - 1]
748                                            .minivideo()?
749                                            .id
750                                            .clone(),
751                                    ))));
752                            }
753
754                            true
755                        }
756                        _ => false,
757                    }
758                }
759            }
760            SingleItemType::None => false,
761        };
762
763        if updated {
764            framework
765                .data
766                .global
767                .get_mut::<Status>()
768                .unwrap()
769                .render_image = true;
770            framework
771                .data
772                .state
773                .get_mut::<Tasks>()
774                .unwrap()
775                .priority
776                .push(Task::RenderAll);
777        }
778
779        Ok(())
780    }
781}
782
783/// apply the env hashmap to a command string
784pub fn apply_env(mut command: String, env: &HashMap<String, String>) -> String {
785    env.iter().for_each(|(key, value)| {
786        command = command.replace(&format!("${{{key}}}"), value);
787    });
788    command
789}