Krolyxon
Commit b29846f

rmptuib29846f

abide by the lord clippy

Author
krolxon
Date
2024-05-01 00:06 +0530
Parent
9c30356
Commit
b29846fb72326dc04bc13fe11d07965879787533

+70 −68

7 files changed

  1. M src/app.rs +15 −15
  2. M src/browser.rs +11 −3
  3. M src/connection.rs +9 −9
  4. M src/handler.rs +17 −14
  5. M src/list.rs +8 −21
  6. M src/queue.rs +8 −4
  7. M src/ui.rs +2 −2
M src/app.rs View
@@ -16,10 +16,10 @@ pub struct App {
1616 /// check if app is running
1717 pub running: bool,
1818 pub conn: Connection,
19− pub browser: FileBrowser, // Directory browser
20− pub queue_list: Queue, // Stores the current playing queue
21− pub pl_list: ContentList<String>, // Stores list of playlists
22− pub selected_tab: SelectedTab, // Used to switch between tabs
19+ pub browser: FileBrowser, // Directory browser
20+ pub queue_list: Queue, // Stores the current playing queue
21+ pub pl_list: ContentList<String>, // Stores list of playlists
22+ pub selected_tab: SelectedTab, // Used to switch between tabs
2323
2424 // Search
2525 pub inputmode: InputMode, // Defines input mode, Normal or Search
@@ -34,8 +34,6 @@ pub struct App {
3434 pub append_list: ContentList<String>,
3535 }
3636
37−
38−
3937 #[derive(Debug, PartialEq, Clone)]
4038 pub enum SelectedTab {
4139 DirectoryBrowser,
@@ -135,7 +133,7 @@ impl App {
135133 }
136134
137135 if !status {
138− if let Some(full_path) = &self.conn.get_full_path(&file) {
136+ if let Some(full_path) = &self.conn.get_full_path(file) {
139137 let song = self.conn.get_song_with_only_filename(full_path);
140138 self.conn.conn.push(&song)?;
141139 }
@@ -147,14 +145,15 @@ impl App {
147145 }
148146
149147 SelectedTab::Queue => {
150− if self.queue_list.list.len() == 0 {
148+ if self.queue_list.list.is_empty() {
151149 return Ok(());
152150 }
153151 let file = self
154152 .queue_list
155153 .list
156154 .get(self.queue_list.index)
157− .unwrap().file
155+ .unwrap()
156+ .file
158157 .to_string();
159158
160159 for (i, song) in self.queue_list.list.clone().iter().enumerate() {
@@ -207,7 +206,11 @@ impl App {
207206 // .get(0)
208207 // .unwrap()
209208 // .clone();
210− let index = self.queue_list.list.iter().position(|x| x.file.contains(path));
209+ let index = self
210+ .queue_list
211+ .list
212+ .iter()
213+ .position(|x| x.file.contains(path));
211214
212215 if index.is_some() {
213216 self.conn.conn.switch(index.unwrap() as u32)?;
@@ -349,11 +352,8 @@ impl App {
349352 }
350353
351354 pub fn change_playlist_name(&mut self) -> AppResult<()> {
352− match self.selected_tab {
353− SelectedTab::Playlists => {
354− self.inputmode = InputMode::PlaylistRename;
355− }
356− _ => {}
355+ if self.selected_tab == SelectedTab::Playlists {
356+ self.inputmode = InputMode::PlaylistRename;
357357 }
358358 Ok(())
359359 }
M src/browser.rs View
@@ -23,7 +23,7 @@ pub trait FileExtension {
2323
2424 impl<P: AsRef<Path>> FileExtension for P {
2525 fn has_extension<S: AsRef<str>>(&self, extensions: &[S]) -> bool {
26− if let Some(ref extension) = self.as_ref().extension().and_then(OsStr::to_str) {
26+ if let Some(extension) = self.as_ref().extension().and_then(OsStr::to_str) {
2727 return extensions
2828 .iter()
2929 .any(|x| x.as_ref().eq_ignore_ascii_case(extension));
@@ -84,7 +84,7 @@ impl FileBrowser {
8484 }]
8585 });
8686
87− self.songs.push(v.get(0).unwrap().clone());
87+ self.songs.push(v.first().unwrap().clone());
8888 } else {
8989 let v = Song {
9090 file: "".to_string(),
@@ -126,7 +126,7 @@ impl FileBrowser {
126126 /// handles going back event
127127 pub fn handle_go_back(&mut self, conn: &mut Connection) -> AppResult<()> {
128128 if self.prev_path != "." {
129− let r = self.path.rfind("/").unwrap();
129+ let r = self.path.rfind('/').unwrap();
130130 self.path = self.path.as_str()[..r].to_string();
131131 self.update_directory(conn)?;
132132 } else {
@@ -138,3 +138,11 @@ impl FileBrowser {
138138 Ok(())
139139 }
140140 }
141+
142+
143+
144+impl Default for FileBrowser {
145+ fn default() -> Self {
146+ Self::new()
147+ }
148+}
M src/connection.rs View
@@ -47,7 +47,7 @@ impl Connection {
4747 let current_song = conn
4848 .currentsong()
4949 .unwrap_or_else(|_| Some(empty_song.clone()))
50− .unwrap_or_else(|| empty_song);
50+ .unwrap_or(empty_song);
5151 Ok(Self {
5252 conn,
5353 songs_filenames,
@@ -63,10 +63,10 @@ impl Connection {
6363
6464 /// Fzf prompt for selecting song
6565 pub fn play_fzf(&mut self) -> Result<()> {
66− is_installed("fzf").map_err(|ex| ex)?;
66+ is_installed("fzf")?;
6767 let ss = &self.songs_filenames;
6868 let fzf_choice = rust_fzf::select(ss.clone(), Vec::new()).unwrap();
69− let index = get_choice_index(&self.songs_filenames, fzf_choice.get(0).unwrap());
69+ let index = get_choice_index(&self.songs_filenames, fzf_choice.first().unwrap());
7070 let song = self.get_song_with_only_filename(ss.get(index).unwrap());
7171 self.push(&song)?;
7272
@@ -75,7 +75,7 @@ impl Connection {
7575
7676 /// Dmenu prompt for selecting songs
7777 pub fn play_dmenu(&mut self) -> Result<()> {
78− is_installed("dmenu").map_err(|ex| ex)?;
78+ is_installed("dmenu")?;
7979 let ss: Vec<&str> = self.songs_filenames.iter().map(|x| x.as_str()).collect();
8080 let op = dmenu!(iter &ss; args "-p", "Choose a song: ", "-l", "30");
8181 let index = get_choice_index(&self.songs_filenames, &op);
@@ -92,7 +92,7 @@ impl Connection {
9292 .conn
9393 .currentsong()
9494 .unwrap_or_else(|_| Some(empty_song.clone()))
95− .unwrap_or_else(|| empty_song);
95+ .unwrap_or(empty_song);
9696
9797 // Playback State
9898 match status.state {
@@ -205,12 +205,12 @@ impl Connection {
205205 pub fn now_playing(&mut self) -> Result<Option<String>> {
206206 if let Some(s) = &self.current_song.title {
207207 if let Some(a) = &self.current_song.artist {
208− return Ok(Some(format!("\"{}\" By {}", s, a)));
208+ Ok(Some(format!("\"{}\" By {}", s, a)))
209209 } else {
210− return Ok(Some(s.to_string()));
210+ Ok(Some(s.to_string()))
211211 }
212212 } else {
213− return Ok(Some(self.current_song.file.clone()));
213+ Ok(Some(self.current_song.file.clone()))
214214 }
215215 }
216216
@@ -262,7 +262,7 @@ impl Connection {
262262 }
263263
264264 /// Gets the index of the string from the Vector
265−fn get_choice_index(ss: &Vec<String>, selection: &str) -> usize {
265+fn get_choice_index(ss: &[String], selection: &str) -> usize {
266266 let mut choice: usize = 0;
267267 if let Some(index) = ss.iter().position(|s| s == selection) {
268268 choice = index;
M src/handler.rs View
@@ -23,7 +23,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
2323 let res = res.iter().map(|(x, _)| *x).collect::<Vec<&str>>();
2424
2525 for (i, (_, item)) in app.browser.filetree.iter().enumerate() {
26− if item.contains(res.get(0).unwrap()) {
26+ if item.contains(res.first().unwrap()) {
2727 app.browser.selected = i;
2828 }
2929 }
@@ -40,7 +40,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
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.file.contains(res.get(0).unwrap()) {
43+ if item.file.contains(res.first().unwrap()) {
4444 app.queue_list.index = i;
4545 }
4646 }
@@ -57,7 +57,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
5757 let res = res.iter().map(|(x, _)| *x).collect::<Vec<&str>>();
5858
5959 for (i, item) in app.pl_list.list.iter().enumerate() {
60− if item.contains(res.get(0).unwrap()) {
60+ if item.contains(res.first().unwrap()) {
6161 app.pl_list.index = i;
6262 }
6363 }
@@ -83,7 +83,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
8383 .collect::<Vec<&str>>();
8484
8585 let res: Vec<(&str, f32)> = fuzzy_search_sorted(&app.search_input, &list);
86− let (res, _) = res.get(0).unwrap();
86+ let (res, _) = res.first().unwrap();
8787
8888 for (i, (_, item)) in app.browser.filetree.iter().enumerate() {
8989 if item.contains(res) {
@@ -121,7 +121,10 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
121121 app.enter_char(to_insert);
122122 }
123123 KeyCode::Enter => {
124− app.conn.conn.pl_rename(app.pl_list.list.get(app.pl_list.index).unwrap(), &app.pl_newname_input)?;
124+ app.conn.conn.pl_rename(
125+ app.pl_list.list.get(app.pl_list.index).unwrap(),
126+ &app.pl_newname_input,
127+ )?;
125128 app.pl_list.list = App::get_playlist(&mut app.conn.conn)?;
126129 app.pl_newname_input.clear();
127130 app.reset_cursor();
@@ -164,10 +167,10 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
164167 s_index = app.queue_list.index;
165168 let short_path = &app.queue_list.list.get(s_index).unwrap().file;
166169
167− if let Some(full_path) = app.conn.get_full_path(&short_path) {
170+ if let Some(full_path) = app.conn.get_full_path(short_path) {
168171 let song = app.conn.get_song_with_only_filename(&full_path);
169172
170− if pl_name.to_string() == "Current Playlist" {
173+ if *pl_name == "Current Playlist" {
171174 app.conn.conn.push(&song)?;
172175 app.update_queue();
173176 } else {
@@ -183,7 +186,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
183186 if let Some(full_path) = app.conn.get_full_path(short_path) {
184187 let song = app.conn.get_song_with_only_filename(&full_path);
185188
186− if pl_name.to_string() == "Current Playlist" {
189+ if *pl_name == "Current Playlist" {
187190 app.conn.conn.push(&song)?;
188191 app.update_queue();
189192 } else {
@@ -199,9 +202,9 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
199202 "wma", "mpc", "aiff", "dff", "mp2", "mka",
200203 ])
201204 {
202− let full_path = app.conn.get_full_path(&f).unwrap_or_default();
205+ let full_path = app.conn.get_full_path(f).unwrap_or_default();
203206 let song = app.conn.get_song_with_only_filename(&full_path);
204− if pl_name.to_string() == "Current Playlist" {
207+ if *pl_name == "Current Playlist" {
205208 app.conn.conn.push(&song)?;
206209 } else {
207210 app.conn.add_to_playlist(pl_name, &song)?;
@@ -371,10 +374,10 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
371374
372375 // Delete highlighted song from the queue
373376 KeyCode::Char('d') => {
374− if app.queue_list.index >= app.queue_list.list.len() - 1 {
375− if app.queue_list.index != 0 {
376− app.queue_list.index -= 1;
377− }
377+ if app.queue_list.index >= app.queue_list.list.len() - 1
378+ && app.queue_list.index != 0
379+ {
380+ app.queue_list.index -= 1;
378381 }
379382
380383 app.conn.conn.delete(app.queue_list.index as u32)?;
M src/list.rs View
@@ -15,20 +15,9 @@ impl<T> ContentList<T> {
1515 // Go to next item in list
1616 pub fn next(&mut self) {
1717 let len = self.list.len();
18− if len != 0 {
19− if self.index < len - 1 {
20− self.index += 1;
21− }
18+ if len != 0 && self.index < len - 1 {
19+ self.index += 1;
2220 }
23−
24− // let len = self.list.len();
25− // if len != 0 {
26− // if self.index == self.list.len() - 1 {
27− // self.index = 0;
28− // } else {
29− // self.index += 1;
30− // }
31− // }
3221 }
3322
3423 /// Go to previous item in list
@@ -36,17 +25,15 @@ impl<T> ContentList<T> {
3625 if self.index != 0 {
3726 self.index -= 1;
3827 }
39− // if self.index == 0 {
40− // let len = self.list.len();
41− // if len != 0 {
42− // self.index = len - 1;
43− // }
44− // } else {
45− // self.index -= 1;
46− // }
4728 }
4829
4930 pub fn reset_index(&mut self) {
5031 self.index = 0;
5132 }
5233 }
34+
35+impl<T> Default for ContentList<T> {
36+ fn default() -> Self {
37+ Self::new()
38+ }
39+}
M src/queue.rs View
@@ -17,10 +17,8 @@ impl Queue {
1717 // Go to next item in list
1818 pub fn next(&mut self) {
1919 let len = self.list.len();
20− if len != 0 {
21− if self.index < len - 1 {
22− self.index += 1;
23− }
20+ if len != 0 && self.index < len - 1 {
21+ self.index += 1;
2422 }
2523 }
2624
@@ -35,3 +33,9 @@ impl Queue {
3533 self.index = 0;
3634 }
3735 }
36+
37+impl Default for Queue {
38+ fn default() -> Self {
39+ Self::new()
40+ }
41+}
M src/ui.rs View
@@ -297,10 +297,10 @@ fn draw_progress_bar(frame: &mut Frame, app: &mut App, size: Rect) {
297297
298298 // Define the title
299299 let title = Block::default()
300− .title(Title::from(format!("{}", state).red().bold()))
300+ .title(Title::from(state.red().bold()))
301301 .title(Title::from(song.green().bold()))
302302 .title(Title::from(duration.cyan().bold()).alignment(Alignment::Right))
303− .title(Title::from(format!("{}", modes_bottom)).position(block::Position::Bottom))
303+ .title(Title::from(modes_bottom).position(block::Position::Bottom))
304304 .borders(Borders::ALL);
305305
306306 let progress_bar = LineGauge::default()