@@ -2,6 +2,7 @@ mod get_state;
22mod write_csv;
33
44use crate :: get_state:: State ;
5+ use auto_launch:: AutoLaunch ;
56use chrono:: Local ;
67use std:: env;
78use std:: path:: Path ;
@@ -10,6 +11,7 @@ use std::time::Duration;
1011
1112const DEFAULT_FREQUENCY : f64 = 2.0 ; // seconds
1213const ARG_STDOUT : & str = "--stdout" ;
14+ const AUTOSTART_APP_NAME : & ' static str = "Work Log" ;
1315
1416fn 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