Krolyxon
Tag v0.1.2

rmptuisrceventpl_append_keys.rs

96 lines · 4.1 KB · Rust

1use crate::app::{App, AppResult, SelectedTab};
2use crate::browser::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 let short_path = &app.queue_list.get_item_at_current_index().file;
22
23 if let Some(full_path) = app.conn.get_full_path(short_path) {
24 let song = app.conn.get_song_with_only_filename(&full_path);
25
26 if *pl_name == "Current Playlist" {
27 app.conn.conn.push(&song)?;
28 app.update_queue();
29 } else {
30 app.conn.add_to_playlist(pl_name, &song)?;
31 }
32 }
33 }
34
35 SelectedTab::DirectoryBrowser => {
36 let (t, f) = app.browser.filetree.get(app.browser.selected).unwrap();
37 if t == "file" {
38 let short_path = f;
39 if let Some(full_path) = app.conn.get_full_path(short_path) {
40 let song = app.conn.get_song_with_only_filename(&full_path);
41
42 if *pl_name == "Current Playlist" {
43 app.conn.conn.push(&song)?;
44 app.update_queue();
45 } else {
46 app.conn.add_to_playlist(pl_name, &song)?;
47 }
48 }
49 } else if t == "directory" {
50 for (t, f) in app.conn.conn.listfiles(f)?.iter() {
51 // dir_vec.push((t, f));
52 if t == "file"
53 && Path::new(&f).has_extension(&[
54 "mp3", "ogg", "flac", "m4a", "wav", "aac", "opus", "ape",
55 "wma", "mpc", "aiff", "dff", "mp2", "mka",
56 ])
57 {
58 let full_path = app.conn.get_full_path(f).unwrap_or_default();
59 let song = app.conn.get_song_with_only_filename(&full_path);
60 if *pl_name == "Current Playlist" {
61 app.conn.conn.push(&song)?;
62 } else {
63 app.conn.add_to_playlist(pl_name, &song)?;
64 }
65 }
66 }
67 }
68 }
69
70 SelectedTab::Playlists => {
71 let playlist_name = app.pl_list.get_item_at_current_index();
72 if *pl_name == "Current Playlist" {
73 app.conn.load_playlist(playlist_name)?;
74 app.update_queue();
75 } else {
76 let songs = app.conn.conn.playlist(playlist_name)?;
77 for song in songs {
78 // We ignore the Err() since there could be songs in playlists, which do not exist in the db anymore.
79 // So instead of panicking, we just ignore if the song does not exists
80 app.conn
81 .add_to_playlist(*pl_name, &song)
82 .unwrap_or_else(|_| {});
83 }
84 }
85 }
86 }
87
88 // hide the playlist popup
89 app.playlist_popup = false;
90 app.append_list.index = 0;
91 }
92 _ => {}
93 }
94
95 Ok(())
96}