Added option to trigger an Alt+Tab on startup.

This helps on Windows, where the focus changes on startup so that
Blinddate types into the wrong window.
This commit is contained in:
Moridius 2020-09-03 19:01:44 +02:00
parent ec1e05d677
commit c608fd2668
3 changed files with 87 additions and 75 deletions

128
Cargo.lock generated
View File

@ -2,180 +2,178 @@
# It is not intended for manual editing. # It is not intended for manual editing.
[[package]] [[package]]
name = "autocfg" name = "autocfg"
version = "0.1.7" version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.2.1" version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]] [[package]]
name = "blinddate" name = "blinddate"
version = "0.1.0" version = "0.2.0"
dependencies = [ dependencies = [
"chrono 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", "chrono",
"enigo 0.0.13 (registry+https://github.com/rust-lang/crates.io-index)", "enigo",
] ]
[[package]] [[package]]
name = "chrono" name = "chrono"
version = "0.4.10" version = "0.4.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "942f72db697d8767c22d46a598e01f2d3b475501ea43d0db4f16d90259182d0b"
dependencies = [ dependencies = [
"num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)", "num-integer",
"num-traits 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits",
"time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", "time",
] ]
[[package]] [[package]]
name = "core-foundation" name = "core-foundation"
version = "0.5.1" version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "286e0b41c3a20da26536c6000a280585d519fd07b3956b43aed8a79e9edce980"
dependencies = [ dependencies = [
"core-foundation-sys 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "core-foundation-sys",
"libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", "libc",
] ]
[[package]] [[package]]
name = "core-foundation-sys" name = "core-foundation-sys"
version = "0.5.1" version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "716c271e8613ace48344f723b60b900a93150271e5be206212d052bbc0883efa"
dependencies = [ dependencies = [
"libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", "libc",
] ]
[[package]] [[package]]
name = "core-graphics" name = "core-graphics"
version = "0.13.0" version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb0ed45fdc32f9ab426238fba9407dfead7bacd7900c9b4dd3f396f46eafdae3"
dependencies = [ dependencies = [
"bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "bitflags",
"core-foundation 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "core-foundation",
"foreign-types 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "foreign-types",
"libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", "libc",
] ]
[[package]] [[package]]
name = "enigo" name = "enigo"
version = "0.0.13" version = "0.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "afd3b113b822a9f49524258a2849efe0192b392eb12088a6d70ce0a60df793bb"
dependencies = [ dependencies = [
"core-graphics 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", "core-graphics",
"libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", "libc",
"objc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", "objc",
"pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)", "pkg-config",
"winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "winapi",
] ]
[[package]] [[package]]
name = "foreign-types" name = "foreign-types"
version = "0.3.2" version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1"
dependencies = [ dependencies = [
"foreign-types-shared 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "foreign-types-shared",
] ]
[[package]] [[package]]
name = "foreign-types-shared" name = "foreign-types-shared"
version = "0.1.1" version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.66" version = "0.2.76"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "755456fae044e6fa1ebbbd1b3e902ae19e73097ed4ed87bb79934a867c007bc3"
[[package]] [[package]]
name = "malloc_buf" name = "malloc_buf"
version = "0.0.6" version = "0.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62bb907fe88d54d8d9ce32a3cceab4218ed2f6b7d35617cafe9adf84e43919cb"
dependencies = [ dependencies = [
"libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", "libc",
] ]
[[package]] [[package]]
name = "num-integer" name = "num-integer"
version = "0.1.41" version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d59457e662d541ba17869cf51cf177c0b5f0cbf476c66bdc90bf1edac4f875b"
dependencies = [ dependencies = [
"autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "autocfg",
"num-traits 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits",
] ]
[[package]] [[package]]
name = "num-traits" name = "num-traits"
version = "0.2.10" version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac267bcc07f48ee5f8935ab0d24f316fb722d7a1292e2913f0cc196b29ffd611"
dependencies = [ dependencies = [
"autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "autocfg",
] ]
[[package]] [[package]]
name = "objc" name = "objc"
version = "0.2.7" version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "915b1b472bc21c53464d6c8461c9d3af805ba1ef837e1cac254428f4a77177b1"
dependencies = [ dependencies = [
"malloc_buf 0.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "malloc_buf",
] ]
[[package]] [[package]]
name = "pkg-config" name = "pkg-config"
version = "0.3.17" version = "0.3.18"
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" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d36492546b6af1463394d46f0c834346f31548646f6ba10849802c9c9a27ac33"
[[package]] [[package]]
name = "time" name = "time"
version = "0.1.42" version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255"
dependencies = [ dependencies = [
"libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", "libc",
"redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)", "wasi",
"winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "winapi",
] ]
[[package]] [[package]]
name = "winapi" name = "wasi"
version = "0.3.8" version = "0.10.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [ dependencies = [
"winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "winapi-x86_64-pc-windows-gnu",
] ]
[[package]] [[package]]
name = "winapi-i686-pc-windows-gnu" name = "winapi-i686-pc-windows-gnu"
version = "0.4.0" version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]] [[package]]
name = "winapi-x86_64-pc-windows-gnu" name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0" version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[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"

View File

@ -1,6 +1,6 @@
[package] [package]
name = "blinddate" name = "blinddate"
version = "0.1.0" version = "0.2.0"
authors = ["Moridius <moridius-github@posteo.hu>"] authors = ["Moridius <moridius-github@posteo.hu>"]
edition = "2018" edition = "2018"

View File

@ -2,6 +2,10 @@ use chrono::{Date, Datelike, Duration, Local};
use enigo::*; use enigo::*;
use std::env; use std::env;
use std::process; use std::process;
use std::{thread, time};
#[cfg(test)]
use chrono::prelude::*;
fn command_to_date(command: &str) -> Result<Date<Local>, ()> { fn command_to_date(command: &str) -> Result<Date<Local>, ()> {
let today = Local::today(); let today = Local::today();
@ -44,9 +48,15 @@ fn calc_date(command: &str, today: Date<Local>) -> Result<Date<Local>, ()> {
Err(()) Err(())
} }
fn type_date(date: Date<Local>) { fn type_date(date: Date<Local>, alt_tab: bool) {
let date_str = format!("{}", date.format("%Y-%m-%d")); let date_str = format!("{}", date.format("%Y-%m-%d"));
let mut enigo = Enigo::new(); let mut enigo = Enigo::new();
if alt_tab {
enigo.key_down(Key::Alt);
enigo.key_click(Key::Tab);
enigo.key_up(Key::Alt);
thread::sleep(time::Duration::from_millis(100));
}
for c in date_str.chars() { for c in date_str.chars() {
enigo.key_click(Key::Layout(c)); enigo.key_click(Key::Layout(c));
} }
@ -54,13 +64,20 @@ fn type_date(date: Date<Local>) {
} }
fn main() { fn main() {
let command = env::args() let mut command = String::new();
.nth(1) let mut alt_tab = false;
.unwrap_or_else(|| "".to_string())
.to_lowercase(); for arg in env::args() {
if arg == "--tab" {
alt_tab = true;
} else {
command = arg.to_lowercase();
break;
}
}
if let Ok(date) = command_to_date(&command) { if let Ok(date) = command_to_date(&command) {
type_date(date); type_date(date, alt_tab);
} else { } else {
eprintln!("blinddate"); eprintln!("blinddate");
eprintln!("Calculates a date and types it. Use with a shell script to enter dates into files rapidly."); eprintln!("Calculates a date and types it. Use with a shell script to enter dates into files rapidly.");
@ -77,9 +94,6 @@ fn main() {
} }
} }
#[cfg(test)]
use chrono::prelude::*;
#[test] #[test]
fn test_calc_date() { fn test_calc_date() {
let today = Local.ymd(2020, 9, 1); let today = Local.ymd(2020, 9, 1);