initial commit

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2021-05-11 08:48:21 +08:00
commit 9b1630d71c
5 changed files with 609 additions and 0 deletions

38
.gitattributes vendored Normal file
View File

@ -0,0 +1,38 @@
# Various C code
*.h text eol=lf
*.c text eol=lf
*.s text eol=lf
*.S text eol=lf
# Rust code
*.rs text eol=lf
*.toml text eol=lf
*.lock text eol=lf
# Misc scripting files
Makefile text eol=lf
*.mk text eol=lf
*.sh text eol=lf
*.ps1 text eol=crlf
*.py text eol=lf
# Human-readable files
*.md eol=lf
README.* text eol=lf
LICENSE text eol=lf
*.txt text eol=lf
# Binary files
*.dfu binary
*.png binary
*.jpg binary
*.bin binary
*.elf binary
# Git configuration files
.gitignore text eol=lf
.gitattributes text eol=lf
# System description files
*.svd eol=lf

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
*.map
*.o
*.elf
# Emacs
*~
target/

429
Cargo.lock generated Normal file
View File

@ -0,0 +1,429 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "autocfg"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73"
dependencies = [
"libc",
"num-integer",
"num-traits",
"time",
"winapi",
]
[[package]]
name = "const-sha1"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb58b6451e8c2a812ad979ed1d83378caa5e927eef2622017a45f251457c2c9d"
[[package]]
name = "core-foundation-sys"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea221b5284a47e40033bf9b66f35f984ec0ea2931eb03505246cd27a963f981b"
[[package]]
name = "crossbeam-channel"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06ed27e177f16d65f0f0c22a213e17c696ace5dd64b14258b52f9417ccb52db4"
dependencies = [
"cfg-if",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-deque"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94af6efb46fef72616855b036a624cf27ba656ffc9be1b9a3c931cfc7749a9a9"
dependencies = [
"cfg-if",
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "52fb27eab85b17fbb9f6fd667089e07d6a2eb8743d02639ee7f6a7a7729c9c94"
dependencies = [
"cfg-if",
"crossbeam-utils",
"lazy_static",
"memoffset",
"scopeguard",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4feb231f0d4d6af81aed15928e58ecf5816aa62a2393e2c82f46973e92a9a278"
dependencies = [
"autocfg",
"cfg-if",
"lazy_static",
]
[[package]]
name = "doc-comment"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10"
[[package]]
name = "either"
version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
[[package]]
name = "heck"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87cbf45460356b7deeb5e3415b5563308c0a9b057c85e12b06ad551f98d0a6ac"
dependencies = [
"unicode-segmentation",
]
[[package]]
name = "hermit-abi"
version = "0.1.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c"
dependencies = [
"libc",
]
[[package]]
name = "lark-killer"
version = "0.1.0"
dependencies = [
"chrono",
"sysinfo",
"winrt-notification",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.94"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18794a8ad5b29321f790b55d93dfba91e125cb1a9edbd4f8e3150acc771c1a5e"
[[package]]
name = "memoffset"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f83fb6581e8ed1f85fd45c116db8405483899489e38406156c25eb743554361d"
dependencies = [
"autocfg",
]
[[package]]
name = "ntapi"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f6bb902e437b6d86e03cce10a7e2af662292c5dfef23b65899ea3ac9354ad44"
dependencies = [
"winapi",
]
[[package]]
name = "num-integer"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db"
dependencies = [
"autocfg",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290"
dependencies = [
"autocfg",
]
[[package]]
name = "num_cpus"
version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3"
dependencies = [
"hermit-abi",
"libc",
]
[[package]]
name = "once_cell"
version = "1.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af8b08b04175473088b46763e51ee54da5f9a164bc162f615b91bc179dbf15a3"
[[package]]
name = "proc-macro2"
version = "1.0.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a152013215dca273577e18d2bf00fa862b89b24169fb78c4c95aeb07992c9cec"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rayon"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b0d8e0819fadc20c74ea8373106ead0600e3a67ef1fe8da56e39b9ae7275674"
dependencies = [
"autocfg",
"crossbeam-deque",
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ab346ac5921dc62ffa9f89b7a773907511cdfa5490c572ae9be1be33e8afa4a"
dependencies = [
"crossbeam-channel",
"crossbeam-deque",
"crossbeam-utils",
"lazy_static",
"num_cpus",
]
[[package]]
name = "scopeguard"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "squote"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fccf17fd09e2455ea796d2ad267b64fa2c5cbd8701b2a93b555d2aa73449f7d"
[[package]]
name = "strum"
version = "0.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7318c509b5ba57f18533982607f24070a55d353e90d4cae30c467cdb2ad5ac5c"
dependencies = [
"strum_macros",
]
[[package]]
name = "strum_macros"
version = "0.20.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee8bc6b87a5112aeeab1f4a9f7ab634fe6cbefc4850006df31267f4cfb9e3149"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "syn"
version = "1.0.72"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1e8cdbefb79a9a5a65e0db8b47b723ee907b7c7f8496c76a1770b5c310bab82"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "sysinfo"
version = "0.17.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "501819ce9ea1571e6c1a0330e34efbd2c7893aeb0d24ac1f177d17eb5224ebf2"
dependencies = [
"cfg-if",
"core-foundation-sys",
"doc-comment",
"libc",
"ntapi",
"once_cell",
"rayon",
"winapi",
]
[[package]]
name = "time"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255"
dependencies = [
"libc",
"wasi",
"winapi",
]
[[package]]
name = "unicode-segmentation"
version = "1.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb0d2e7be6ae3a5fa87eed5fb451aff96f2573d2694942e40543ae0bbe19c796"
[[package]]
name = "unicode-xid"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "wasi"
version = "0.10.0+wasi-snapshot-preview1"
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 = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "426842497696b65fbfc575691d94ef65befb248ed1a8c4361e293c724e7ebe61"
dependencies = [
"const-sha1",
"windows_gen",
"windows_macros",
"windows_winmd",
]
[[package]]
name = "windows_gen"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77ac8f0f06b647f42ee5459a8e1ffe41795647582c5926ec3fa363a91aad7d77"
dependencies = [
"proc-macro2",
"quote",
"squote",
"syn",
"windows_gen_macros",
"windows_winmd",
]
[[package]]
name = "windows_gen_macros"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23eac2169a20173b890c496f9e0e1149a92ef29fe4ba96026b72eec363b993f9"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "windows_macros"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9edc57c944eec106c7823b425ab0fd9f90163489e50a4df747f65fcf9030e1fb"
dependencies = [
"proc-macro2",
"quote",
"squote",
"syn",
"windows_gen",
"windows_winmd",
]
[[package]]
name = "windows_winmd"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16d44527d04c9713312ed598f5d6ce3c453754dbfc03ddc376615be4415ffc88"
dependencies = [
"windows_winmd_macros",
]
[[package]]
name = "windows_winmd_macros"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2353f43f512938450614a176abf2b6cb31ac3b84fd71c88470fee571303e3f36"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "winrt-notification"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5ee9845acda665033013f93baec4f71ac0e60a391b530a5a83bdb966c1807ca"
dependencies = [
"strum",
"windows",
"xml-rs",
]
[[package]]
name = "xml-rs"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b07db065a5cf61a7e4ba64f29e67db906fb1787316516c4e6e5ff0fea1efcd8a"

13
Cargo.toml Normal file
View File

@ -0,0 +1,13 @@
[package]
authors = ["Sean Cross <sean@xobs.io>"]
description = "Terminate Lark when it's not office hours"
edition = "2018"
name = "lark-killer"
version = "0.1.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
chrono = "0.4"
sysinfo = "0.17"
winrt-notification = "0.3"

123
src/main.rs Normal file
View File

@ -0,0 +1,123 @@
fn make_holidays() -> std::collections::BTreeMap<chrono::Date<chrono::Local>, &'static str> {
use chrono::offset::TimeZone;
use chrono::Local;
let mut holidays = std::collections::BTreeMap::new();
holidays.insert(Local.ymd(2021, 1, 1), "New Year's Day");
holidays.insert(Local.ymd(2021, 2, 12), "Chinese New Year");
holidays.insert(Local.ymd(2021, 2, 13), "Chinese New Year");
holidays.insert(Local.ymd(2021, 4, 2), "Good Friday");
holidays.insert(Local.ymd(2021, 5, 1), "Labour Day");
holidays.insert(Local.ymd(2021, 5, 13), "Hari Raya Puasa");
holidays.insert(Local.ymd(2021, 7, 20), "Vesak Day");
holidays.insert(Local.ymd(2021, 8, 9), "National Day");
holidays.insert(Local.ymd(2021, 11, 4), "Deepavali");
holidays.insert(Local.ymd(2021, 12, 25), "Christmas");
holidays.insert(Local.ymd(2022, 1, 1), "New Year's Day");
holidays.insert(Local.ymd(2022, 2, 1), "Chinese New Year");
holidays.insert(Local.ymd(2022, 2, 2), "Chinese New Year");
holidays.insert(Local.ymd(2022, 4, 15), "Good Friday");
holidays.insert(Local.ymd(2022, 5, 1), "Labour Day");
holidays.insert(Local.ymd(2022, 5, 2), "Hari Raya Puasa");
holidays.insert(Local.ymd(2022, 5, 3), "Labour Day (Observed)");
holidays.insert(Local.ymd(2022, 5, 15), "Vesak Day");
holidays.insert(Local.ymd(2022, 7, 9), "Hari Raya Haji");
holidays.insert(Local.ymd(2022, 8, 9), "National Day");
holidays.insert(Local.ymd(2022, 10, 24), "Deepavali");
holidays.insert(Local.ymd(2022, 12, 25), "Christmas");
holidays
}
fn process_is_running(name: &str) -> bool {
use sysinfo::{ProcessExt, SystemExt};
// First we update all information of our system struct.
let mut system = sysinfo::System::new_all();
system.refresh_all();
for (_pid, proc_) in system.get_processes() {
if proc_.name() == name {
return true;
}
}
false
}
fn try_terminating(name: &str) -> Option<std::path::PathBuf> {
let mut process_path = None;
use sysinfo::{ProcessExt, SystemExt};
// First we update all information of our system struct.
let mut system = sysinfo::System::new_all();
system.refresh_all();
// Now let's print every process' id and name:
for (pid, proc_) in system.get_processes() {
// println!("{}:{} => status: {:?}", pid, proc_.name(), proc_.status());
if proc_.name() == name {
println!("LARK FOUND!!! PID: {}, status: {:?}", pid, proc_.status());
println!("Original exe: {}", proc_.exe().display());
process_path = Some(proc_.exe().to_owned());
proc_.kill(sysinfo::Signal::Term);
}
}
process_path
}
fn is_working_hours<T: chrono::Datelike + chrono::Timelike>(date_time: &T) -> bool {
let holidays = make_holidays();
if let Some(holiday) = holidays.get(&chrono::Local::today()) {
println!("Happy {}! No work today.", holiday);
return false;
}
match date_time.weekday() {
chrono::Weekday::Sun | chrono::Weekday::Sat => return false,
_ => (),
}
date_time.hour() >= 9 && date_time.hour() <= 18
}
fn main() {
use chrono::Local;
use winrt_notification::{Duration, Sound, Toast};
let mut process_path: Option<std::path::PathBuf> = None;
let process_name = "Lark.exe";
loop {
let local = Local::now(); // e.g. `2014-11-28T21:45:59.324310806+09:00`
let should_be_running = is_working_hours(&local);
if let Some(p) = &process_path {
if should_be_running && !process_is_running(process_name) {
println!("Running Lark!");
std::process::Command::new(p)
.spawn()
.expect("Couldn't run Lark!");
Toast::new(Toast::POWERSHELL_APP_ID)
.title("Started Lark")
.text1("Running Lark because it's time to work")
.sound(Some(Sound::SMS))
.duration(Duration::Short)
.show()
.expect("unable to toast");
}
} else {
if !should_be_running && process_is_running(process_name) {
println!("Terminating Lark, since it should't be running");
process_path = try_terminating(process_name);
if process_path.is_some() {
Toast::new(Toast::POWERSHELL_APP_ID)
.title("Terminated Lark")
.text1("Lark was terminated because it is outside working hours")
.sound(Some(Sound::SMS))
.duration(Duration::Short)
.show()
.expect("unable to toast");
}
}
}
std::thread::sleep(std::time::Duration::from_secs(100));
}
}