Skip to content

Commit 9b62e4f

Browse files
committed
feat: configurable via args + help
1 parent 97bb63b commit 9b62e4f

5 files changed

Lines changed: 63 additions & 48 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/target
2-
/logs
2+
/logs
3+
.idea

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ edition = "2021"
55

66
[dependencies]
77
chrono = "0.4"
8-
device_query = "0.2"
8+
device_query = "3.0.0"
99
active-win-pos-rs = "0.9"

src/main.rs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,62 @@
11
mod get_state;
22
mod write_csv;
33

4-
use std::env;
4+
use crate::get_state::State;
55
use chrono::Local;
6+
use std::env;
7+
use std::path::Path;
68
use std::thread::sleep;
79
use std::time::Duration;
810

9-
const FREQUENCY: f64 = 2.0; // seconds
11+
const DEFAULT_FREQUENCY: f64 = 2.0; // seconds
1012
const ARG_STDOUT: &str = "--stdout";
1113

1214
fn main() {
1315
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();
1548
loop {
1649
let current_time = Local::now();
1750
let state = get_state::get_state();
1851
if state != prev_state {
1952
if args.iter().any(|arg| arg == ARG_STDOUT) {
2053
println!("{}", state.to_csv(&current_time));
2154
} else {
22-
let csv_filename = format!("logs/activity_log_{}.csv", current_time.format("%Y_%m_%d"));
23-
write_csv::add_to_csv(&csv_filename, &current_time, &state);
55+
write_csv::add_to_csv(file, &current_time, &state);
2456
}
2557
prev_state = state;
2658
}
2759

28-
sleep(Duration::from_secs_f64(FREQUENCY));
60+
sleep(Duration::from_secs_f64(freq));
2961
}
3062
}

src/write_csv.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1+
use crate::get_state;
12
use chrono::Local;
23
use std::fs::{create_dir_all, OpenOptions};
3-
use std::path::Path;
44
use std::io::Write;
5-
use crate::get_state;
5+
use std::path::Path;
66

77

88
/// Appends a log line to the CSV file.
99
///
1010
/// The log line has the format:
1111
/// `YYYY-MM-DD HH:MM:SS.mmm;window_title_without_semicolon;x,y`
1212
pub(crate) fn add_to_csv(
13-
csv_filename: &str,
13+
csv_filename: &Path,
1414
current_time: &chrono::DateTime<Local>,
1515
state: &get_state::State,
1616
) {
1717
// Ensure that the parent directory exists
18-
if let Some(parent) = Path::new(csv_filename).parent() {
18+
if let Some(parent) = csv_filename.parent() {
1919
if !parent.exists() {
2020
println!("creating {}", parent.display());
2121
if let Err(e) = create_dir_all(parent) {
@@ -32,11 +32,11 @@ pub(crate) fn add_to_csv(
3232
match file_result {
3333
Ok(mut file) => {
3434
if let Err(e) = writeln!(file, "{}", state.to_csv(&current_time)) {
35-
eprintln!("Error writing to file {}: {}", csv_filename, e);
35+
eprintln!("Error writing to file {}: {}", csv_filename.display(), e);
3636
}
3737
}
3838
Err(e) => {
39-
eprintln!("Error opening file {}: {}", csv_filename, e);
39+
eprintln!("Error opening file {}: {}", csv_filename.display(), e);
4040
}
4141
}
4242
}

0 commit comments

Comments
 (0)