add dmenu selector

This commit is contained in:
krolxon 2024-04-22 13:02:31 +05:30
parent e5e69281ca
commit 579b09b61d
4 changed files with 108 additions and 43 deletions

7
Cargo.lock generated
View File

@ -142,6 +142,7 @@ dependencies = [
"clap", "clap",
"mpd", "mpd",
"rust_fzf", "rust_fzf",
"simple-dmenu",
] ]
[[package]] [[package]]
@ -150,6 +151,12 @@ version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76a94b9c6a9880356cc6de038a52397509f8ac86a18c30d76da904b08fec2cb1" checksum = "76a94b9c6a9880356cc6de038a52397509f8ac86a18c30d76da904b08fec2cb1"
[[package]]
name = "simple-dmenu"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f392360283341235b298e79f0360bd924db6e6ad1e0964cbf4c43ea79c237eb"
[[package]] [[package]]
name = "strsim" name = "strsim"
version = "0.11.1" version = "0.11.1"

View File

@ -9,3 +9,4 @@ edition = "2021"
mpd = "0.1.0" mpd = "0.1.0"
clap = { version = "4.0.29", features = ["derive"] } clap = { version = "4.0.29", features = ["derive"] }
rust_fzf = "0.3.1" rust_fzf = "0.3.1"
simple-dmenu = "0.1.0"

View File

@ -1,66 +1,53 @@
use mpd::song::Song; use mpd::song::Song;
use mpd::Client; use mpd::Client;
use simple_dmenu::dmenu;
use std::process::Command;
pub struct Connection { pub struct Connection {
pub conn: Client, pub conn: Client,
pub songs_filenames: Vec<String>,
} }
impl Connection { impl Connection {
pub fn new(addrs: &str) -> Result<Self, mpd::error::Error> { pub fn new(addrs: &str) -> Result<Self, mpd::error::Error> {
let conn = Client::connect(addrs)?; let mut conn = Client::connect(addrs)?;
Ok(Self { conn }) let mut songs_filenames: Vec<String> = Vec::new();
get_file_tree_into_vec(&mut conn, &mut songs_filenames, ".", ".");
Ok(Self {
conn,
songs_filenames,
})
} }
pub fn play_fzf(&mut self) { pub fn play_fzf(&mut self) {
let mut ss: Vec<String> = Vec::new(); let ss = &self.songs_filenames;
self.get_file_tree_into_vec(&mut ss, ".", ".");
let choice: usize;
let fzf_choice = rust_fzf::select(ss.clone(), Vec::new()).unwrap(); let fzf_choice = rust_fzf::select(ss.clone(), Vec::new()).unwrap();
if let Some(index) = ss.iter().position(|s| s == fzf_choice.get(0).unwrap()) { let index = get_choice_index(&self.songs_filenames, fzf_choice.get(0).unwrap());
choice = index; let song = self.get_song_with_only_filename(ss.get(index).unwrap());
} else { self.push(&song);
return; }
}
let song = self.get_song(ss.get(choice).unwrap()); pub fn play_dmenu(&mut self) {
let ss: Vec<&str> = self.songs_filenames.iter().map(|x| x.as_str()).collect();
let op = dmenu!(iter &ss; args "-l", "30");
let index = get_choice_index(&self.songs_filenames, &op);
let song = self.get_song_with_only_filename(ss.get(index).unwrap());
self.push(&song);
}
fn push(&mut self, song: &Song) {
self.conn.push(song).unwrap();
if self.conn.queue().unwrap().is_empty() { if self.conn.queue().unwrap().is_empty() {
self.push(song); self.conn.push(song).unwrap();
self.conn.play().unwrap(); self.conn.play().unwrap();
} else { } else {
self.push(song); self.conn.push(song).unwrap();
self.conn.next().unwrap(); self.conn.next().unwrap();
} }
} }
fn get_file_tree_into_vec(&mut self, vec: &mut Vec<String>, path: &str, dir_append: &str) { fn get_song_with_only_filename(&self, filename: &str) -> Song {
let songs = self.conn.listfiles(path).unwrap_or_default();
for (i, s) in songs {
// Output of listfiles contains the last-modified thing, we dont want that
if i != "Last-Modified" {
if i == "directory" {
self.get_file_tree_into_vec(vec, &s, &s);
} else {
// We parse the string as float because the output of listfiles contains some random numbers, we dont want that
if !s.parse::<f32>().is_ok() {
let mut sam: String = String::new();
sam.push_str(dir_append);
sam.push_str(r"/");
sam.push_str(s.as_str());
vec.push(sam);
}
}
}
}
}
fn push(&mut self, song: Song) {
self.conn.push(song).unwrap();
}
fn get_song(&self, filename: &str) -> Song {
Song { Song {
file: filename.to_string(), file: filename.to_string(),
artist: None, artist: None,
@ -89,4 +76,54 @@ impl Connection {
status.volume, status.repeat, status.random, status.single, status.consume status.volume, status.repeat, status.random, status.single, status.consume
); );
} }
// Play controls
pub fn pause(&mut self) {
self.conn.pause(true).unwrap();
}
pub fn toggle_pause(&mut self) {
self.conn.toggle_pause().unwrap();
}
// Volume controls
pub fn inc_volume(&mut self, inc: i8) {
let cur = self.conn.status().unwrap().volume;
self.conn.volume(cur + inc).unwrap();
}
pub fn dec_volume(&mut self, dec: i8) {
let cur = self.conn.status().unwrap().volume;
self.conn.volume(cur - dec).unwrap();
}
}
fn get_file_tree_into_vec(conn: &mut Client, vec: &mut Vec<String>, path: &str, dir_append: &str) {
let songs = conn.listfiles(path).unwrap_or_default();
for (i, s) in songs {
// Output of listfiles contains the last-modified thing, we dont want that
if i != "Last-Modified" {
if i == "directory" {
get_file_tree_into_vec(conn, vec, &s, &s);
} else {
// We parse the string as float because the output of listfiles contains some random numbers, we dont want that
if !s.parse::<f32>().is_ok() {
let mut sam: String = String::new();
sam.push_str(dir_append);
sam.push_str(r"/");
sam.push_str(s.as_str());
vec.push(sam);
}
}
}
}
}
fn get_choice_index(ss: &Vec<String>, selection: &str) -> usize {
let mut choice: usize = 0;
if let Some(index) = ss.iter().position(|s| s == selection) {
choice = index;
}
choice
} }

View File

@ -5,29 +5,49 @@ use connection::Connection;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(version, about, long_about = None)] #[command(version, about, long_about = None)]
struct Args { struct Args {
/// Ignore case in search /// pause
#[arg(short, long, default_value = "false")] #[arg(short, long, default_value = "false")]
pub pause: bool, pub pause: bool,
/// toggle pause
#[arg(short, long, default_value = "false")] #[arg(short, long, default_value = "false")]
pub toggle_pause: bool, pub toggle_pause: bool,
/// show current status
#[arg(short, long, default_value = "false")] #[arg(short, long, default_value = "false")]
pub show_status: bool, pub show_status: bool,
/// use fzf selector for selecting songs
#[arg(short, long, default_value = "false")] #[arg(short, long, default_value = "false")]
pub fzf_select: bool, pub fzf_select: bool,
/// use dmenu selector for selecting songss
#[arg(short, long, default_value = "false")]
pub dmenu_select: bool,
} }
fn main() -> Result<(), Box<dyn std::error::Error>> { fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = Args::parse(); let args = Args::parse();
let mut conn = Connection::new("127.0.0.1:6600")?; let mut conn = Connection::new("127.0.0.1:6600")?;
if args.show_status { if args.show_status {
conn.status(); conn.status();
} }
if args.toggle_pause {
conn.toggle_pause();
}
if args.pause {
conn.pause();
}
if args.fzf_select { if args.fzf_select {
conn.play_fzf() conn.play_fzf();
}
if args.dmenu_select {
conn.play_dmenu();
} }
Ok(()) Ok(())