Krolyxon
Commit 579b09b

rmptuisrcconnection.rs

129 lines · 4.0 KB · Rust

1use mpd::song::Song;
2use mpd::Client;
3use simple_dmenu::dmenu;
4use std::process::Command;
5
6pub struct Connection {
7 pub conn: Client,
8 pub songs_filenames: Vec<String>,
9}
10
11impl Connection {
12 pub fn new(addrs: &str) -> Result<Self, mpd::error::Error> {
13 let mut conn = Client::connect(addrs)?;
14 let mut songs_filenames: Vec<String> = Vec::new();
15 get_file_tree_into_vec(&mut conn, &mut songs_filenames, ".", ".");
16
17 Ok(Self {
18 conn,
19 songs_filenames,
20 })
21 }
22
23 pub fn play_fzf(&mut self) {
24 let ss = &self.songs_filenames;
25 let fzf_choice = rust_fzf::select(ss.clone(), Vec::new()).unwrap();
26 let index = get_choice_index(&self.songs_filenames, fzf_choice.get(0).unwrap());
27 let song = self.get_song_with_only_filename(ss.get(index).unwrap());
28 self.push(&song);
29 }
30
31 pub fn play_dmenu(&mut self) {
32 let ss: Vec<&str> = self.songs_filenames.iter().map(|x| x.as_str()).collect();
33 let op = dmenu!(iter &ss; args "-l", "30");
34 let index = get_choice_index(&self.songs_filenames, &op);
35 let song = self.get_song_with_only_filename(ss.get(index).unwrap());
36 self.push(&song);
37 }
38
39 fn push(&mut self, song: &Song) {
40 self.conn.push(song).unwrap();
41 if self.conn.queue().unwrap().is_empty() {
42 self.conn.push(song).unwrap();
43 self.conn.play().unwrap();
44 } else {
45 self.conn.push(song).unwrap();
46 self.conn.next().unwrap();
47 }
48 }
49
50 fn get_song_with_only_filename(&self, filename: &str) -> Song {
51 Song {
52 file: filename.to_string(),
53 artist: None,
54 title: None,
55 duration: None,
56 last_mod: None,
57 name: None,
58 place: None,
59 range: None,
60 tags: vec![("".to_string(), "".to_string())],
61 }
62 }
63
64 pub fn status(&mut self) {
65 let current_song = self.conn.currentsong();
66 let status = self.conn.status().unwrap();
67
68 if current_song.is_ok() {
69 let song = current_song.unwrap();
70 if let Some(s) = song {
71 println!("{} - {}", s.artist.unwrap(), s.title.unwrap());
72 }
73 }
74 println!(
75 "volume: {}\trepeat: {}\trandom: {}\tsingle: {}\tconsume: {}",
76 status.volume, status.repeat, status.random, status.single, status.consume
77 );
78 }
79
80 // Play controls
81 pub fn pause(&mut self) {
82 self.conn.pause(true).unwrap();
83 }
84
85 pub fn toggle_pause(&mut self) {
86 self.conn.toggle_pause().unwrap();
87 }
88
89 // Volume controls
90 pub fn inc_volume(&mut self, inc: i8) {
91 let cur = self.conn.status().unwrap().volume;
92 self.conn.volume(cur + inc).unwrap();
93 }
94
95 pub fn dec_volume(&mut self, dec: i8) {
96 let cur = self.conn.status().unwrap().volume;
97 self.conn.volume(cur - dec).unwrap();
98 }
99}
100
101fn get_file_tree_into_vec(conn: &mut Client, vec: &mut Vec<String>, path: &str, dir_append: &str) {
102 let songs = conn.listfiles(path).unwrap_or_default();
103 for (i, s) in songs {
104 // Output of listfiles contains the last-modified thing, we dont want that
105 if i != "Last-Modified" {
106 if i == "directory" {
107 get_file_tree_into_vec(conn, vec, &s, &s);
108 } else {
109 // We parse the string as float because the output of listfiles contains some random numbers, we dont want that
110 if !s.parse::<f32>().is_ok() {
111 let mut sam: String = String::new();
112 sam.push_str(dir_append);
113 sam.push_str(r"/");
114 sam.push_str(s.as_str());
115 vec.push(sam);
116 }
117 }
118 }
119 }
120}
121
122fn get_choice_index(ss: &Vec<String>, selection: &str) -> usize {
123 let mut choice: usize = 0;
124 if let Some(index) = ss.iter().position(|s| s == selection) {
125 choice = index;
126 }
127
128 choice
129}