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