aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authortslil <tslil@posteo.de>2026-07-13 15:06:41 +0100
committertslil <tslil@posteo.de>2026-07-13 21:21:21 +0100
commit3f289981bd9c8e800a6cd310d4b1f2f36634c9f1 (patch)
treeba711eb220e39fe64388d370b16384a7bc84eaab /src/main.rs
parente0241df8afde0483fc36cb2300b53127672486dc (diff)
Pure history
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs146
1 files changed, 110 insertions, 36 deletions
diff --git a/src/main.rs b/src/main.rs
index 74f90c8..86c6341 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,61 +1,135 @@
-use std::process::Command;
+use std::{process::Command, process::exit};
+mod cli;
mod config;
mod learner;
mod state;
mod trajectory;
-use crate::{config::default_state_path, state::State};
+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()
- .expect("failed to execute mpc clear");
-
- _ = Command::new("mpc")
- .args(["add", path])
- .output()
- .expect("failed to execute mpc add");
-
- _ = Command::new("mpc")
- .arg("play")
- .output()
- .expect("failed to execute mpc play");
+ _ = 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])
+ .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 state = State::load(&default_state_path());
- let mut state = match state {
- Err(e) => {
- eprintln!("Failed to load state: {e}");
- State::default()
- }
- Ok(s) => s,
+ let action = parse();
+ if action.is_none() {
+ return;
};
- let ur = state.update_albums();
- println!("Update result: {ur}");
+ let action = action.unwrap();
- if let Some((next_album, prob)) = state.next() {
- 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.");
- }
+ 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.");
+ }
- if let Err(e) = state.try_save() {
- eprintln!("Failed to save state: {e}");
+ 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}");
+ }
+ }
}
}