From 742aaa2a08a9e8837edf47bdc107d9d535dbf45a Mon Sep 17 00:00:00 2001 From: Amir Saeid Date: Sun, 15 Mar 2020 20:22:22 +0000 Subject: Add command line arguments --- Cargo.lock | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + README.md | 12 +++++++++ src/bin/main.rs | 42 +++++++++++++++++++++++-------- 4 files changed, 122 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 15cd5c0..4c438e8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,11 +1,37 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +[[package]] +name = "ansi_term" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +dependencies = [ + "winapi", +] + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d" +[[package]] +name = "bitflags" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" + [[package]] name = "chrono" version = "0.4.11" @@ -17,6 +43,30 @@ dependencies = [ "time", ] +[[package]] +name = "clap" +version = "2.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9" +dependencies = [ + "ansi_term", + "atty", + "bitflags", + "strsim", + "textwrap", + "unicode-width", + "vec_map", +] + +[[package]] +name = "hermit-abi" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1010591b26bbfe835e9faeabeb11866061cc7dcebffd56ad7d0942d0e61aefd8" +dependencies = [ + "libc", +] + [[package]] name = "libc" version = "0.2.67" @@ -59,6 +109,7 @@ name = "sctd" version = "0.1.0" dependencies = [ "chrono", + "clap", "spa", "x11", ] @@ -72,6 +123,21 @@ dependencies = [ "chrono", ] +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" + +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] + [[package]] name = "time" version = "0.1.42" @@ -83,6 +149,18 @@ dependencies = [ "winapi", ] +[[package]] +name = "unicode-width" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479" + +[[package]] +name = "vec_map" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" + [[package]] name = "winapi" version = "0.3.8" diff --git a/Cargo.toml b/Cargo.toml index 055639b..8e8fca4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ edition = "2018" [dependencies] chrono = "0.4.11" spa = "0.2.0" +clap = "2.33.0" [dependencies.x11] version = "2.18.2" diff --git a/README.md b/README.md index 3329a51..f6de249 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,15 @@ # sct - set color temperature daemon Based on [sct](https://flak.tedunangst.com/post/sct-set-color-temperature) by Ted Unangst. Can be run as a daemon and calculates sunrise and sunset based on geo-pos lat/lon and sets the temperature accordingly. + +## Usage + +### Set latitude and longitude +```bash +$ cargo run -- --latitude 53.3498 --longitude 6.2603 +``` + +### Reset +```bash +$ cargo run -- --reset +``` diff --git a/src/bin/main.rs b/src/bin/main.rs index 7e69ae1..832320a 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -1,8 +1,10 @@ extern crate chrono; +extern crate clap; extern crate spa; use chrono::prelude::*; -use spa::{SunriseAndSet, calc_sunrise_and_set}; +use clap::{value_t_or_exit, App, Arg}; +use spa::{calc_sunrise_and_set, SunriseAndSet}; use std::os::raw::{c_ushort, c_void}; use std::ptr; use std::thread; @@ -56,23 +58,41 @@ fn get_temp(utc: DateTime, ss: &SunriseAndSet) -> u32 { } else { return low_temp; } - }, + } SunriseAndSet::PolarDay => return high_temp, SunriseAndSet::PolarNight => return low_temp, } } fn main() { - // Dublin - let lat: f64 = 53.3498; - let lon: f64 = 6.2603; + let matches = App::new("sctd") + .about("set color temperature daemon") + .arg( + Arg::with_name("latitude") + .long("latitude") + .takes_value(true), + ) + .arg( + Arg::with_name("longitude") + .long("longitude") + .takes_value(true), + ) + .arg(Arg::with_name("reset").long("reset")) + .get_matches(); + + if matches.is_present("reset") { + set_temp(5500); + } else { + let latitude = value_t_or_exit!(matches, "latitude", f64); + let longitude = value_t_or_exit!(matches, "longitude", f64); - loop { - let utc: DateTime = Utc::now(); - match calc_sunrise_and_set(utc, lat, lon) { - Ok(ss) => set_temp(get_temp(utc, &ss)), - Err(e) => println!("Error calculating sunrise and sunset: {:?}", e), + loop { + let utc: DateTime = Utc::now(); + match calc_sunrise_and_set(utc, latitude, longitude) { + Ok(ss) => set_temp(get_temp(utc, &ss)), + Err(e) => println!("Error calculating sunrise and sunset: {:?}", e), + } + thread::sleep(Duration::from_secs(300)); } - thread::sleep(Duration::from_secs(300)); } } -- cgit v1.2.3