aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rwxr-xr-xsrc/connection.rs92
-rwxr-xr-xsrc/main.rs34
2 files changed, 126 insertions, 0 deletions
diff --git a/src/connection.rs b/src/connection.rs
new file mode 100755
index 0000000..ebc847f
--- /dev/null
+++ b/src/connection.rs
@@ -0,0 +1,92 @@
+use mpd::song::Song;
+use mpd::Client;
+
+pub struct Connection {
+ pub conn: Client,
+}
+
+impl Connection {
+ pub fn new(addrs: &str) -> Result<Self, mpd::error::Error> {
+ let conn = Client::connect(addrs)?;
+ Ok(Self { conn })
+ }
+
+ pub fn play_fzf(&mut self) {
+ let mut ss: Vec<String> = Vec::new();
+ self.get_file_tree_into_vec(&mut ss, ".", ".");
+
+ let choice: usize;
+ 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()) {
+ choice = index;
+ } else {
+ return;
+ }
+
+ let song = self.get_song(ss.get(choice).unwrap());
+
+ if self.conn.queue().unwrap().is_empty() {
+ self.push(song);
+ self.conn.play().unwrap();
+ } else {
+ self.push(song);
+ self.conn.next().unwrap();
+ }
+ }
+
+ fn get_file_tree_into_vec(&mut self, vec: &mut Vec<String>, path: &str, dir_append: &str) {
+ 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 {
+ file: filename.to_string(),
+ artist: None,
+ title: None,
+ duration: None,
+ last_mod: None,
+ name: None,
+ place: None,
+ range: None,
+ tags: vec![("".to_string(), "".to_string())],
+ }
+ }
+
+ pub fn status(&mut self) {
+ let current_song = self.conn.currentsong();
+ let status = self.conn.status().unwrap();
+
+ if current_song.is_ok() {
+ let song = current_song.unwrap();
+ if let Some(s) = song {
+ println!("{} - {}", s.artist.unwrap(), s.title.unwrap());
+ }
+ }
+ println!(
+ "volume: {}\trepeat: {}\trandom: {}\tsingle: {}\tconsume: {}",
+ status.volume, status.repeat, status.random, status.single, status.consume
+ );
+ }
+}
diff --git a/src/main.rs b/src/main.rs
new file mode 100755
index 0000000..48db1ba
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,34 @@
+mod connection;
+use clap::Parser;
+use connection::Connection;
+
+#[derive(Parser, Debug)]
+#[command(version, about, long_about = None)]
+struct Args {
+ /// Ignore case in search
+ #[arg(short, long, default_value = "false")]
+ pub pause: bool,
+
+ #[arg(short, long, default_value = "false")]
+ pub toggle_pause: bool,
+
+ #[arg(short, long, default_value = "false")]
+ pub show_status: bool,
+
+ #[arg(short, long, default_value = "false")]
+ pub fzf_select: bool,
+}
+
+fn main() -> Result<(), Box<dyn std::error::Error>> {
+ let args = Args::parse();
+ let mut conn = Connection::new("127.0.0.1:6600")?;
+ if args.show_status {
+ conn.status();
+ }
+
+ if args.fzf_select {
+ conn.play_fzf()
+ }
+
+ Ok(())
+}