Skip to content

Commit

Permalink
0.3.1: Add testing & dry run
Browse files Browse the repository at this point in the history
  • Loading branch information
Randomblock1 committed Feb 7, 2023
1 parent 2138ed6 commit 5c4847b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "caffeinate2"
version = "0.3.0"
version = "0.3.1"
edition = "2021"
authors = ["Randomblock1 <randomblock1@pm.me>"]
description = "Caffeinate MacOS utility with more options. Temporarily prevent your system from sleeping."
Expand Down
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@

`caffeinate` but it's written in Rust and has more options. Keeps your Mac wide awake.

## UNDER CONSTRUCTION

There are probably some bugs hiding somewhere, but regardless, it works.

## Installation

### GitHub Releases
Expand Down
53 changes: 42 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ use std::process;
use std::thread;

fn set_assertions(iokit: &power_management::IOKit, args: &Args, state: bool) -> Vec<u32> {
if args.dry_run {
// Don't actually sleep
return Vec::new();
}

if args.entirely {
// Prevents the system from sleeping entirely.
iokit.set_sleep_disabled(true).unwrap_or_else(|_| {
Expand Down Expand Up @@ -92,6 +97,11 @@ struct Args {
#[arg(short, long)]
user_active: bool,

/// Dry run. Don't actually sleep.
/// Useful for testing.
#[arg(long)]
dry_run: bool,

/// Wait for X seconds.
/// Also supports time units (e.g. 1s, 1m, 1h, 1d).
#[arg(short, long, name = "DURATION")]
Expand Down Expand Up @@ -141,12 +151,12 @@ fn parse_duration(duration: String) -> u64 {

fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut args = Args::parse();
if !args.display
&& !args.disk
&& !args.system
&& !args.system_on_ac
&& !args.entirely
&& !args.user_active
if !(args.display
|| args.disk
|| args.system
|| args.system_on_ac
|| args.entirely
|| args.user_active)
{
// Default to system sleep if no other options are specified
args.system = true;
Expand All @@ -156,9 +166,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
panic!("This program only works on macOS.");
}

let iokit = power_management::IOKit::new();

let assertions = set_assertions(&iokit, &args, true);
if args.verbose {
println!("DEBUG {:#?}", &args);
}
Expand All @@ -185,8 +192,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}
print!("] ");

let mut signals = Signals::new([SIGINT])?;
let iokit = power_management::IOKit::new();
let assertions = set_assertions(&iokit, &args, true);

let mut signals = Signals::new([SIGINT])?;
let assertions_clone = assertions.clone();
thread::spawn(move || {
for _ in signals.forever() {
Expand Down Expand Up @@ -309,10 +318,32 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
process::exit(0);
}
} else {
// If no arguments are provided, disable sleep until Ctrl+C is pressed
// If no timer arguments are provided, disable sleep until Ctrl+C is pressed
set_assertions(&iokit, &args, true);
println!("until Ctrl+C pressed.");
thread::park();
}
Ok(())
}

#[cfg(test)]
mod tests {
#[test]
fn test_parse_duration() {
let duration = "1d2h3m4s".to_string();
let result = super::parse_duration(duration);
assert_eq!(result, 93784);

let duration = "1day 2hrs3m".to_string();
let result = super::parse_duration(duration);
assert_eq!(result, 93780);

let duration = "3 minutes 17 hours 2 seconds".to_string();
let result = super::parse_duration(duration);
assert_eq!(result, 61382);

let duration = "45323".to_string();
let result = super::parse_duration(duration);
assert_eq!(result, 45323);
}
}

0 comments on commit 5c4847b

Please sign in to comment.