Krolyxon
Commit 9c30356

rmptui9c30356

move queue into its own file, and optimize it

Author
krolxon
Date
2024-04-30 23:10 +0530
Parent
32a3c60
Commit
9c30356a3e0b333bab32ea1a866738fbab14ea11

+75 −53

5 files changed

  1. M src/app.rs +13 −9
  2. M src/handler.rs +3 −3
  3. M src/lib.rs +3 −0
  4. A src/queue.rs +37 −0
  5. M src/ui.rs +19 −41
M src/app.rs View
@@ -3,8 +3,9 @@ use std::time::Duration;
33 use crate::browser::FileBrowser;
44 use crate::connection::Connection;
55 use crate::list::ContentList;
6+use crate::queue::Queue;
67 use crate::ui::InputMode;
7−use mpd::Client;
8+use mpd::{Client, Song};
89
910 // Application result type
1011 pub type AppResult<T> = std::result::Result<T, Box<dyn std::error::Error>>;
@@ -16,7 +17,7 @@ pub struct App {
1617 pub running: bool,
1718 pub conn: Connection,
1819 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
2021 pub pl_list: ContentList<String>, // Stores list of playlists
2122 pub selected_tab: SelectedTab, // Used to switch between tabs
2223
@@ -33,6 +34,8 @@ pub struct App {
3334 pub append_list: ContentList<String>,
3435 }
3536
37+
38+
3639 #[derive(Debug, PartialEq, Clone)]
3740 pub enum SelectedTab {
3841 DirectoryBrowser,
@@ -43,7 +46,7 @@ pub enum SelectedTab {
4346 impl App {
4447 pub fn builder(addrs: &str) -> AppResult<Self> {
4548 let mut conn = Connection::new(addrs).unwrap();
46− let mut queue_list = ContentList::new();
49+ let mut queue_list = Queue::new();
4750 let mut pl_list = ContentList::new();
4851 pl_list.list = Self::get_playlist(&mut conn.conn)?;
4952
@@ -78,7 +81,7 @@ impl App {
7881 self.running = false;
7982 }
8083
81− pub fn get_queue(conn: &mut Connection, vec: &mut Vec<String>) {
84+ pub fn get_queue(conn: &mut Connection, vec: &mut Vec<Song>) {
8285 // conn.conn.queue().unwrap().into_iter().for_each(|x| {
8386 // if let Some(title) = x.title {
8487 // if let Some(artist) = x.artist {
@@ -91,7 +94,8 @@ impl App {
9194 // }
9295 // });
9396 conn.conn.queue().unwrap().into_iter().for_each(|x| {
94− vec.push(x.file);
97+ // vec.push(x.title.unwrap());
98+ vec.push(x);
9599 });
96100 }
97101
@@ -124,7 +128,7 @@ impl App {
124128
125129 let mut status = false;
126130 for (i, song) in self.queue_list.list.clone().iter().enumerate() {
127− if song.contains(file) {
131+ if song.file.contains(file) {
128132 self.conn.conn.delete(i as u32).unwrap();
129133 status = true;
130134 }
@@ -150,11 +154,11 @@ impl App {
150154 .queue_list
151155 .list
152156 .get(self.queue_list.index)
153− .unwrap()
157+ .unwrap().file
154158 .to_string();
155159
156160 for (i, song) in self.queue_list.list.clone().iter().enumerate() {
157− if song.contains(&file) {
161+ if song.file.contains(&file) {
158162 self.conn.conn.delete(i as u32).unwrap();
159163 if self.queue_list.index == self.queue_list.list.len() - 1
160164 && self.queue_list.index != 0
@@ -203,7 +207,7 @@ impl App {
203207 // .get(0)
204208 // .unwrap()
205209 // .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));
207211
208212 if index.is_some() {
209213 self.conn.conn.switch(index.unwrap() as u32)?;
M src/handler.rs View
@@ -34,13 +34,13 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
3434 .queue_list
3535 .list
3636 .iter()
37− .map(|f| f.as_str())
37+ .map(|f| f.file.as_str())
3838 .collect::<Vec<&str>>();
3939 let res: Vec<(&str, f32)> = fuzzy_search_sorted(&app.search_input, &list);
4040 let res = res.iter().map(|(x, _)| *x).collect::<Vec<&str>>();
4141
4242 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()) {
4444 app.queue_list.index = i;
4545 }
4646 }
@@ -162,7 +162,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
162162 match app.selected_tab {
163163 SelectedTab::Queue => {
164164 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;
166166
167167 if let Some(full_path) = app.conn.get_full_path(&short_path) {
168168 let song = app.conn.get_song_with_only_filename(&full_path);
M src/lib.rs View
@@ -24,3 +24,6 @@ pub mod handler;
2424
2525 /// Application
2626 pub mod app;
27+
28+/// The Queue structure
29+pub mod queue;
A src/queue.rs View
@@ -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+}
M src/ui.rs View
@@ -1,7 +1,6 @@
11 use std::time::Duration;
22
33 use crate::app::{App, SelectedTab};
4−use mpd::Song;
54 use ratatui::{
65 prelude::*,
76 widgets::{block::Title, *},
@@ -77,28 +76,22 @@ fn draw_directory_browser(frame: &mut Frame, app: &mut App, size: Rect) {
7776
7877 let mut status: bool = false;
7978 for sn in app.queue_list.list.iter() {
80− if sn.contains(s) {
79+ if sn.file.contains(s) {
8180 status = true;
8281 }
8382 }
8483
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+
8592 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− ]);
9393 row.magenta().bold()
9494 } 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− ]);
10295 row
10396 }
10497 } else {
@@ -153,17 +146,8 @@ fn draw_directory_browser(frame: &mut Frame, app: &mut App, size: Rect) {
153146
154147 /// draws playing queue
155148 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| {
157150 // 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();
167151 let title = song.clone().title.unwrap_or_else(|| song.clone().file);
168152 let artist = song.clone().artist.unwrap_or_default().cyan();
169153 let album = song
@@ -184,23 +168,17 @@ fn draw_queue(frame: &mut Frame, app: &mut App, size: Rect) {
184168
185169 let time = App::format_time(song.clone().duration.unwrap_or_else(|| Duration::new(0, 0)));
186170
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) {
195180 row.magenta().bold()
196181 } 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− ]);
204182 row
205183 }
206184 });