Krolyxon
Commit 6ec98cf

rmptui6ec98cf

move FileExtension to utils.rs

Author
krolxon
Date
2024-06-01 20:25 +0530
Parent
ebc4428
Commit
6ec98cf967cf0a7c2a4b49079b2fb55e0cb2c420

+44 −36

6 files changed

  1. M src/app.rs +2 −1
  2. M src/browser.rs +1 −19
  3. M src/connection.rs +5 −15
  4. M src/event/pl_append_keys.rs +1 −1
  5. M src/lib.rs +3 −0
  6. A src/utils.rs +32 −0
M src/app.rs View
@@ -1,6 +1,7 @@
11 use std::{path::Path, time::Duration};
22
3−use crate::browser::{FileBrowser, FileExtension};
3+use crate::browser::FileBrowser;
4+use crate::utils::FileExtension;
45 use crate::connection::Connection;
56 use crate::list::ContentList;
67 use crate::ui::InputMode;
M src/browser.rs View
@@ -1,9 +1,8 @@
1−use std::ffi::OsStr;
21 use std::path::Path;
32
43 use mpd::Song;
54
6−use crate::{app::AppResult, connection::Connection};
5+use crate::{app::AppResult, connection::Connection, utils::FileExtension};
76
87 #[derive(Debug)]
98 /// struct for working with directory browser tab in rmptui
@@ -16,23 +15,6 @@ pub struct FileBrowser {
1615 pub songs: Vec<Song>,
1716 }
1817
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−
3618 impl FileBrowser {
3719 pub fn new() -> FileBrowser {
3820 FileBrowser {
M src/connection.rs View
@@ -1,8 +1,8 @@
11 use crate::app::AppResult;
2+use crate::utils::is_installed;
23 use mpd::song::Song;
34 use mpd::{Client, State};
45 use simple_dmenu::dmenu;
5−use std::process::Command;
66 use std::time::Duration;
77
88 #[derive(Debug)]
@@ -192,20 +192,20 @@ impl Connection {
192192
193193 /// Toggle Repeat mode
194194 pub fn toggle_repeat(&mut self) {
195− let mode = self.conn.status().unwrap().repeat;
195+ let mode = self.status.repeat;
196196 self.conn.repeat(!mode).unwrap();
197197 }
198198
199199 /// Toggle random mode
200200 pub fn toggle_random(&mut self) {
201− let mode = self.conn.status().unwrap().random;
201+ let mode = self.status.random;
202202 self.conn.random(!mode).unwrap();
203203 }
204204
205205 // Volume controls
206206 /// Increase Volume
207207 pub fn inc_volume(&mut self, v: i8) {
208− let cur = self.conn.status().unwrap().volume;
208+ let cur = self.status.volume;
209209 if cur + v <= 100 {
210210 self.conn.volume(cur + v).unwrap();
211211 }
@@ -213,19 +213,9 @@ impl Connection {
213213
214214 /// Decrease volume
215215 pub fn dec_volume(&mut self, v: i8) {
216− let cur = self.conn.status().unwrap().volume;
216+ let cur = self.status.volume;
217217 if cur - v >= 0 {
218218 self.conn.volume(cur - v).unwrap();
219219 }
220220 }
221221 }
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−}
M src/event/pl_append_keys.rs View
@@ -1,5 +1,5 @@
11 use crate::app::{App, AppResult, SelectedTab};
2−use crate::browser::FileExtension;
2+use crate::utils::FileExtension;
33 use crossterm::event::{KeyCode, KeyEvent};
44 use std::path::Path;
55
M src/lib.rs View
@@ -18,3 +18,6 @@ pub mod event;
1818
1919 /// Application
2020 pub mod app;
21+
22+/// Utilities
23+pub mod utils;
A src/utils.rs View
@@ -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+