diff --git a/src/app.rs b/src/app.rs index 06d76ce..bce9b47 100755 --- a/src/app.rs +++ b/src/app.rs @@ -1,6 +1,7 @@ use std::{path::Path, time::Duration}; -use crate::browser::{FileBrowser, FileExtension}; +use crate::browser::FileBrowser; +use crate::utils::FileExtension; use crate::connection::Connection; use crate::list::ContentList; use crate::ui::InputMode; diff --git a/src/browser.rs b/src/browser.rs index 4cc5a56..92d3ca9 100755 --- a/src/browser.rs +++ b/src/browser.rs @@ -1,9 +1,8 @@ -use std::ffi::OsStr; use std::path::Path; use mpd::Song; -use crate::{app::AppResult, connection::Connection}; +use crate::{app::AppResult, connection::Connection, utils::FileExtension}; #[derive(Debug)] /// struct for working with directory browser tab in rmptui @@ -16,23 +15,6 @@ pub struct FileBrowser { pub songs: Vec, } -// https://stackoverflow.com/questions/72392835/check-if-a-file-is-of-a-given-type -pub trait FileExtension { - fn has_extension>(&self, extensions: &[S]) -> bool; -} - -impl> FileExtension for P { - fn has_extension>(&self, extensions: &[S]) -> bool { - 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)); - } - - false - } -} - impl FileBrowser { pub fn new() -> FileBrowser { FileBrowser { diff --git a/src/connection.rs b/src/connection.rs index f6f89cd..6d48080 100755 --- a/src/connection.rs +++ b/src/connection.rs @@ -1,8 +1,8 @@ use crate::app::AppResult; +use crate::utils::is_installed; use mpd::song::Song; use mpd::{Client, State}; use simple_dmenu::dmenu; -use std::process::Command; use std::time::Duration; #[derive(Debug)] @@ -192,20 +192,20 @@ impl Connection { /// Toggle Repeat mode pub fn toggle_repeat(&mut self) { - let mode = self.conn.status().unwrap().repeat; + let mode = self.status.repeat; self.conn.repeat(!mode).unwrap(); } /// Toggle random mode pub fn toggle_random(&mut self) { - let mode = self.conn.status().unwrap().random; + let mode = self.status.random; self.conn.random(!mode).unwrap(); } // Volume controls /// Increase Volume pub fn inc_volume(&mut self, v: i8) { - let cur = self.conn.status().unwrap().volume; + let cur = self.status.volume; if cur + v <= 100 { self.conn.volume(cur + v).unwrap(); } @@ -213,19 +213,9 @@ impl Connection { /// Decrease volume pub fn dec_volume(&mut self, v: i8) { - let cur = self.conn.status().unwrap().volume; + let cur = self.status.volume; if cur - v >= 0 { self.conn.volume(cur - v).unwrap(); } } } - -/// Checks if given program is installed in your system -fn is_installed(ss: &str) -> bool { - let output = Command::new("which") - .arg(ss) - .output() - .expect("Failed to execute command"); - - output.status.success() -} diff --git a/src/event/pl_append_keys.rs b/src/event/pl_append_keys.rs index 4a1be79..aeefc4c 100755 --- a/src/event/pl_append_keys.rs +++ b/src/event/pl_append_keys.rs @@ -1,5 +1,5 @@ use crate::app::{App, AppResult, SelectedTab}; -use crate::browser::FileExtension; +use crate::utils::FileExtension; use crossterm::event::{KeyCode, KeyEvent}; use std::path::Path; diff --git a/src/lib.rs b/src/lib.rs index 6744922..eb4168c 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,3 +18,6 @@ pub mod event; /// Application pub mod app; + +/// Utilities +pub mod utils; diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..b8c1bea --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,32 @@ +use std::process::Command; +use std::ffi::OsStr; +use std::path::Path; + +/// Checks if given program is installed in your system +pub fn is_installed(ss: &str) -> bool { + let output = Command::new("which") + .arg(ss) + .output() + .expect("Failed to execute command"); + + output.status.success() +} + +/// Checks if a file has a given extension +// https://stackoverflow.com/questions/72392835/check-if-a-file-is-of-a-given-type +pub trait FileExtension { + fn has_extension>(&self, extensions: &[S]) -> bool; +} + +impl> FileExtension for P { + fn has_extension>(&self, extensions: &[S]) -> bool { + 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)); + } + + false + } +} +