Skip to content

Commit

Permalink
Add MIN_ITER_DUR env var, add configuration to README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinegb committed Aug 20, 2024
1 parent 29cbb3c commit 522537f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,29 @@ Just run the program (if installed through Cargo, enter
`apple-music-rich-presence` into your terminal) and you're good to go! I would
recommend adding it to your Login Items in System Settings so that you don't
have to start it manually every time you restart your computer.

### Configuration

There are a few environment variables that can be used to configure Apple Music
Rich Presence.

#### `MIN_ITER_DUR`

Apple Music Rich Presence, by default, will ensure that each iteration (where it
checks whether Discord and Apple Music are open, whether the currently playing
song has changed, etc.) spans at least 1 second. If you feel that it need more
time to prevent glitchy behaviour like Apple Music refusing to quit, you can
raise this to, say, `MIN_ITER_DUR=3`.

#### `DAEMON`

By default, Apple Music Rich Presence will launch a daemon process to run in the
background. You can disable this behaviour, however, by setting `DAEMON` to `0`.
Then, the program will stay present and you'll be able to see its logging in
your terminal.

#### `RUST_LOG`

This variable configures the level of logging to perform. See
[`env_logger`'s documentation](https://docs.rs/env_logger/latest/env_logger/#enabling-logging)
for info.
11 changes: 8 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ use log::{debug, error, info, trace, warn};
use sysinfo::{ProcessRefreshKind, ProcessesToUpdate};

const CLIENT_ID: &str = "1273805325954187386";
const LOOP_DURATION: Duration = Duration::from_secs(1);
const EXPECT_CLIENT_INITIALIZED: &str = "Discord client has not been initialized";

fn inner_loop(
Expand Down Expand Up @@ -200,6 +199,12 @@ fn try_main() -> Result<(), Box<dyn Error>> {
debug!("Daemonized");
}

let min_iter_dur = Duration::from_secs(
env::var("MIN_ITER_DUR")
.unwrap_or("1".to_string())
.parse()
.map_err(|err| format!("Failed to parse `MIN_ITER_DUR`: {err}"))?,
);
let mut sys = sysinfo::System::new();
let mut discord_client_option = None;
let mut last_track = None;
Expand All @@ -219,8 +224,8 @@ fn try_main() -> Result<(), Box<dyn Error>> {

let loop_start_elapsed = loop_start.elapsed();

if loop_start_elapsed < LOOP_DURATION {
sleep(LOOP_DURATION - loop_start_elapsed);
if loop_start_elapsed < min_iter_dur {
sleep(min_iter_dur - loop_start_elapsed);
}
}
}
Expand Down

0 comments on commit 522537f

Please sign in to comment.