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
mod mpv;
use anyhow::{anyhow, Result};
use std::sync::{
mpsc::{Receiver, Sender},
Arc,
};
use time::{format_description, UtcOffset};
use clap::Parser;
use cursive::{
event::{Event, Key},
view::SizeConstraint,
views::{
Dialog, DummyView, LinearLayout, ResizedView, ScrollView, SelectView, TextContent, TextView,
},
Cursive, View,
};
use log::error;
use mpv::{force_play, get_file_name, get_playlist, DEFAULT_VOL};
use mpv::VanControl;
use mpv::PlayStatus;
#[derive(Parser, Debug)]
#[clap(about, version, author)]
struct Args {
#[clap()]
args: Vec<String>,
}
struct CurrentStatus {
vol: Option<Arc<TextContent>>,
current_song_status: Arc<TextContent>,
current_artist_status: Arc<TextContent>,
current_time_status: Arc<TextContent>,
}
pub fn init_siv(siv: &mut Cursive) -> Result<()> {
let (control_tx, control_rx) = std::sync::mpsc::channel();
let (view, current_status) = get_view();
let vol_status = current_status.vol.unwrap();
start_mpv(
CurrentStatus {
vol: None,
..current_status
},
control_rx,
);
let args = Args::parse().args;
for i in args {
mpv::add(&i)?;
}
set_cursive(vol_status, control_tx, siv);
siv.add_layer(view);
Ok(())
}
fn set_cursive(vol_status: Arc<TextContent>, control_tx: Sender<VanControl>, siv: &mut Cursive) {
let volume_status_clone = vol_status.clone();
let control_tx_clone = control_tx.clone();
let control_tx_clone_2 = control_tx.clone();
let control_tx_clone_3 = control_tx.clone();
let control_tx_clone_4 = control_tx.clone();
siv.add_global_callback('=', move |_| {
if let Err(e) = add_volume(control_tx.clone(), volume_status_clone.clone()) {
error!("{}", e);
}
});
siv.add_global_callback('-', move |_| {
if let Err(e) = reduce_volume(control_tx_clone_2.clone(), vol_status.clone()) {
error!("{}", e);
}
});
siv.add_global_callback(Event::Key(Key::Right), move |_| {
control_tx_clone_3.send(VanControl::NextSong).unwrap();
});
siv.add_global_callback(Event::Key(Key::Left), move |_| {
control_tx_clone.send(VanControl::PrevSong).unwrap();
});
siv.add_global_callback('p', move |_| {
control_tx_clone_4.send(VanControl::PauseControl).unwrap();
});
siv.add_global_callback('l', move |s| {
playlist_view(s);
});
siv.add_global_callback('~', cursive::Cursive::toggle_debug_console);
siv.set_autorefresh(true);
}
fn get_view() -> (Dialog, CurrentStatus) {
let mut vol_view = TextView::new(format!("vol: {}", DEFAULT_VOL));
let vol_status = Arc::new(vol_view.get_shared_content());
let mut current_song_view = TextView::new("Unknown");
let current_song_status = Arc::new(current_song_view.get_shared_content());
let mut current_time_view = TextView::new("-/-");
let current_time_status = Arc::new(current_time_view.get_shared_content());
let mut current_artist_view = TextView::new("Unknown");
let current_artist_status = Arc::new(current_artist_view.get_shared_content());
let view = wrap_in_dialog(
LinearLayout::vertical()
.child(current_song_view.center())
.child(DummyView {})
.child(current_artist_view.center())
.child(DummyView {})
.child(current_time_view.center())
.child(DummyView {})
.child(vol_view.center()),
"Van",
None,
);
(
view,
CurrentStatus {
vol: Some(vol_status),
current_song_status,
current_artist_status,
current_time_status,
},
)
}
fn playlist_view(siv: &mut Cursive) {
let playlist = get_playlist();
let mut files = vec![];
if let Ok(playlist) = playlist {
for i in playlist {
files.push(i.filename);
}
} else {
error!("{:?}", playlist.unwrap_err());
}
let view = wrap_in_dialog(
SelectView::new()
.with_all_str(files.clone())
.on_submit(move |s, c: &String| {
let index = files.clone().iter().position(|x| x == c);
if let Some(index) = index {
force_play(index.try_into().unwrap()).ok();
}
s.pop_layer();
}),
"Playlist",
None,
)
.button("Back", |s| {
s.cb_sink()
.send(Box::new(|s| {
s.pop_layer();
}))
.unwrap();
});
siv.add_layer(view);
}
fn start_mpv(current_status: CurrentStatus, control_rx: Receiver<VanControl>) {
std::thread::spawn(move || {
let (getinfo_tx, getinfo_rx) = std::sync::mpsc::channel();
let current_song_status_clone = current_status.current_song_status.clone();
std::thread::spawn(move || {
let buf = std::ffi::CString::new("C").expect("Unknown Error!");
unsafe { libc::setlocale(libc::LC_NUMERIC, buf.as_ptr()) };
if let Err(e) = mpv::play(control_rx, getinfo_tx) {
eprintln!("{}", e);
std::process::exit(1);
}
});
loop {
let mut time_str = String::from("-/-");
let r = getinfo_rx.try_recv();
if let Ok(status) = r {
match status {
PlayStatus::MediaInfo(m) => {
current_song_status_clone.set_content(m.title);
current_status.current_artist_status.set_content(m.artist);
if let Ok(current_time) = get_time(m.current_time) {
time_str = time_str.replace("-/", &format!("{}/", current_time));
}
if let Ok(duration) = get_time(m.duration) {
time_str = time_str.replace("/-", &format!("/{}", duration));
}
current_status.current_time_status.set_content(time_str);
}
PlayStatus::Loading => {
if let Ok(name) = get_file_name() {
current_status.current_song_status.clone().set_content(name);
current_status
.current_artist_status
.clone()
.set_content("Unknown");
current_status
.current_time_status
.clone()
.set_content("-/-");
}
}
}
}
}
});
}
fn add_volume(control_tx: Sender<VanControl>, vol_status: Arc<TextContent>) -> Result<()> {
let mut current_vol = mpv::get_volume()?;
if current_vol < 100.0 {
current_vol += 5.0;
control_tx.send(VanControl::SetVolume(current_vol))?;
vol_status.set_content(format!("vol: {}", current_vol));
}
Ok(())
}
fn reduce_volume(control_tx: Sender<VanControl>, vol_status: Arc<TextContent>) -> Result<()> {
let mut current_vol = mpv::get_volume()?;
if current_vol > 0.0 {
current_vol -= 5.0;
control_tx.send(VanControl::SetVolume(current_vol))?;
vol_status.set_content(format!("vol: {}", current_vol));
}
Ok(())
}
fn wrap_in_dialog<V: View, S: Into<String>>(inner: V, title: S, width: Option<usize>) -> Dialog {
Dialog::around(ResizedView::new(
SizeConstraint::AtMost(width.unwrap_or(64)),
SizeConstraint::Free,
ScrollView::new(inner),
))
.padding_lrtb(2, 2, 1, 1)
.title(title)
}
fn get_time(time: i64) -> Result<String> {
let f = format_description::parse("[offset_minute]:[offset_second]")?;
let offset = UtcOffset::from_whole_seconds(time.try_into()?)?;
let minute = offset.whole_minutes();
let date = offset.format(&f)?;
let sess = date
.split_once(':')
.map(|x| x.1)
.ok_or_else(|| anyhow!("Can not convert time!"))?;
let date = format!("{}:{}", minute, sess);
Ok(date)
}