Krolyxon
Commit fde33bc

rmptuisrcevent_handlerpl_append_keys.rs

115 lines · 5.4 KB · Rust

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