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
|
use mpd::song::Song;
use mpd::{Client, State};
use simple_dmenu::dmenu;
use std::process::Command;
pub type Result<T> = core::result::Result<T, Error>;
pub type Error = Box<dyn std::error::Error>;
#[derive(Debug)]
pub struct Connection {
pub conn: Client,
pub songs_filenames: Vec<String>,
// pub state: String,
}
impl Connection {
pub fn new(addrs: &str) -> Result<Self> {
let mut conn = Client::connect(addrs).unwrap();
let songs_filenames: Vec<String> = conn
.listall()
.unwrap()
.into_iter()
.map(|x| x.file)
.collect();
Ok(Self {
conn,
songs_filenames,
// state: "Stopped".to_string(),
})
}
pub fn play_fzf(&mut self) -> Result<()> {
is_installed("fzf").map_err(|ex| ex)?;
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.get(0).unwrap());
let song = self.get_song_with_only_filename(ss.get(index).unwrap());
self.push(&song)?;
Ok(())
}
pub fn play_dmenu(&mut self) -> Result<()> {
is_installed("dmenu").map_err(|ex| ex)?;
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)?;
Ok(())
}
// pub fn update_state(&mut self) {
// match self.conn.status().unwrap().state {
// State::Stop => self.state = "Stopped".to_string(),
// State::Play => self.state = "Playing".to_string(),
// State::Pause => self.state = "Paused".to_string(),
// }
// }
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)?;
if self.conn.status()?.state == State::Stop {
self.conn.play()?;
}
self.conn.next().unwrap();
}
Ok(())
}
pub fn push_playlist(&mut self, playlist: &str) -> Result<()> {
let songs: Vec<Song> = self.conn.playlist(playlist)?;
for song in songs {
if self.songs_filenames.contains(&song.file) {
let song = self.get_song_with_only_filename(&song.file);
self.conn.push(&song)?;
self.conn.play()?;
}
}
Ok(())
}
pub fn get_song_with_only_filename(&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 get_current_song(&mut self) -> Option<String> {
self.conn.currentsong().unwrap().unwrap_or_default().title
}
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
);
}
pub fn now_playing(&mut self) -> Option<String> {
let song = self.conn.currentsong().unwrap().unwrap_or_default();
if let Some(s) = song.title {
if let Some(a) = song.artist {
Some(format!("{} - {}", s, a))
} else {
Some(s)
}
} else {
None
}
}
// Playback 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 set_volume(&mut self, u: String) {
let cur = self.conn.status().unwrap().volume;
let sym = u.get(0..1).unwrap();
let u: i8 = u.parse::<i8>().unwrap();
if sym == "+" || sym == "-" {
self.conn.volume(cur + u).unwrap();
} else {
self.conn.volume(u).unwrap();
}
}
}
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
}
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())
}
}
|