shoko_screen_timer/
lib.rs1use std::{fs, thread};
2use std::time::{Duration, SystemTime, UNIX_EPOCH};
3
4pub fn get_active() -> u64 {
5 SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs()
6}
7
8pub fn format_screen_time(active_seconds: u64) -> String {
9 let hours = active_seconds / 3600;
10 let minutes = (active_seconds % 3600) / 60;
11 let seconds = active_seconds % 60;
12
13 format!(
14 "{{\"text\": \"{:02}:{:02}:{:02}\", \"tooltip\": \"Screen Time\" }}",
15 hours, minutes, seconds)
16}
17
18pub fn active_timer(callback: fn(u64)) {
19 let path = "/dev/shm/active_time.text";
20 let mut active_start = match fs::read_to_string(path) {
21 Ok(val) => val.as_str().trim().parse::<u64>().unwrap_or_else(|_| get_active()),
22 Err(_) => {
23 let active = get_active();
24 let _ = fs::write(
25 path,
26 active.to_string()
27 );
28 active
29 }
30 };
31
32 let mut last_active = get_active();
33
34 loop {
35 let active = get_active();
36 if active > last_active + 5 {
37 let eepy_nix = active - last_active - 1;
38 active_start += eepy_nix;
39 let _ = fs::write(path, active_start.to_string());
40 }
41 last_active = active;
42 if active_start > active {
43 active_start = active;
44 let _ = fs::write(path, active_start.to_string());
45 }
46 let active_seconds = active - active_start;
47 callback(active_seconds);
48 thread::sleep(Duration::from_secs(1));
49 }
50
51}