move FileExtension to utils.rs
This commit is contained in:
parent
7ae0a2cc19
commit
15be9357da
|
|
@ -1,6 +1,7 @@
|
||||||
use std::{path::Path, time::Duration};
|
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::connection::Connection;
|
||||||
use crate::list::ContentList;
|
use crate::list::ContentList;
|
||||||
use crate::ui::InputMode;
|
use crate::ui::InputMode;
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,8 @@
|
||||||
use std::ffi::OsStr;
|
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use mpd::Song;
|
use mpd::Song;
|
||||||
|
|
||||||
use crate::{app::AppResult, connection::Connection};
|
use crate::{app::AppResult, connection::Connection, utils::FileExtension};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
/// struct for working with directory browser tab in rmptui
|
/// struct for working with directory browser tab in rmptui
|
||||||
|
|
@ -16,23 +15,6 @@ pub struct FileBrowser {
|
||||||
pub songs: Vec<Song>,
|
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 {
|
impl FileBrowser {
|
||||||
pub fn new() -> FileBrowser {
|
pub fn new() -> FileBrowser {
|
||||||
FileBrowser {
|
FileBrowser {
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
use crate::app::AppResult;
|
use crate::app::AppResult;
|
||||||
|
use crate::utils::is_installed;
|
||||||
use mpd::song::Song;
|
use mpd::song::Song;
|
||||||
use mpd::{Client, State};
|
use mpd::{Client, State};
|
||||||
use simple_dmenu::dmenu;
|
use simple_dmenu::dmenu;
|
||||||
use std::process::Command;
|
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|
@ -192,20 +192,20 @@ impl Connection {
|
||||||
|
|
||||||
/// Toggle Repeat mode
|
/// Toggle Repeat mode
|
||||||
pub fn toggle_repeat(&mut self) {
|
pub fn toggle_repeat(&mut self) {
|
||||||
let mode = self.conn.status().unwrap().repeat;
|
let mode = self.status.repeat;
|
||||||
self.conn.repeat(!mode).unwrap();
|
self.conn.repeat(!mode).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Toggle random mode
|
/// Toggle random mode
|
||||||
pub fn toggle_random(&mut self) {
|
pub fn toggle_random(&mut self) {
|
||||||
let mode = self.conn.status().unwrap().random;
|
let mode = self.status.random;
|
||||||
self.conn.random(!mode).unwrap();
|
self.conn.random(!mode).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Volume controls
|
// Volume controls
|
||||||
/// Increase Volume
|
/// Increase Volume
|
||||||
pub fn inc_volume(&mut self, v: i8) {
|
pub fn inc_volume(&mut self, v: i8) {
|
||||||
let cur = self.conn.status().unwrap().volume;
|
let cur = self.status.volume;
|
||||||
if cur + v <= 100 {
|
if cur + v <= 100 {
|
||||||
self.conn.volume(cur + v).unwrap();
|
self.conn.volume(cur + v).unwrap();
|
||||||
}
|
}
|
||||||
|
|
@ -213,19 +213,9 @@ impl Connection {
|
||||||
|
|
||||||
/// Decrease volume
|
/// Decrease volume
|
||||||
pub fn dec_volume(&mut self, v: i8) {
|
pub fn dec_volume(&mut self, v: i8) {
|
||||||
let cur = self.conn.status().unwrap().volume;
|
let cur = self.status.volume;
|
||||||
if cur - v >= 0 {
|
if cur - v >= 0 {
|
||||||
self.conn.volume(cur - v).unwrap();
|
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()
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::app::{App, AppResult, SelectedTab};
|
use crate::app::{App, AppResult, SelectedTab};
|
||||||
use crate::browser::FileExtension;
|
use crate::utils::FileExtension;
|
||||||
use crossterm::event::{KeyCode, KeyEvent};
|
use crossterm::event::{KeyCode, KeyEvent};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,3 +18,6 @@ pub mod event;
|
||||||
|
|
||||||
/// Application
|
/// Application
|
||||||
pub mod app;
|
pub mod app;
|
||||||
|
|
||||||
|
/// Utilities
|
||||||
|
pub mod utils;
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue