Skip to content

Commit 58cba93

Browse files
committed
(un-)install autostart option
1 parent 958fecf commit 58cba93

File tree

4 files changed

+177
-1
lines changed

4 files changed

+177
-1
lines changed

Cargo.lock

Lines changed: 121 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ edition = "2021"
66
[dependencies]
77
chrono = "0.4"
88
device_query = "3.0.0"
9-
active-win-pos-rs = "0.9"
9+
active-win-pos-rs = "0.9"
10+
auto-launch = "0.5"

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,8 @@ Options:
1515
--frequency=SECONDS set the frequency of logging (default: 2.0)
1616
--file=FILE set the file to write to (default: ~/work_log.csv)
1717
--stdout print to stdout instead of a file
18+
--autostart-install install the program to run at startup.
19+
(options are preserved, do not move the executable afterwards)
20+
--autostart-remove remove the program from startup
1821
--help display this help and exit
1922
```

src/main.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mod get_state;
22
mod write_csv;
33

44
use crate::get_state::State;
5+
use auto_launch::AutoLaunch;
56
use chrono::Local;
67
use std::env;
78
use std::path::Path;
@@ -10,6 +11,7 @@ use std::time::Duration;
1011

1112
const DEFAULT_FREQUENCY: f64 = 2.0; // seconds
1213
const ARG_STDOUT: &str = "--stdout";
14+
const AUTOSTART_APP_NAME: &'static str = "Work Log";
1315

1416
fn main() {
1517
let args: Vec<String> = env::args().collect();
@@ -21,9 +23,58 @@ fn main() {
2123
println!(" --frequency=SECONDS set the frequency of logging (default: 2.0)");
2224
println!(" --file=FILE set the file to write to (default: ~/work_log.csv)");
2325
println!(" --stdout print to stdout instead of a file");
26+
println!(" --autostart-install install the program to run at startup.");
27+
println!(" (options are preserved, do not move the executable afterwards)");
28+
println!(" --autostart-remove remove the program from startup");
2429
println!(" --help display this help and exit");
2530
return;
2631
}
32+
33+
if args.iter().any(|arg| arg == "--autostart-install") {
34+
let vec = args
35+
.iter()
36+
.filter(|arg| *arg != "--autostart-install")
37+
// adapt relative paths to absolute paths:
38+
.map(|arg| {
39+
if arg.starts_with("--file=") {
40+
let abs = env::current_dir()
41+
.expect("could not locate current directory")
42+
.join(Path::new(&arg[7..]))
43+
.canonicalize()
44+
.expect("could not canonicalize path")
45+
.to_str()
46+
.expect("could not convert path to string")
47+
.to_string();
48+
format!("--file={}", abs)
49+
} else {
50+
arg.clone()
51+
}
52+
})
53+
.collect::<Vec<String>>();
54+
let auto = AutoLaunch::new(
55+
AUTOSTART_APP_NAME,
56+
env::current_exe()
57+
.expect("could not locate current executable path")
58+
.to_str()
59+
.unwrap(),
60+
vec.as_slice(),
61+
);
62+
auto.enable().expect("could not enable autostart");
63+
return;
64+
}
65+
if args.iter().any(|arg| arg == "--autostart-remove") {
66+
let auto = AutoLaunch::new(
67+
AUTOSTART_APP_NAME,
68+
env::current_exe()
69+
.expect("could not locate current executable path")
70+
.to_str()
71+
.unwrap(),
72+
&["--minimized"],
73+
);
74+
auto.disable().expect("could not disable autostart");
75+
return;
76+
}
77+
2778
let freq = args
2879
.iter()
2980
.find(|arg| arg.starts_with("--frequency="))

0 commit comments

Comments
 (0)