src/items/singleitem.rs

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