aboutsummaryrefslogtreecommitdiff
path: root/src/connection.rs
blob: 972ee101dd3732527a4246de98056bf21c723d4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
use mpd::song::Song;
use mpd::{Client, State};
use simple_dmenu::dmenu;
use std::process::Command;
use std::time::Duration;

pub type Result<T> = core::result::Result<T, Error>;
pub type Error = Box<dyn std::error::Error>;

#[derive(Debug)]
/// struct storing the mpd Client related stuff
pub struct Connection {
    pub conn: Client,
    pub songs_filenames: Vec<String>,
    pub state: String,
    pub elapsed: Duration,
    pub total_duration: Duration,
    pub volume: u8,
    pub repeat: bool,
    pub random: bool,
    pub current_song: Song,
}

impl Connection {
    /// Create a new connection
    pub fn new(addrs: &str) -> Result<Self> {
        let mut conn = Client::connect(addrs).unwrap();

        let empty_song = Song {
            file: "No Song playing or in Queue".to_string(),
            ..Default::default()
        };

        let songs_filenames: Vec<String> = conn
            .listall()
            .unwrap()
            .into_iter()
            .map(|x| x.file)
            .collect();

        let status = conn.status().unwrap();
        let (elapsed, total) = status.time.unwrap_or_default();
        let volume: u8 = status.volume as u8;
        let repeat = status.repeat;
        let random = status.random;

        let current_song = conn
            .currentsong()
            .unwrap_or_else(|_| Some(empty_song.clone()))
            .unwrap_or(empty_song);
        Ok(Self {
            conn,
            songs_filenames,
            state: "Stopped".to_string(),
            elapsed,
            total_duration: total,
            volume,
            repeat,
            random,
            current_song,
        })
    }

    /// Fzf prompt for selecting song
    pub fn play_fzf(&mut self) -> Result<()> {
        is_installed("fzf")?;
        let ss = &self.songs_filenames;
        let fzf_choice = rust_fzf::select(ss.clone(), Vec::new()).unwrap();
        let index = get_choice_index(&self.songs_filenames, fzf_choice.first().unwrap());
        let song = self.get_song_with_only_filename(ss.get(index).unwrap());
        self.push(&song)?;

        Ok(())
    }

    /// Dmenu prompt for selecting songs
    pub fn play_dmenu(&mut self) -> Result<()> {
        is_installed("dmenu")?;
        let ss: Vec<&str> = self.songs_filenames.iter().map(|x| x.as_str()).collect();
        let op = dmenu!(iter &ss; args "-p", "Choose a song: ", "-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)?;
        Ok(())
    }

    /// Update status
    pub fn update_status(&mut self) {
        let status = self.conn.status().unwrap();
        let empty_song = self.get_song_with_only_filename("No Song playing or in Queue");
        let current_song = self
            .conn
            .currentsong()
            .unwrap_or_else(|_| Some(empty_song.clone()))
            .unwrap_or(empty_song);

        // Playback State
        match status.state {
            State::Stop => self.state = "Stopped".to_string(),
            State::Play => self.state = "Playing".to_string(),
            State::Pause => self.state = "Paused".to_string(),
        }

        // Progress
        let (elapsed, total) = status.time.unwrap_or_default();
        self.elapsed = elapsed;
        self.total_duration = total;

        // Volume
        self.volume = status.volume as u8;

        // Repeat mode
        self.repeat = status.repeat;

        // Random mode
        self.random = status.random;

        self.current_song = current_song;
    }

    /// Get progress ratio of current playing song
    pub fn get_progress_ratio(&self) -> f64 {
        let total = self.total_duration.as_secs_f64();
        if total == 0.0 {
            0.0
        } else {
            let ratio = self.elapsed.as_secs_f64() / self.total_duration.as_secs_f64();
            if ratio > 1.0 || ratio == 0.0 {
                0.0
            } else {
                ratio
            }
        }
    }

    /// push the given song to queue
    pub fn push(&mut self, song: &Song) -> Result<()> {
        if self.conn.queue().unwrap().is_empty() {
            self.conn.push(song).unwrap();
            self.conn.play().unwrap();
        } else {
            self.conn.push(song)?;
            let len: u32 = (self.conn.queue().unwrap().len() - 1).try_into().unwrap();
            self.conn.switch(len)?;
            if self.conn.status()?.state == State::Stop {
                self.conn.play()?;
            }
        }

        Ok(())
    }

    /// Push all songs of a playlist into queue
    pub fn load_playlist(&mut self, playlist: &str) -> Result<()> {
        self.conn.load(playlist, ..)?;
        self.conn.play()?;
        Ok(())
    }

    /// Add given song to playlist
    pub fn add_to_playlist(&mut self, playlist: &str, song: &Song) -> Result<()> {
        self.conn.pl_push(playlist, song)?;
        Ok(())
    }

    /// Given a filename, get instance of Song with only filename
    pub fn get_song_with_only_filename(&self, filename: &str) -> Song {
        Song {
            file: filename.to_string(),
            ..Default::default()
        }
    }

    /// Given a song name from a directory, it returns the full path of the song in the database
    pub fn get_full_path(&self, short_path: &str) -> Option<String> {
        for (i, f) in self.songs_filenames.iter().enumerate() {
            if f.contains(short_path) {
                return Some(self.songs_filenames.get(i).unwrap().to_string());
            }
        }

        None

        // Ok(filename.to_string())
    }

    /// Print status to stdout
    pub fn status(&mut self) {
        let current_song = self.conn.currentsong();
        let status = self.conn.status().unwrap();

        if current_song.is_ok() && status.state != State::Stop {
            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
        );
    }

    /// Gives title of current playing song
    pub fn now_playing(&mut self) -> Result<Option<String>> {
        if let Some(s) = &self.current_song.title {
            if let Some(a) = &self.current_song.artist {
                Ok(Some(format!("\"{}\" By {}", s, a)))
            } else {
                Ok(Some(s.to_string()))
            }
        } else {
            Ok(Some(self.current_song.file.clone()))
        }
    }

    // Playback controls
    /// Pause playback
    pub fn pause(&mut self) {
        self.conn.pause(true).unwrap();
    }

    /// Toggles playback
    pub fn toggle_pause(&mut self) {
        self.conn.toggle_pause().unwrap();
    }

    /// Toggle Repeat mode
    pub fn toggle_repeat(&mut self) {
        if self.conn.status().unwrap().repeat {
            self.conn.repeat(false).unwrap();
        } else {
            self.conn.repeat(true).unwrap();
        }
    }

    /// Toggle random mode
    pub fn toggle_random(&mut self) {
        if self.conn.status().unwrap().random {
            self.conn.random(false).unwrap();
        } else {
            self.conn.random(true).unwrap();
        }
    }

    // Volume controls
    /// Increase Volume
    pub fn inc_volume(&mut self, v: i8) {
        let cur = self.conn.status().unwrap().volume;
        if cur + v <= 100 {
            self.conn.volume(cur + v).unwrap();
        }
    }

    /// Decrease volume
    pub fn dec_volume(&mut self, v: i8) {
        let cur = self.conn.status().unwrap().volume;
        if cur - v >= 0 {
            self.conn.volume(cur - v).unwrap();
        }
    }
}

/// Gets the index of the string from the Vector
fn get_choice_index(ss: &[String], selection: &str) -> usize {
    let mut choice: usize = 0;
    if let Some(index) = ss.iter().position(|s| s == selection) {
        choice = index;
    }

    choice
}

/// Checks if given program is installed in your system
fn is_installed(ss: &str) -> Result<()> {
    let output = Command::new("which")
        .arg(ss)
        .output()
        .expect("Failed to execute command");
    if output.status.success() {
        Ok(())
    } else {
        let err = format!("{} not installed", ss);
        Err(err.into())
    }
}