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
|
use std::{process::Command, process::exit};
mod cli;
mod config;
mod learner;
mod state;
mod trajectory;
use crate::cli::{Action, parse};
use crate::config::Config;
use crate::state::State;
fn mpc_load_and_play(path: &str) {
_ = Command::new("mpc").arg("clear").output();
_ = Command::new("mpc").args(["add", path]).output();
_ = Command::new("mpc").arg("play").output();
}
fn notify(text: &str) {
if let Err(e) = Command::new("notify-send")
.args(["-t", "5000", "-a", "mood", text])
.status()
{
eprintln!("Failed to show notification: {e}");
}
}
fn load_state_no_default() -> State {
State::try_load().unwrap_or_else(|e| {
eprintln!("Failed to load state: {e}, nothing to dump.");
exit(1);
})
}
fn main() {
let action = parse();
if action.is_none() {
return;
};
let action = action.unwrap();
match action {
Action::DumpAlbums => {
let state = load_state_no_default();
println!(
"{}",
state.dump_albums().unwrap_or_else(|e| {
eprintln!("Failed to serialize state (dump-albums): {e}");
exit(1);
})
);
}
Action::DumpLearner => {
let state = load_state_no_default();
println!(
"{}",
state.dump_learner().unwrap_or_else(|e| {
eprintln!("Failed to serialize state (dump-learner): {e}");
exit(1);
})
);
}
Action::DumpTrajectory => {
let state = load_state_no_default();
println!(
"{}",
state.dump_trajectory().unwrap_or_else(|e| {
eprintln!("Failed to serialize state (dump-trajectory): {e}");
exit(1);
})
);
}
Action::NewState => {
let default = State::default();
if let Err(e) = default.try_save() {
eprintln!("Failed to write new state: {e}");
exit(1);
}
println!("New state written.");
}
Action::NewConfig => {
let default = Config::default();
if let Err(e) = default.try_save() {
eprintln!("Failed to write new config: {e}");
exit(1);
}
println!("New config written.");
}
Action::Run => {
let config = match Config::try_load() {
Ok(c) => c,
Err(e) => {
eprintln!("Failed to load config: {e}, writing defaults.");
let default = Config::default();
if let Err(se) = default.try_save() {
panic!("Failed to save default config: {se}");
}
default
}
};
let mut state = match State::try_load() {
Ok(s) => s,
Err(e) => {
eprintln!("Failed to load state: {e}");
State::default()
}
};
let ur = state.update_albums(&config);
println!("Update result: {ur}");
if let Some((next_album, prob)) = state.next(&config) {
let text = format!("Now playing: {next_album} ({:.2}%)", prob * 100.0);
println!("{text}");
mpc_load_and_play(&next_album);
notify(&text);
} else {
println!("No next album available.");
}
if let Err(e) = state.try_save() {
panic!("Failed to save state: {e}");
}
}
}
}
|