Add the base main code and create the events enum

This commit is contained in:
Anas Elgarhy 2023-02-12 05:33:58 +02:00
parent 532b9d5490
commit 355ef0db93
No known key found for this signature in database
GPG Key ID: 0501802A1D496528
5 changed files with 71 additions and 8 deletions

7
.idea/discord.xml Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DiscordProjectSettings">
<option name="show" value="ASK" />
<option name="description" value="" />
</component>
</project>

View File

@ -3,8 +3,4 @@
<component name="ComposerSettings">
<execution />
</component>
<component name="DiscordProjectSettings">
<option name="show" value="ASK" />
<option name="description" value="" />
</component>
</project>

12
src/cmus/events.rs Normal file
View File

@ -0,0 +1,12 @@
use crate::cmus::player_settings::AAAMode;
use crate::cmus::TrackStatus;
pub enum CmusEvent {
StatusChanged(TrackStatus),
TrackChanged,
VolumeChanged { left: u8, right: u8 },
PositionChanged(u32),
ShuffleChanged(bool),
RepeatChanged(bool),
AAAMode(AAAMode),
}

View File

@ -1,8 +1,13 @@
mod events;
mod query;
mod player_settings;
use std::collections::HashMap;
use std::fmt::Display;
use std::num::ParseIntError;
use std::str::FromStr;
use typed_builder::TypedBuilder;
use crate::cmus::query::CmusQueryResponse;
#[derive(Debug, PartialEq, Default)]
pub struct TrackMetadata {
@ -26,7 +31,7 @@ pub struct Track {
pub position: u32,
}
#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub enum CmusError {
CmusRunningError(String),
UnknownStatus,
@ -35,6 +40,8 @@ pub enum CmusError {
DurationError(String),
PositionError(String),
UnknownError(String),
UnknownAAAMode(String),
UnknownShuffleMode(String),
}
impl Display for CmusError {
@ -47,6 +54,8 @@ impl Display for CmusError {
CmusError::DurationError(s) => write!(f, "Duration error: {}", s),
CmusError::PositionError(s) => write!(f, "Position error: {}", s),
CmusError::UnknownError(s) => write!(f, "Unknown error: {}", s),
CmusError::UnknownAAAMode(s) => write!(f, "Unknown AAA mode: {}", s),
CmusError::UnknownShuffleMode(s) => write!(f, "Unknown shuffle mode: {}", s),
}
}
}
@ -164,10 +173,10 @@ impl Track {
}
/// Make a status request to cmus.
/// And collect the output, and parse it into a `Track`.
/// And collect the output, and parse it into a `CmusQueryResponse`.
/// If the cmus is not running, or the socket is not available, this function will return an error.
#[inline]
pub fn get_track(query_command: &mut std::process::Command) -> Result<Track, CmusError> {
pub fn ping_cmus(query_command: &mut std::process::Command) -> Result<CmusQueryResponse, CmusError> {
// Just run the command, and collect the output.
let output = query_command
.output()
@ -182,7 +191,7 @@ pub fn get_track(query_command: &mut std::process::Command) -> Result<Track, Cmu
let output =
String::from_utf8(output.stdout).map_err(|e| CmusError::UnknownError(e.to_string()))?;
Track::from_str(&output).map_err(|e| CmusError::UnknownError(e.to_string()))
CmusQueryResponse::from_str(&output).map_err(|e| CmusError::UnknownError(e.to_string()))
}
/// Build the query command.

View File

@ -18,4 +18,43 @@ fn main() {
&args.cmus_socket_address,
&args.cmus_socket_password,
);
let sleep = || {
std::thread::sleep(std::time::Duration::from_millis(args.interval));
};
// Initialize the buffer to store the track info, to compare it with the next one.
let mut previous_track = cmus::Track::default();
// Initialize the buffer to store the cover path, to compare it with the next one.
// This is used to speed up the main loop, because we don't need to process the template and search for the cover every time.
// We only need to do it when the track directory changes.
let mut previous_cover_path: Option<String> = None;
loop {
// Get the track info, and compare it with the previous one.
let Ok(track) = cmus::ping_cmus(&mut query_command) else {
if args.link {
std::process::exit(0)
} else {
// If the track info is the same as the previous one, just sleep for a while and try again.
sleep();
continue;
}
};
/* // Compare the track info with the previous one, and if they are the same, just sleep for a while and try again.
if track == previous_track {
sleep();
continue;
}
// If the track info is different from the previous one, get the changes events.
let changes = track.get_changes(&previous_track);
*/
sleep();
}
}