rmptui6ec98cf
move FileExtension to utils.rs
+44 −36
6 files changed
- M src/app.rs +2 −1
- M src/browser.rs +1 −19
- M src/connection.rs +5 −15
- M src/event/pl_append_keys.rs +1 −1
- M src/lib.rs +3 −0
- A src/utils.rs +32 −0
| @@ -1,6 +1,7 @@ | |||
| 1 | 1 | use std::{path::Path, time::Duration}; | |
| 2 | 2 | ||
| 3 | − | use crate::browser::{FileBrowser, FileExtension}; | |
| 3 | + | use crate::browser::FileBrowser; | |
| 4 | + | use crate::utils::FileExtension; | |
| 4 | 5 | use crate::connection::Connection; | |
| 5 | 6 | use crate::list::ContentList; | |
| 6 | 7 | use crate::ui::InputMode; | |
| @@ -1,9 +1,8 @@ | |||
| 1 | − | use std::ffi::OsStr; | |
| 2 | 1 | use std::path::Path; | |
| 3 | 2 | ||
| 4 | 3 | use mpd::Song; | |
| 5 | 4 | ||
| 6 | − | use crate::{app::AppResult, connection::Connection}; | |
| 5 | + | use crate::{app::AppResult, connection::Connection, utils::FileExtension}; | |
| 7 | 6 | ||
| 8 | 7 | #[derive(Debug)] | |
| 9 | 8 | /// struct for working with directory browser tab in rmptui | |
| @@ -16,23 +15,6 @@ pub struct FileBrowser { | |||
| 16 | 15 | pub songs: Vec<Song>, | |
| 17 | 16 | } | |
| 18 | 17 | ||
| 19 | − | // https://stackoverflow.com/questions/72392835/check-if-a-file-is-of-a-given-type | |
| 20 | − | pub trait FileExtension { | |
| 21 | − | fn has_extension<S: AsRef<str>>(&self, extensions: &[S]) -> bool; | |
| 22 | − | } | |
| 23 | − | ||
| 24 | − | impl<P: AsRef<Path>> FileExtension for P { | |
| 25 | − | fn has_extension<S: AsRef<str>>(&self, extensions: &[S]) -> bool { | |
| 26 | − | if let Some(extension) = self.as_ref().extension().and_then(OsStr::to_str) { | |
| 27 | − | return extensions | |
| 28 | − | .iter() | |
| 29 | − | .any(|x| x.as_ref().eq_ignore_ascii_case(extension)); | |
| 30 | − | } | |
| 31 | − | ||
| 32 | − | false | |
| 33 | − | } | |
| 34 | − | } | |
| 35 | − | ||
| 36 | 18 | impl FileBrowser { | |
| 37 | 19 | pub fn new() -> FileBrowser { | |
| 38 | 20 | FileBrowser { | |
| @@ -1,8 +1,8 @@ | |||
| 1 | 1 | use crate::app::AppResult; | |
| 2 | + | use crate::utils::is_installed; | |
| 2 | 3 | use mpd::song::Song; | |
| 3 | 4 | use mpd::{Client, State}; | |
| 4 | 5 | use simple_dmenu::dmenu; | |
| 5 | − | use std::process::Command; | |
| 6 | 6 | use std::time::Duration; | |
| 7 | 7 | ||
| 8 | 8 | #[derive(Debug)] | |
| @@ -192,20 +192,20 @@ impl Connection { | |||
| 192 | 192 | ||
| 193 | 193 | /// Toggle Repeat mode | |
| 194 | 194 | pub fn toggle_repeat(&mut self) { | |
| 195 | − | let mode = self.conn.status().unwrap().repeat; | |
| 195 | + | let mode = self.status.repeat; | |
| 196 | 196 | self.conn.repeat(!mode).unwrap(); | |
| 197 | 197 | } | |
| 198 | 198 | ||
| 199 | 199 | /// Toggle random mode | |
| 200 | 200 | pub fn toggle_random(&mut self) { | |
| 201 | − | let mode = self.conn.status().unwrap().random; | |
| 201 | + | let mode = self.status.random; | |
| 202 | 202 | self.conn.random(!mode).unwrap(); | |
| 203 | 203 | } | |
| 204 | 204 | ||
| 205 | 205 | // Volume controls | |
| 206 | 206 | /// Increase Volume | |
| 207 | 207 | pub fn inc_volume(&mut self, v: i8) { | |
| 208 | − | let cur = self.conn.status().unwrap().volume; | |
| 208 | + | let cur = self.status.volume; | |
| 209 | 209 | if cur + v <= 100 { | |
| 210 | 210 | self.conn.volume(cur + v).unwrap(); | |
| 211 | 211 | } | |
| @@ -213,19 +213,9 @@ impl Connection { | |||
| 213 | 213 | ||
| 214 | 214 | /// Decrease volume | |
| 215 | 215 | pub fn dec_volume(&mut self, v: i8) { | |
| 216 | − | let cur = self.conn.status().unwrap().volume; | |
| 216 | + | let cur = self.status.volume; | |
| 217 | 217 | if cur - v >= 0 { | |
| 218 | 218 | self.conn.volume(cur - v).unwrap(); | |
| 219 | 219 | } | |
| 220 | 220 | } | |
| 221 | 221 | } | |
| 222 | − | ||
| 223 | − | /// Checks if given program is installed in your system | |
| 224 | − | fn is_installed(ss: &str) -> bool { | |
| 225 | − | let output = Command::new("which") | |
| 226 | − | .arg(ss) | |
| 227 | − | .output() | |
| 228 | − | .expect("Failed to execute command"); | |
| 229 | − | ||
| 230 | − | output.status.success() | |
| 231 | − | } | |
| @@ -1,5 +1,5 @@ | |||
| 1 | 1 | use crate::app::{App, AppResult, SelectedTab}; | |
| 2 | − | use crate::browser::FileExtension; | |
| 2 | + | use crate::utils::FileExtension; | |
| 3 | 3 | use crossterm::event::{KeyCode, KeyEvent}; | |
| 4 | 4 | use std::path::Path; | |
| 5 | 5 | ||
| @@ -18,3 +18,6 @@ pub mod event; | |||
| 18 | 18 | ||
| 19 | 19 | /// Application | |
| 20 | 20 | pub mod app; | |
| 21 | + | ||
| 22 | + | /// Utilities | |
| 23 | + | pub mod utils; | |
| @@ -0,0 +1,32 @@ | |||
| 1 | + | use std::process::Command; | |
| 2 | + | use std::ffi::OsStr; | |
| 3 | + | use std::path::Path; | |
| 4 | + | ||
| 5 | + | /// Checks if given program is installed in your system | |
| 6 | + | pub fn is_installed(ss: &str) -> bool { | |
| 7 | + | let output = Command::new("which") | |
| 8 | + | .arg(ss) | |
| 9 | + | .output() | |
| 10 | + | .expect("Failed to execute command"); | |
| 11 | + | ||
| 12 | + | output.status.success() | |
| 13 | + | } | |
| 14 | + | ||
| 15 | + | /// Checks if a file has a given extension | |
| 16 | + | // https://stackoverflow.com/questions/72392835/check-if-a-file-is-of-a-given-type | |
| 17 | + | pub trait FileExtension { | |
| 18 | + | fn has_extension<S: AsRef<str>>(&self, extensions: &[S]) -> bool; | |
| 19 | + | } | |
| 20 | + | ||
| 21 | + | impl<P: AsRef<Path>> FileExtension for P { | |
| 22 | + | fn has_extension<S: AsRef<str>>(&self, extensions: &[S]) -> bool { | |
| 23 | + | if let Some(extension) = self.as_ref().extension().and_then(OsStr::to_str) { | |
| 24 | + | return extensions | |
| 25 | + | .iter() | |
| 26 | + | .any(|x| x.as_ref().eq_ignore_ascii_case(extension)); | |
| 27 | + | } | |
| 28 | + | ||
| 29 | + | false | |
| 30 | + | } | |
| 31 | + | } | |
| 32 | + | ||