abide by the lord clippy

This commit is contained in:
krolxon 2024-05-01 00:06:50 +05:30
parent 9c30356a3e
commit b29846fb72
7 changed files with 70 additions and 68 deletions

View File

@ -16,10 +16,10 @@ pub struct App {
/// check if app is running /// check if app is running
pub running: bool, pub running: bool,
pub conn: Connection, pub conn: Connection,
pub browser: FileBrowser, // Directory browser pub browser: FileBrowser, // Directory browser
pub queue_list: Queue, // Stores the current playing queue pub queue_list: Queue, // Stores the current playing queue
pub pl_list: ContentList<String>, // Stores list of playlists pub pl_list: ContentList<String>, // Stores list of playlists
pub selected_tab: SelectedTab, // Used to switch between tabs pub selected_tab: SelectedTab, // Used to switch between tabs
// Search // Search
pub inputmode: InputMode, // Defines input mode, Normal or Search pub inputmode: InputMode, // Defines input mode, Normal or Search
@ -34,8 +34,6 @@ pub struct App {
pub append_list: ContentList<String>, pub append_list: ContentList<String>,
} }
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
pub enum SelectedTab { pub enum SelectedTab {
DirectoryBrowser, DirectoryBrowser,
@ -135,7 +133,7 @@ impl App {
} }
if !status { if !status {
if let Some(full_path) = &self.conn.get_full_path(&file) { if let Some(full_path) = &self.conn.get_full_path(file) {
let song = self.conn.get_song_with_only_filename(full_path); let song = self.conn.get_song_with_only_filename(full_path);
self.conn.conn.push(&song)?; self.conn.conn.push(&song)?;
} }
@ -147,14 +145,15 @@ impl App {
} }
SelectedTab::Queue => { SelectedTab::Queue => {
if self.queue_list.list.len() == 0 { if self.queue_list.list.is_empty() {
return Ok(()); return Ok(());
} }
let file = self let file = self
.queue_list .queue_list
.list .list
.get(self.queue_list.index) .get(self.queue_list.index)
.unwrap().file .unwrap()
.file
.to_string(); .to_string();
for (i, song) in self.queue_list.list.clone().iter().enumerate() { for (i, song) in self.queue_list.list.clone().iter().enumerate() {
@ -207,7 +206,11 @@ impl App {
// .get(0) // .get(0)
// .unwrap() // .unwrap()
// .clone(); // .clone();
let index = self.queue_list.list.iter().position(|x| x.file.contains(path)); let index = self
.queue_list
.list
.iter()
.position(|x| x.file.contains(path));
if index.is_some() { if index.is_some() {
self.conn.conn.switch(index.unwrap() as u32)?; self.conn.conn.switch(index.unwrap() as u32)?;
@ -349,11 +352,8 @@ impl App {
} }
pub fn change_playlist_name(&mut self) -> AppResult<()> { pub fn change_playlist_name(&mut self) -> AppResult<()> {
match self.selected_tab { if self.selected_tab == SelectedTab::Playlists {
SelectedTab::Playlists => { self.inputmode = InputMode::PlaylistRename;
self.inputmode = InputMode::PlaylistRename;
}
_ => {}
} }
Ok(()) Ok(())
} }

View File

@ -23,7 +23,7 @@ pub trait FileExtension {
impl<P: AsRef<Path>> FileExtension for P { impl<P: AsRef<Path>> FileExtension for P {
fn has_extension<S: AsRef<str>>(&self, extensions: &[S]) -> bool { fn has_extension<S: AsRef<str>>(&self, extensions: &[S]) -> bool {
if let Some(ref extension) = self.as_ref().extension().and_then(OsStr::to_str) { if let Some(extension) = self.as_ref().extension().and_then(OsStr::to_str) {
return extensions return extensions
.iter() .iter()
.any(|x| x.as_ref().eq_ignore_ascii_case(extension)); .any(|x| x.as_ref().eq_ignore_ascii_case(extension));
@ -84,7 +84,7 @@ impl FileBrowser {
}] }]
}); });
self.songs.push(v.get(0).unwrap().clone()); self.songs.push(v.first().unwrap().clone());
} else { } else {
let v = Song { let v = Song {
file: "".to_string(), file: "".to_string(),
@ -126,7 +126,7 @@ impl FileBrowser {
/// handles going back event /// handles going back event
pub fn handle_go_back(&mut self, conn: &mut Connection) -> AppResult<()> { pub fn handle_go_back(&mut self, conn: &mut Connection) -> AppResult<()> {
if self.prev_path != "." { if self.prev_path != "." {
let r = self.path.rfind("/").unwrap(); let r = self.path.rfind('/').unwrap();
self.path = self.path.as_str()[..r].to_string(); self.path = self.path.as_str()[..r].to_string();
self.update_directory(conn)?; self.update_directory(conn)?;
} else { } else {
@ -138,3 +138,11 @@ impl FileBrowser {
Ok(()) Ok(())
} }
} }
impl Default for FileBrowser {
fn default() -> Self {
Self::new()
}
}

View File

@ -47,7 +47,7 @@ impl Connection {
let current_song = conn let current_song = conn
.currentsong() .currentsong()
.unwrap_or_else(|_| Some(empty_song.clone())) .unwrap_or_else(|_| Some(empty_song.clone()))
.unwrap_or_else(|| empty_song); .unwrap_or(empty_song);
Ok(Self { Ok(Self {
conn, conn,
songs_filenames, songs_filenames,
@ -63,10 +63,10 @@ impl Connection {
/// Fzf prompt for selecting song /// Fzf prompt for selecting song
pub fn play_fzf(&mut self) -> Result<()> { pub fn play_fzf(&mut self) -> Result<()> {
is_installed("fzf").map_err(|ex| ex)?; is_installed("fzf")?;
let ss = &self.songs_filenames; let ss = &self.songs_filenames;
let fzf_choice = rust_fzf::select(ss.clone(), Vec::new()).unwrap(); let fzf_choice = rust_fzf::select(ss.clone(), Vec::new()).unwrap();
let index = get_choice_index(&self.songs_filenames, fzf_choice.get(0).unwrap()); let index = get_choice_index(&self.songs_filenames, fzf_choice.first().unwrap());
let song = self.get_song_with_only_filename(ss.get(index).unwrap()); let song = self.get_song_with_only_filename(ss.get(index).unwrap());
self.push(&song)?; self.push(&song)?;
@ -75,7 +75,7 @@ impl Connection {
/// Dmenu prompt for selecting songs /// Dmenu prompt for selecting songs
pub fn play_dmenu(&mut self) -> Result<()> { pub fn play_dmenu(&mut self) -> Result<()> {
is_installed("dmenu").map_err(|ex| ex)?; is_installed("dmenu")?;
let ss: Vec<&str> = self.songs_filenames.iter().map(|x| x.as_str()).collect(); let ss: Vec<&str> = self.songs_filenames.iter().map(|x| x.as_str()).collect();
let op = dmenu!(iter &ss; args "-p", "Choose a song: ", "-l", "30"); let op = dmenu!(iter &ss; args "-p", "Choose a song: ", "-l", "30");
let index = get_choice_index(&self.songs_filenames, &op); let index = get_choice_index(&self.songs_filenames, &op);
@ -92,7 +92,7 @@ impl Connection {
.conn .conn
.currentsong() .currentsong()
.unwrap_or_else(|_| Some(empty_song.clone())) .unwrap_or_else(|_| Some(empty_song.clone()))
.unwrap_or_else(|| empty_song); .unwrap_or(empty_song);
// Playback State // Playback State
match status.state { match status.state {
@ -205,12 +205,12 @@ impl Connection {
pub fn now_playing(&mut self) -> Result<Option<String>> { pub fn now_playing(&mut self) -> Result<Option<String>> {
if let Some(s) = &self.current_song.title { if let Some(s) = &self.current_song.title {
if let Some(a) = &self.current_song.artist { if let Some(a) = &self.current_song.artist {
return Ok(Some(format!("\"{}\" By {}", s, a))); Ok(Some(format!("\"{}\" By {}", s, a)))
} else { } else {
return Ok(Some(s.to_string())); Ok(Some(s.to_string()))
} }
} else { } else {
return Ok(Some(self.current_song.file.clone())); Ok(Some(self.current_song.file.clone()))
} }
} }
@ -262,7 +262,7 @@ impl Connection {
} }
/// Gets the index of the string from the Vector /// Gets the index of the string from the Vector
fn get_choice_index(ss: &Vec<String>, selection: &str) -> usize { fn get_choice_index(ss: &[String], selection: &str) -> usize {
let mut choice: usize = 0; let mut choice: usize = 0;
if let Some(index) = ss.iter().position(|s| s == selection) { if let Some(index) = ss.iter().position(|s| s == selection) {
choice = index; choice = index;

View File

@ -23,7 +23,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
let res = res.iter().map(|(x, _)| *x).collect::<Vec<&str>>(); let res = res.iter().map(|(x, _)| *x).collect::<Vec<&str>>();
for (i, (_, item)) in app.browser.filetree.iter().enumerate() { for (i, (_, item)) in app.browser.filetree.iter().enumerate() {
if item.contains(res.get(0).unwrap()) { if item.contains(res.first().unwrap()) {
app.browser.selected = i; app.browser.selected = i;
} }
} }
@ -40,7 +40,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
let res = res.iter().map(|(x, _)| *x).collect::<Vec<&str>>(); let res = res.iter().map(|(x, _)| *x).collect::<Vec<&str>>();
for (i, item) in app.queue_list.list.iter().enumerate() { for (i, item) in app.queue_list.list.iter().enumerate() {
if item.file.contains(res.get(0).unwrap()) { if item.file.contains(res.first().unwrap()) {
app.queue_list.index = i; app.queue_list.index = i;
} }
} }
@ -57,7 +57,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
let res = res.iter().map(|(x, _)| *x).collect::<Vec<&str>>(); let res = res.iter().map(|(x, _)| *x).collect::<Vec<&str>>();
for (i, item) in app.pl_list.list.iter().enumerate() { for (i, item) in app.pl_list.list.iter().enumerate() {
if item.contains(res.get(0).unwrap()) { if item.contains(res.first().unwrap()) {
app.pl_list.index = i; app.pl_list.index = i;
} }
} }
@ -83,7 +83,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
.collect::<Vec<&str>>(); .collect::<Vec<&str>>();
let res: Vec<(&str, f32)> = fuzzy_search_sorted(&app.search_input, &list); let res: Vec<(&str, f32)> = fuzzy_search_sorted(&app.search_input, &list);
let (res, _) = res.get(0).unwrap(); let (res, _) = res.first().unwrap();
for (i, (_, item)) in app.browser.filetree.iter().enumerate() { for (i, (_, item)) in app.browser.filetree.iter().enumerate() {
if item.contains(res) { if item.contains(res) {
@ -121,7 +121,10 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
app.enter_char(to_insert); app.enter_char(to_insert);
} }
KeyCode::Enter => { KeyCode::Enter => {
app.conn.conn.pl_rename(app.pl_list.list.get(app.pl_list.index).unwrap(), &app.pl_newname_input)?; app.conn.conn.pl_rename(
app.pl_list.list.get(app.pl_list.index).unwrap(),
&app.pl_newname_input,
)?;
app.pl_list.list = App::get_playlist(&mut app.conn.conn)?; app.pl_list.list = App::get_playlist(&mut app.conn.conn)?;
app.pl_newname_input.clear(); app.pl_newname_input.clear();
app.reset_cursor(); app.reset_cursor();
@ -164,10 +167,10 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
s_index = app.queue_list.index; s_index = app.queue_list.index;
let short_path = &app.queue_list.list.get(s_index).unwrap().file; let short_path = &app.queue_list.list.get(s_index).unwrap().file;
if let Some(full_path) = app.conn.get_full_path(&short_path) { if let Some(full_path) = app.conn.get_full_path(short_path) {
let song = app.conn.get_song_with_only_filename(&full_path); let song = app.conn.get_song_with_only_filename(&full_path);
if pl_name.to_string() == "Current Playlist" { if *pl_name == "Current Playlist" {
app.conn.conn.push(&song)?; app.conn.conn.push(&song)?;
app.update_queue(); app.update_queue();
} else { } else {
@ -183,7 +186,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
if let Some(full_path) = app.conn.get_full_path(short_path) { if let Some(full_path) = app.conn.get_full_path(short_path) {
let song = app.conn.get_song_with_only_filename(&full_path); let song = app.conn.get_song_with_only_filename(&full_path);
if pl_name.to_string() == "Current Playlist" { if *pl_name == "Current Playlist" {
app.conn.conn.push(&song)?; app.conn.conn.push(&song)?;
app.update_queue(); app.update_queue();
} else { } else {
@ -199,9 +202,9 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
"wma", "mpc", "aiff", "dff", "mp2", "mka", "wma", "mpc", "aiff", "dff", "mp2", "mka",
]) ])
{ {
let full_path = app.conn.get_full_path(&f).unwrap_or_default(); let full_path = app.conn.get_full_path(f).unwrap_or_default();
let song = app.conn.get_song_with_only_filename(&full_path); let song = app.conn.get_song_with_only_filename(&full_path);
if pl_name.to_string() == "Current Playlist" { if *pl_name == "Current Playlist" {
app.conn.conn.push(&song)?; app.conn.conn.push(&song)?;
} else { } else {
app.conn.add_to_playlist(pl_name, &song)?; app.conn.add_to_playlist(pl_name, &song)?;
@ -371,10 +374,10 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
// Delete highlighted song from the queue // Delete highlighted song from the queue
KeyCode::Char('d') => { KeyCode::Char('d') => {
if app.queue_list.index >= app.queue_list.list.len() - 1 { if app.queue_list.index >= app.queue_list.list.len() - 1
if app.queue_list.index != 0 { && app.queue_list.index != 0
app.queue_list.index -= 1; {
} app.queue_list.index -= 1;
} }
app.conn.conn.delete(app.queue_list.index as u32)?; app.conn.conn.delete(app.queue_list.index as u32)?;

View File

@ -15,20 +15,9 @@ impl<T> ContentList<T> {
// Go to next item in list // Go to next item in list
pub fn next(&mut self) { pub fn next(&mut self) {
let len = self.list.len(); let len = self.list.len();
if len != 0 { if len != 0 && self.index < len - 1 {
if self.index < len - 1 { self.index += 1;
self.index += 1;
}
} }
// let len = self.list.len();
// if len != 0 {
// if self.index == self.list.len() - 1 {
// self.index = 0;
// } else {
// self.index += 1;
// }
// }
} }
/// Go to previous item in list /// Go to previous item in list
@ -36,17 +25,15 @@ impl<T> ContentList<T> {
if self.index != 0 { if self.index != 0 {
self.index -= 1; self.index -= 1;
} }
// if self.index == 0 {
// let len = self.list.len();
// if len != 0 {
// self.index = len - 1;
// }
// } else {
// self.index -= 1;
// }
} }
pub fn reset_index(&mut self) { pub fn reset_index(&mut self) {
self.index = 0; self.index = 0;
} }
} }
impl<T> Default for ContentList<T> {
fn default() -> Self {
Self::new()
}
}

View File

@ -17,10 +17,8 @@ impl Queue {
// Go to next item in list // Go to next item in list
pub fn next(&mut self) { pub fn next(&mut self) {
let len = self.list.len(); let len = self.list.len();
if len != 0 { if len != 0 && self.index < len - 1 {
if self.index < len - 1 { self.index += 1;
self.index += 1;
}
} }
} }
@ -35,3 +33,9 @@ impl Queue {
self.index = 0; self.index = 0;
} }
} }
impl Default for Queue {
fn default() -> Self {
Self::new()
}
}

View File

@ -297,10 +297,10 @@ fn draw_progress_bar(frame: &mut Frame, app: &mut App, size: Rect) {
// Define the title // Define the title
let title = Block::default() let title = Block::default()
.title(Title::from(format!("{}", state).red().bold())) .title(Title::from(state.red().bold()))
.title(Title::from(song.green().bold())) .title(Title::from(song.green().bold()))
.title(Title::from(duration.cyan().bold()).alignment(Alignment::Right)) .title(Title::from(duration.cyan().bold()).alignment(Alignment::Right))
.title(Title::from(format!("{}", modes_bottom)).position(block::Position::Bottom)) .title(Title::from(modes_bottom).position(block::Position::Bottom))
.borders(Borders::ALL); .borders(Borders::ALL);
let progress_bar = LineGauge::default() let progress_bar = LineGauge::default()