Skip to content

Commit 97bb63b

Browse files
committed
refactor and arg for stdout
1 parent 5963054 commit 97bb63b

File tree

3 files changed

+37
-24
lines changed

3 files changed

+37
-24
lines changed

src/get_state.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,39 @@
11
use active_win_pos_rs::get_active_window;
2+
use chrono::Local;
23
use device_query::{DeviceQuery, DeviceState};
34

5+
// TODO react to keyboard input (e.g. typing)
6+
47
#[derive(Debug, PartialEq)]
58
pub struct State {
9+
pub app_name: String,
610
pub window_title: String,
711
pub cursor: (i32, i32),
812
}
913

14+
const SEPARATOR: &str = ",";
15+
16+
impl State {
17+
pub fn to_csv(&self, time: &chrono::DateTime<Local>,) -> String {
18+
format!("{}{}{}{}{}{}{}{}{}", time.format("%+"), SEPARATOR,self.app_name.replace(SEPARATOR, ""), SEPARATOR, self.window_title.replace(SEPARATOR, ""), SEPARATOR, self.cursor.0,SEPARATOR, self.cursor.1)
19+
}
20+
}
21+
1022
pub(crate) fn get_state() -> State {
1123
let mouse = DeviceState::new().get_mouse();
12-
let window_title = match get_active_window() {
13-
Ok(window) => format!("{}: {}", window.app_name, window.title),
24+
match get_active_window() {
25+
Ok(window) => State {
26+
app_name: window.app_name,
27+
window_title: window.title,
28+
cursor: mouse.coords,
29+
},
1430
Err(e) => {
1531
eprintln!("Error getting active window: {:?}", e);
16-
String::new()
32+
State {
33+
app_name: String::from(""),
34+
window_title: String::from(""),
35+
cursor: mouse.coords,
36+
}
1737
}
18-
};
19-
State {
20-
window_title,
21-
cursor: mouse.coords,
2238
}
2339
}

src/main.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
mod get_state;
22
mod write_csv;
33

4+
use std::env;
45
use chrono::Local;
56
use std::thread::sleep;
67
use std::time::Duration;
78

8-
const FREQUENCY: f64 = 10.0; // seconds
9+
const FREQUENCY: f64 = 2.0; // seconds
10+
const ARG_STDOUT: &str = "--stdout";
911

1012
fn main() {
13+
let args: Vec<String> = env::args().collect();
1114
let mut prev_state = get_state::get_state();
1215
loop {
1316
let current_time = Local::now();
1417
let state = get_state::get_state();
1518
if state != prev_state {
16-
let cursor = format!("{},{}", state.cursor.0, state.cursor.1);
17-
let csv_filename = format!("logs/activity_log_{}.csv", current_time.format("%Y_%m_%d"));
18-
write_csv::add_to_csv(&csv_filename, &current_time, &state.window_title, &cursor);
19+
if args.iter().any(|arg| arg == ARG_STDOUT) {
20+
println!("{}", state.to_csv(&current_time));
21+
} 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);
24+
}
1925
prev_state = state;
2026
}
2127

src/write_csv.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use chrono::Local;
22
use std::fs::{create_dir_all, OpenOptions};
33
use std::path::Path;
44
use std::io::Write;
5+
use crate::get_state;
6+
57

68
/// Appends a log line to the CSV file.
79
///
@@ -10,19 +12,8 @@ use std::io::Write;
1012
pub(crate) fn add_to_csv(
1113
csv_filename: &str,
1214
current_time: &chrono::DateTime<Local>,
13-
window_title: &str,
14-
cursor: &str,
15+
state: &get_state::State,
1516
) {
16-
// Format the time to include milliseconds (as in the Python code)
17-
let ms = current_time.timestamp_subsec_millis();
18-
let time_str = format!("{}.{:03}", current_time.format("%Y-%m-%d %H:%M:%S"), ms);
19-
20-
// Remove any semicolons from the window title
21-
let title_adapted = window_title.replace(";", "");
22-
let line = format!("{};{};{}", time_str, title_adapted, cursor);
23-
24-
println!("{}", line);
25-
2617
// Ensure that the parent directory exists
2718
if let Some(parent) = Path::new(csv_filename).parent() {
2819
if !parent.exists() {
@@ -40,7 +31,7 @@ pub(crate) fn add_to_csv(
4031
.open(csv_filename);
4132
match file_result {
4233
Ok(mut file) => {
43-
if let Err(e) = writeln!(file, "{}", line) {
34+
if let Err(e) = writeln!(file, "{}", state.to_csv(&current_time)) {
4435
eprintln!("Error writing to file {}: {}", csv_filename, e);
4536
}
4637
}

0 commit comments

Comments
 (0)