rmptui9c30356
move queue into its own file, and optimize it
+75 −53
5 files changed
- M src/app.rs +13 −9
- M src/handler.rs +3 −3
- M src/lib.rs +3 −0
- A src/queue.rs +37 −0
- M src/ui.rs +19 −41
| @@ -3,8 +3,9 @@ use std::time::Duration; | |||
| 3 | 3 | use crate::browser::FileBrowser; | |
| 4 | 4 | use crate::connection::Connection; | |
| 5 | 5 | use crate::list::ContentList; | |
| 6 | + | use crate::queue::Queue; | |
| 6 | 7 | use crate::ui::InputMode; | |
| 7 | − | use mpd::Client; | |
| 8 | + | use mpd::{Client, Song}; | |
| 8 | 9 | ||
| 9 | 10 | // Application result type | |
| 10 | 11 | pub type AppResult<T> = std::result::Result<T, Box<dyn std::error::Error>>; | |
| @@ -16,7 +17,7 @@ pub struct App { | |||
| 16 | 17 | pub running: bool, | |
| 17 | 18 | pub conn: Connection, | |
| 18 | 19 | pub browser: FileBrowser, // Directory browser | |
| 19 | − | pub queue_list: ContentList<String>, // Stores the current playing queue | |
| 20 | + | pub queue_list: Queue, // Stores the current playing queue | |
| 20 | 21 | pub pl_list: ContentList<String>, // Stores list of playlists | |
| 21 | 22 | pub selected_tab: SelectedTab, // Used to switch between tabs | |
| 22 | 23 | ||
| @@ -33,6 +34,8 @@ pub struct App { | |||
| 33 | 34 | pub append_list: ContentList<String>, | |
| 34 | 35 | } | |
| 35 | 36 | ||
| 37 | + | ||
| 38 | + | ||
| 36 | 39 | #[derive(Debug, PartialEq, Clone)] | |
| 37 | 40 | pub enum SelectedTab { | |
| 38 | 41 | DirectoryBrowser, | |
| @@ -43,7 +46,7 @@ pub enum SelectedTab { | |||
| 43 | 46 | impl App { | |
| 44 | 47 | pub fn builder(addrs: &str) -> AppResult<Self> { | |
| 45 | 48 | let mut conn = Connection::new(addrs).unwrap(); | |
| 46 | − | let mut queue_list = ContentList::new(); | |
| 49 | + | let mut queue_list = Queue::new(); | |
| 47 | 50 | let mut pl_list = ContentList::new(); | |
| 48 | 51 | pl_list.list = Self::get_playlist(&mut conn.conn)?; | |
| 49 | 52 | ||
| @@ -78,7 +81,7 @@ impl App { | |||
| 78 | 81 | self.running = false; | |
| 79 | 82 | } | |
| 80 | 83 | ||
| 81 | − | pub fn get_queue(conn: &mut Connection, vec: &mut Vec<String>) { | |
| 84 | + | pub fn get_queue(conn: &mut Connection, vec: &mut Vec<Song>) { | |
| 82 | 85 | // conn.conn.queue().unwrap().into_iter().for_each(|x| { | |
| 83 | 86 | // if let Some(title) = x.title { | |
| 84 | 87 | // if let Some(artist) = x.artist { | |
| @@ -91,7 +94,8 @@ impl App { | |||
| 91 | 94 | // } | |
| 92 | 95 | // }); | |
| 93 | 96 | conn.conn.queue().unwrap().into_iter().for_each(|x| { | |
| 94 | − | vec.push(x.file); | |
| 97 | + | // vec.push(x.title.unwrap()); | |
| 98 | + | vec.push(x); | |
| 95 | 99 | }); | |
| 96 | 100 | } | |
| 97 | 101 | ||
| @@ -124,7 +128,7 @@ impl App { | |||
| 124 | 128 | ||
| 125 | 129 | let mut status = false; | |
| 126 | 130 | for (i, song) in self.queue_list.list.clone().iter().enumerate() { | |
| 127 | − | if song.contains(file) { | |
| 131 | + | if song.file.contains(file) { | |
| 128 | 132 | self.conn.conn.delete(i as u32).unwrap(); | |
| 129 | 133 | status = true; | |
| 130 | 134 | } | |
| @@ -150,11 +154,11 @@ impl App { | |||
| 150 | 154 | .queue_list | |
| 151 | 155 | .list | |
| 152 | 156 | .get(self.queue_list.index) | |
| 153 | − | .unwrap() | |
| 157 | + | .unwrap().file | |
| 154 | 158 | .to_string(); | |
| 155 | 159 | ||
| 156 | 160 | for (i, song) in self.queue_list.list.clone().iter().enumerate() { | |
| 157 | − | if song.contains(&file) { | |
| 161 | + | if song.file.contains(&file) { | |
| 158 | 162 | self.conn.conn.delete(i as u32).unwrap(); | |
| 159 | 163 | if self.queue_list.index == self.queue_list.list.len() - 1 | |
| 160 | 164 | && self.queue_list.index != 0 | |
| @@ -203,7 +207,7 @@ impl App { | |||
| 203 | 207 | // .get(0) | |
| 204 | 208 | // .unwrap() | |
| 205 | 209 | // .clone(); | |
| 206 | − | let index = self.queue_list.list.iter().position(|x| x.contains(path)); | |
| 210 | + | let index = self.queue_list.list.iter().position(|x| x.file.contains(path)); | |
| 207 | 211 | ||
| 208 | 212 | if index.is_some() { | |
| 209 | 213 | self.conn.conn.switch(index.unwrap() as u32)?; | |
| @@ -34,13 +34,13 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { | |||
| 34 | 34 | .queue_list | |
| 35 | 35 | .list | |
| 36 | 36 | .iter() | |
| 37 | − | .map(|f| f.as_str()) | |
| 37 | + | .map(|f| f.file.as_str()) | |
| 38 | 38 | .collect::<Vec<&str>>(); | |
| 39 | 39 | let res: Vec<(&str, f32)> = fuzzy_search_sorted(&app.search_input, &list); | |
| 40 | 40 | let res = res.iter().map(|(x, _)| *x).collect::<Vec<&str>>(); | |
| 41 | 41 | ||
| 42 | 42 | for (i, item) in app.queue_list.list.iter().enumerate() { | |
| 43 | − | if item.contains(res.get(0).unwrap()) { | |
| 43 | + | if item.file.contains(res.get(0).unwrap()) { | |
| 44 | 44 | app.queue_list.index = i; | |
| 45 | 45 | } | |
| 46 | 46 | } | |
| @@ -162,7 +162,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { | |||
| 162 | 162 | match app.selected_tab { | |
| 163 | 163 | SelectedTab::Queue => { | |
| 164 | 164 | s_index = app.queue_list.index; | |
| 165 | − | let short_path = app.queue_list.list.get(s_index).unwrap().to_string(); | |
| 165 | + | let short_path = &app.queue_list.list.get(s_index).unwrap().file; | |
| 166 | 166 | ||
| 167 | 167 | if let Some(full_path) = app.conn.get_full_path(&short_path) { | |
| 168 | 168 | let song = app.conn.get_song_with_only_filename(&full_path); | |
| @@ -24,3 +24,6 @@ pub mod handler; | |||
| 24 | 24 | ||
| 25 | 25 | /// Application | |
| 26 | 26 | pub mod app; | |
| 27 | + | ||
| 28 | + | /// The Queue structure | |
| 29 | + | pub mod queue; | |
| @@ -0,0 +1,37 @@ | |||
| 1 | + | use mpd::Song; | |
| 2 | + | ||
| 3 | + | #[derive(Debug)] | |
| 4 | + | pub struct Queue { | |
| 5 | + | pub list: Vec<Song>, | |
| 6 | + | pub index: usize, | |
| 7 | + | } | |
| 8 | + | ||
| 9 | + | impl Queue { | |
| 10 | + | pub fn new() -> Self { | |
| 11 | + | Queue { | |
| 12 | + | list: Vec::new(), | |
| 13 | + | index: 0, | |
| 14 | + | } | |
| 15 | + | } | |
| 16 | + | ||
| 17 | + | // Go to next item in list | |
| 18 | + | pub fn next(&mut self) { | |
| 19 | + | let len = self.list.len(); | |
| 20 | + | if len != 0 { | |
| 21 | + | if self.index < len - 1 { | |
| 22 | + | self.index += 1; | |
| 23 | + | } | |
| 24 | + | } | |
| 25 | + | } | |
| 26 | + | ||
| 27 | + | /// Go to previous item in list | |
| 28 | + | pub fn prev(&mut self) { | |
| 29 | + | if self.index != 0 { | |
| 30 | + | self.index -= 1; | |
| 31 | + | } | |
| 32 | + | } | |
| 33 | + | ||
| 34 | + | pub fn reset_index(&mut self) { | |
| 35 | + | self.index = 0; | |
| 36 | + | } | |
| 37 | + | } | |
| @@ -1,7 +1,6 @@ | |||
| 1 | 1 | use std::time::Duration; | |
| 2 | 2 | ||
| 3 | 3 | use crate::app::{App, SelectedTab}; | |
| 4 | − | use mpd::Song; | |
| 5 | 4 | use ratatui::{ | |
| 6 | 5 | prelude::*, | |
| 7 | 6 | widgets::{block::Title, *}, | |
| @@ -77,28 +76,22 @@ fn draw_directory_browser(frame: &mut Frame, app: &mut App, size: Rect) { | |||
| 77 | 76 | ||
| 78 | 77 | let mut status: bool = false; | |
| 79 | 78 | for sn in app.queue_list.list.iter() { | |
| 80 | − | if sn.contains(s) { | |
| 79 | + | if sn.file.contains(s) { | |
| 81 | 80 | status = true; | |
| 82 | 81 | } | |
| 83 | 82 | } | |
| 84 | 83 | ||
| 84 | + | let row = Row::new(vec![ | |
| 85 | + | Cell::from(artist), | |
| 86 | + | Cell::from(track.green()), | |
| 87 | + | Cell::from(title), | |
| 88 | + | Cell::from(album.cyan()), | |
| 89 | + | Cell::from(time.to_string().magenta()), | |
| 90 | + | ]); | |
| 91 | + | ||
| 85 | 92 | if status { | |
| 86 | − | let row = Row::new(vec![ | |
| 87 | − | Cell::from(artist), | |
| 88 | − | Cell::from(track.green()), | |
| 89 | − | Cell::from(title), | |
| 90 | − | Cell::from(album), | |
| 91 | − | Cell::from(time.to_string().magenta()), | |
| 92 | − | ]); | |
| 93 | 93 | row.magenta().bold() | |
| 94 | 94 | } else { | |
| 95 | − | let row = Row::new(vec![ | |
| 96 | − | Cell::from(artist), | |
| 97 | − | Cell::from(track.green()), | |
| 98 | − | Cell::from(title), | |
| 99 | − | Cell::from(album), | |
| 100 | − | Cell::from(time.to_string().magenta()), | |
| 101 | − | ]); | |
| 102 | 95 | row | |
| 103 | 96 | } | |
| 104 | 97 | } else { | |
| @@ -153,17 +146,8 @@ fn draw_directory_browser(frame: &mut Frame, app: &mut App, size: Rect) { | |||
| 153 | 146 | ||
| 154 | 147 | /// draws playing queue | |
| 155 | 148 | fn draw_queue(frame: &mut Frame, app: &mut App, size: Rect) { | |
| 156 | − | let rows = app.queue_list.list.iter().map(|s| { | |
| 149 | + | let rows = app.queue_list.list.iter().map(|song| { | |
| 157 | 150 | // metadata | |
| 158 | − | let song = app | |
| 159 | − | .conn | |
| 160 | − | .conn | |
| 161 | − | .lsinfo(&Song { | |
| 162 | − | file: app.conn.get_full_path(&s).unwrap_or_default(), | |
| 163 | − | ..Default::default() | |
| 164 | − | }) | |
| 165 | − | .unwrap_or_default(); | |
| 166 | − | let song = song.get(0).unwrap(); | |
| 167 | 151 | let title = song.clone().title.unwrap_or_else(|| song.clone().file); | |
| 168 | 152 | let artist = song.clone().artist.unwrap_or_default().cyan(); | |
| 169 | 153 | let album = song | |
| @@ -184,23 +168,17 @@ fn draw_queue(frame: &mut Frame, app: &mut App, size: Rect) { | |||
| 184 | 168 | ||
| 185 | 169 | let time = App::format_time(song.clone().duration.unwrap_or_else(|| Duration::new(0, 0))); | |
| 186 | 170 | ||
| 187 | − | if s.contains(&app.conn.current_song.file) { | |
| 188 | − | let row = Row::new(vec![ | |
| 189 | − | Cell::from(artist), | |
| 190 | − | Cell::from(track.green()), | |
| 191 | − | Cell::from(title), | |
| 192 | − | Cell::from(album), | |
| 193 | − | Cell::from(time.to_string().magenta()), | |
| 194 | − | ]); | |
| 171 | + | let row = Row::new(vec![ | |
| 172 | + | Cell::from(artist), | |
| 173 | + | Cell::from(track.green()), | |
| 174 | + | Cell::from(title), | |
| 175 | + | Cell::from(album.cyan()), | |
| 176 | + | Cell::from(time.to_string().magenta()), | |
| 177 | + | ]); | |
| 178 | + | ||
| 179 | + | if song.file.contains(&app.conn.current_song.file) { | |
| 195 | 180 | row.magenta().bold() | |
| 196 | 181 | } else { | |
| 197 | − | let row = Row::new(vec![ | |
| 198 | − | Cell::from(artist), | |
| 199 | − | Cell::from(track.green()), | |
| 200 | − | Cell::from(title), | |
| 201 | − | Cell::from(album), | |
| 202 | − | Cell::from(time.to_string().magenta()), | |
| 203 | − | ]); | |
| 204 | 182 | row | |
| 205 | 183 | } | |
| 206 | 184 | }); | |