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

View File

@ -23,7 +23,7 @@ pub trait FileExtension {
impl<P: AsRef<Path>> FileExtension for P {
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
.iter()
.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 {
let v = Song {
file: "".to_string(),
@ -126,7 +126,7 @@ impl FileBrowser {
/// handles going back event
pub fn handle_go_back(&mut self, conn: &mut Connection) -> AppResult<()> {
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.update_directory(conn)?;
} else {
@ -138,3 +138,11 @@ impl FileBrowser {
Ok(())
}
}
impl Default for FileBrowser {
fn default() -> Self {
Self::new()
}
}

View File

@ -47,7 +47,7 @@ impl Connection {
let current_song = conn
.currentsong()
.unwrap_or_else(|_| Some(empty_song.clone()))
.unwrap_or_else(|| empty_song);
.unwrap_or(empty_song);
Ok(Self {
conn,
songs_filenames,
@ -63,10 +63,10 @@ impl Connection {
/// Fzf prompt for selecting song
pub fn play_fzf(&mut self) -> Result<()> {
is_installed("fzf").map_err(|ex| ex)?;
is_installed("fzf")?;
let ss = &self.songs_filenames;
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());
self.push(&song)?;
@ -75,7 +75,7 @@ impl Connection {
/// Dmenu prompt for selecting songs
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 op = dmenu!(iter &ss; args "-p", "Choose a song: ", "-l", "30");
let index = get_choice_index(&self.songs_filenames, &op);
@ -92,7 +92,7 @@ impl Connection {
.conn
.currentsong()
.unwrap_or_else(|_| Some(empty_song.clone()))
.unwrap_or_else(|| empty_song);
.unwrap_or(empty_song);
// Playback State
match status.state {
@ -205,12 +205,12 @@ impl Connection {
pub fn now_playing(&mut self) -> Result<Option<String>> {
if let Some(s) = &self.current_song.title {
if let Some(a) = &self.current_song.artist {
return Ok(Some(format!("\"{}\" By {}", s, a)));
Ok(Some(format!("\"{}\" By {}", s, a)))
} else {
return Ok(Some(s.to_string()));
Ok(Some(s.to_string()))
}
} 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
fn get_choice_index(ss: &Vec<String>, selection: &str) -> usize {
fn get_choice_index(ss: &[String], selection: &str) -> usize {
let mut choice: usize = 0;
if let Some(index) = ss.iter().position(|s| s == selection) {
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>>();
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;
}
}
@ -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>>();
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;
}
}
@ -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>>();
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;
}
}
@ -83,7 +83,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
.collect::<Vec<&str>>();
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() {
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);
}
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_newname_input.clear();
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;
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);
if pl_name.to_string() == "Current Playlist" {
if *pl_name == "Current Playlist" {
app.conn.conn.push(&song)?;
app.update_queue();
} 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) {
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.update_queue();
} else {
@ -199,9 +202,9 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
"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);
if pl_name.to_string() == "Current Playlist" {
if *pl_name == "Current Playlist" {
app.conn.conn.push(&song)?;
} else {
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
KeyCode::Char('d') => {
if app.queue_list.index >= app.queue_list.list.len() - 1 {
if app.queue_list.index != 0 {
app.queue_list.index -= 1;
}
if app.queue_list.index >= app.queue_list.list.len() - 1
&& app.queue_list.index != 0
{
app.queue_list.index -= 1;
}
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
pub fn next(&mut self) {
let len = self.list.len();
if len != 0 {
if self.index < len - 1 {
self.index += 1;
}
if len != 0 && self.index < len - 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
@ -36,17 +25,15 @@ impl<T> ContentList<T> {
if self.index != 0 {
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) {
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
pub fn next(&mut self) {
let len = self.list.len();
if len != 0 {
if self.index < len - 1 {
self.index += 1;
}
if len != 0 && self.index < len - 1 {
self.index += 1;
}
}
@ -35,3 +33,9 @@ impl Queue {
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
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(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);
let progress_bar = LineGauge::default()