move FileExtension to utils.rs

This commit is contained in:
krolxon 2024-06-01 20:25:35 +05:30
parent 7ae0a2cc19
commit 15be9357da
6 changed files with 44 additions and 36 deletions

View File

@ -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;

View File

@ -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<Song>,
}
// https://stackoverflow.com/questions/72392835/check-if-a-file-is-of-a-given-type
pub trait FileExtension {
fn has_extension<S: AsRef<str>>(&self, extensions: &[S]) -> bool;
}
impl<P: AsRef<Path>> FileExtension for P {
fn has_extension<S: AsRef<str>>(&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 {

View File

@ -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()
}

View File

@ -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;

View File

@ -18,3 +18,6 @@ pub mod event;
/// Application
pub mod app;
/// Utilities
pub mod utils;

32
src/utils.rs Normal file
View File

@ -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<S: AsRef<str>>(&self, extensions: &[S]) -> bool;
}
impl<P: AsRef<Path>> FileExtension for P {
fn has_extension<S: AsRef<str>>(&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
}
}