Krolyxon
Tag v0.1.5

rmptuisrceventpl_append_keys.rs

102 lines · 4.5 KB · Rust

1use crate::app::{App, AppResult, SelectedTab};
2use crate::utils::FileExtension;
3use crossterm::event::{KeyCode, KeyEvent};
4use std::path::Path;
5
6pub fn hande_pl_append_keys(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
7 match key_event.code {
8 KeyCode::Char('q') | KeyCode::Esc => {
9 app.playlist_popup = false;
10 }
11
12 KeyCode::Char('j') | KeyCode::Down => app.append_list.next(),
13 KeyCode::Char('k') | KeyCode::Up => app.append_list.prev(),
14
15 KeyCode::Enter => {
16 // name of highlighted playlist in append list
17 let pl_name = &app.append_list.get_item_at_current_index();
18
19 match app.selected_tab {
20 SelectedTab::Queue => {
21 // Just exit out the menu if no item is selected in the Queue
22 if app.queue_list.list.is_empty() {
23 app.playlist_popup = false;
24 return Ok(());
25 }
26
27 if let Ok(songs) = app.conn.conn.songs(app.queue_list.index as u32) {
28 let option_song = songs.first();
29 if let Some(song) = option_song {
30 if *pl_name == "Current Playlist" {
31 app.conn.conn.push(&song)?;
32 app.update_queue();
33 } else {
34 app.conn.add_to_playlist(pl_name, &song)?;
35 }
36 }
37 }
38 }
39
40 SelectedTab::DirectoryBrowser => {
41 let (t, f) = app.browser.filetree.get(app.browser.selected).unwrap();
42 if t == "file" {
43 let short_path = f;
44 if let Some(full_path) = app.conn.get_full_path(short_path) {
45 let song = app.conn.get_song_with_only_filename(&full_path);
46
47 if *pl_name == "Current Playlist" {
48 app.conn.conn.push(&song)?;
49 app.update_queue();
50 } else {
51 app.conn.add_to_playlist(pl_name, &song)?;
52 }
53 }
54 } else if t == "directory" {
55 let file = format!("{}/{}", app.browser.path, f);
56 for (t, f) in app.conn.conn.listfiles(&file)?.iter() {
57 // dir_vec.push((t, f));
58 if t == "file"
59 && Path::new(&f).has_extension(&[
60 "mp3", "ogg", "flac", "m4a", "wav", "aac", "opus", "ape",
61 "wma", "mpc", "aiff", "dff", "mp2", "mka",
62 ])
63 {
64 let full_path = app.conn.get_full_path(f).unwrap_or_default();
65 let song = app.conn.get_song_with_only_filename(&full_path);
66 if *pl_name == "Current Playlist" {
67 app.conn.conn.push(&song)?;
68 } else {
69 app.conn.add_to_playlist(pl_name, &song)?;
70 }
71 }
72 }
73 }
74 }
75
76 SelectedTab::Playlists => {
77 let playlist_name = app.pl_list.get_item_at_current_index();
78 if *pl_name == "Current Playlist" {
79 app.conn.load_playlist(playlist_name)?;
80 app.update_queue();
81 } else {
82 let songs = app.conn.conn.playlist(playlist_name)?;
83 for song in songs {
84 // We ignore the Err() since there could be songs in playlists, which do not exist in the db anymore.
85 // So instead of panicking, we just ignore if the song does not exists
86 app.conn
87 .add_to_playlist(*pl_name, &song)
88 .unwrap_or_else(|_| {});
89 }
90 }
91 }
92 }
93
94 // hide the playlist popup
95 app.playlist_popup = false;
96 app.append_list.index = 0;
97 }
98 _ => {}
99 }
100
101 Ok(())
102}