|
1 | 1 | mod get_state; |
2 | 2 | mod write_csv; |
3 | 3 |
|
4 | | -use std::env; |
| 4 | +use crate::get_state::State; |
5 | 5 | use chrono::Local; |
| 6 | +use std::env; |
| 7 | +use std::path::Path; |
6 | 8 | use std::thread::sleep; |
7 | 9 | use std::time::Duration; |
8 | 10 |
|
9 | | -const FREQUENCY: f64 = 2.0; // seconds |
| 11 | +const DEFAULT_FREQUENCY: f64 = 2.0; // seconds |
10 | 12 | const ARG_STDOUT: &str = "--stdout"; |
11 | 13 |
|
12 | 14 | fn main() { |
13 | 15 | let args: Vec<String> = env::args().collect(); |
14 | | - let mut prev_state = get_state::get_state(); |
| 16 | + if args.iter().any(|arg| arg == "--help") { |
| 17 | + println!("Usage: work_log [OPTION]..."); |
| 18 | + println!("Log the active window and cursor position to a CSV file."); |
| 19 | + println!(); |
| 20 | + println!("Options:"); |
| 21 | + println!(" --frequency=SECONDS set the frequency of logging (default: 2.0)"); |
| 22 | + println!(" --file=FILE set the file to write to (default: ~/work_log.csv)"); |
| 23 | + println!(" --stdout print to stdout instead of a file"); |
| 24 | + return; |
| 25 | + } |
| 26 | + let freq = args |
| 27 | + .iter() |
| 28 | + .find(|arg| arg.starts_with("--frequency=")) |
| 29 | + .map(|arg| arg.split('=').last().unwrap()) |
| 30 | + .map(|arg| arg.parse::<f64>().unwrap_or(DEFAULT_FREQUENCY)) |
| 31 | + .unwrap_or(DEFAULT_FREQUENCY); |
| 32 | + let user_home = env::var("HOME").unwrap_or(env::var("USERPROFILE").unwrap_or(".".to_string())); |
| 33 | + let default_file = Path::new(&user_home).join(Path::new("work_log.csv")); |
| 34 | + let file = args |
| 35 | + .iter() |
| 36 | + .find(|arg| arg.starts_with("--file=")) |
| 37 | + .map(|arg| { |
| 38 | + Path::new( |
| 39 | + arg.split('=') |
| 40 | + .last() |
| 41 | + .expect(&format!("Invalid argument: {}", arg)), |
| 42 | + ) |
| 43 | + }) |
| 44 | + .unwrap_or(&default_file); |
| 45 | + println!("Logging to {}", file.display()); |
| 46 | + |
| 47 | + let mut prev_state: State = get_state::get_state(); |
15 | 48 | loop { |
16 | 49 | let current_time = Local::now(); |
17 | 50 | let state = get_state::get_state(); |
18 | 51 | if state != prev_state { |
19 | 52 | if args.iter().any(|arg| arg == ARG_STDOUT) { |
20 | 53 | println!("{}", state.to_csv(¤t_time)); |
21 | 54 | } else { |
22 | | - let csv_filename = format!("logs/activity_log_{}.csv", current_time.format("%Y_%m_%d")); |
23 | | - write_csv::add_to_csv(&csv_filename, ¤t_time, &state); |
| 55 | + write_csv::add_to_csv(file, ¤t_time, &state); |
24 | 56 | } |
25 | 57 | prev_state = state; |
26 | 58 | } |
27 | 59 |
|
28 | | - sleep(Duration::from_secs_f64(FREQUENCY)); |
| 60 | + sleep(Duration::from_secs_f64(freq)); |
29 | 61 | } |
30 | 62 | } |
0 commit comments