commit 0b1f5429ba90952b66bd81a24c67b07b8add9d47 Author: Moridius Date: Wed Sep 2 21:09:39 2020 +0200 Initial commit. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..53eaa21 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +**/*.rs.bk diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..c0c0817 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,181 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "autocfg" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "bitflags" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "blinddate" +version = "0.1.0" +dependencies = [ + "chrono 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", + "enigo 0.0.13 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "chrono" +version = "0.4.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "core-foundation" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "core-foundation-sys 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "core-foundation-sys" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "core-graphics" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "core-foundation 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "foreign-types 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "enigo" +version = "0.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "core-graphics 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", + "objc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "foreign-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "foreign-types-shared 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "foreign-types-shared" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "libc" +version = "0.2.66" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "malloc_buf" +version = "0.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-integer" +version = "0.1.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-traits" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "objc" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "malloc_buf 0.0.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "pkg-config" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "redox_syscall" +version = "0.1.56" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "time" +version = "0.1.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[metadata] +"checksum autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "1d49d90015b3c36167a20fe2810c5cd875ad504b39cff3d4eae7977e6b7c1cb2" +"checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +"checksum chrono 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "31850b4a4d6bae316f7a09e691c944c28299298837edc0a03f755618c23cbc01" +"checksum core-foundation 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "286e0b41c3a20da26536c6000a280585d519fd07b3956b43aed8a79e9edce980" +"checksum core-foundation-sys 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "716c271e8613ace48344f723b60b900a93150271e5be206212d052bbc0883efa" +"checksum core-graphics 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fb0ed45fdc32f9ab426238fba9407dfead7bacd7900c9b4dd3f396f46eafdae3" +"checksum enigo 0.0.13 (registry+https://github.com/rust-lang/crates.io-index)" = "afd3b113b822a9f49524258a2849efe0192b392eb12088a6d70ce0a60df793bb" +"checksum foreign-types 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" +"checksum foreign-types-shared 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" +"checksum libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)" = "d515b1f41455adea1313a4a2ac8a8a477634fbae63cc6100e3aebb207ce61558" +"checksum malloc_buf 0.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "62bb907fe88d54d8d9ce32a3cceab4218ed2f6b7d35617cafe9adf84e43919cb" +"checksum num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "b85e541ef8255f6cf42bbfe4ef361305c6c135d10919ecc26126c4e5ae94bc09" +"checksum num-traits 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "d4c81ffc11c212fa327657cb19dd85eb7419e163b5b076bede2bdb5c974c07e4" +"checksum objc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "915b1b472bc21c53464d6c8461c9d3af805ba1ef837e1cac254428f4a77177b1" +"checksum pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)" = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677" +"checksum redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)" = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84" +"checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f" +"checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6" +"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..e618112 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "blinddate" +version = "0.1.0" +authors = ["Moridius "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +chrono = "0.4.9" +enigo = "0.0.13" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..bd8cc13 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,106 @@ +use chrono::{Date, Datelike, Duration, Local}; +use enigo::*; +use std::env; +use std::process; + +fn command_to_date(command: &str) -> Result, ()> { + let today = Local::today(); + calc_date(command, today) +} + +fn calc_date(command: &str, today: Date) -> Result, ()> { + let wday = today.weekday().num_days_from_monday() as i64; + if command == "today" { + return Ok(today); + } else if command == "tomorrow" { + return Ok(today + Duration::days(1)); + } else if command == "yesterday" { + return Ok(today - Duration::days(1)); + } else if command == "daybeforeyesterday" { + return Ok(today - Duration::days(2)); + } else if command == "dayaftertomorrow" { + return Ok(today + Duration::days(2)); + } else if command.starts_with("next") || command.starts_with("last") { + let target = match &command[4..] { + "monday" => Ok(0), + "tuesday" => Ok(1), + "wednesday" => Ok(2), + "thursday" => Ok(3), + "friday" => Ok(4), + "saturday" => Ok(5), + "sunday" => Ok(6), + _ => Err(()), + }; + + if let Ok(target) = target { + if command.starts_with("next") { + let offset = (target - wday + 7 - 1) % 7 + 1; + return Ok(today + Duration::days(offset)); + } + let offset = (wday - target + 7 - 1) % 7 + 1; + return Ok(today - Duration::days(offset)); + } + }; + Err(()) +} + +fn type_date(date: Date) { + let date_str = format!("{}", date.format("%Y-%m-%d")); + let mut enigo = Enigo::new(); + for c in date_str.chars() { + enigo.key_click(Key::Layout(c)); + } + // enigo.key_click(Key::Return); +} + +fn main() { + let command = env::args() + .nth(1) + .unwrap_or_else(|| "".to_string()) + .to_lowercase(); + + if let Ok(date) = command_to_date(&command) { + type_date(date); + } else { + eprintln!("blinddate"); + eprintln!("Calculates a date and types it. Use with a shell script to enter dates into files rapidly."); + eprintln!("Usage: blinddate "); + eprintln!("Supported commands:"); + eprintln!(" today"); + eprintln!(" tomorrow"); + eprintln!(" yesterday"); + eprintln!(" daybeforeyesterday"); + eprintln!(" dayaftertomorrow"); + eprintln!(" next (e.g. nextmonday)"); + eprintln!(" last (e.g. lastsaturday)"); + process::exit(1); + } +} + +#[cfg(test)] +use chrono::prelude::*; + +#[test] +fn test_calc_date() { + let today = Local.ymd(2020, 9, 1); + assert!(Local.ymd(2020, 9, 1) == calc_date("today", today).unwrap()); + assert!(Local.ymd(2020, 9, 2) == calc_date("tomorrow", today).unwrap()); + assert!(Local.ymd(2020, 8, 31) == calc_date("yesterday", today).unwrap()); + assert!(Local.ymd(2020, 8, 30) == calc_date("daybeforeyesterday", today).unwrap()); + assert!(Local.ymd(2020, 9, 3) == calc_date("dayaftertomorrow", today).unwrap()); + assert!(Local.ymd(2020, 9, 7) == calc_date("nextmonday", today).unwrap()); + assert!(Local.ymd(2020, 9, 8) == calc_date("nexttuesday", today).unwrap()); + assert!(Local.ymd(2020, 9, 2) == calc_date("nextwednesday", today).unwrap()); + assert!(Local.ymd(2020, 9, 3) == calc_date("nextthursday", today).unwrap()); + assert!(Local.ymd(2020, 9, 4) == calc_date("nextfriday", today).unwrap()); + assert!(Local.ymd(2020, 9, 5) == calc_date("nextsaturday", today).unwrap()); + assert!(Local.ymd(2020, 9, 6) == calc_date("nextsunday", today).unwrap()); + assert!(Local.ymd(2020, 8, 31) == calc_date("lastmonday", today).unwrap()); + assert!(Local.ymd(2020, 8, 25) == calc_date("lasttuesday", today).unwrap()); + assert!(Local.ymd(2020, 8, 26) == calc_date("lastwednesday", today).unwrap()); + assert!(Local.ymd(2020, 8, 27) == calc_date("lastthursday", today).unwrap()); + assert!(Local.ymd(2020, 8, 28) == calc_date("lastfriday", today).unwrap()); + assert!(Local.ymd(2020, 8, 29) == calc_date("lastsaturday", today).unwrap()); + assert!(Local.ymd(2020, 8, 30) == calc_date("lastsunday", today).unwrap()); + assert!(calc_date("weirdbullshit", today).is_err()); +}