67 lines
1.8 KiB
Python
Executable file
67 lines
1.8 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
|
|
"""
|
|
Logs what window is in focus
|
|
"""
|
|
|
|
import csv
|
|
import datetime
|
|
import os
|
|
import typing
|
|
|
|
import i3ipc
|
|
|
|
|
|
class ScreenTime:
|
|
FIELDS = ["date", "type", "class", "role", "instance", "title"]
|
|
|
|
def write(self, line: typing.Dict) -> None:
|
|
now = datetime.datetime.now()
|
|
line["date"] = now.timestamp()
|
|
|
|
print("WROTE", line)
|
|
with open(self.csv_path, 'a') as typedfd:
|
|
writer = csv.DictWriter(typedfd, fieldnames=self.FIELDS)
|
|
writer.writerow(line)
|
|
|
|
def on_window_event(self, _: i3ipc.connection.Connection,
|
|
e: i3ipc.events.WindowEvent) -> None:
|
|
focused = self.i3.get_tree().find_focused()
|
|
self.write({
|
|
"type": "window_" + e.change,
|
|
"class": focused.window_class,
|
|
"role": focused.window_role,
|
|
"title": focused.window_title,
|
|
"instance": focused.window_instance,
|
|
})
|
|
|
|
def on_mode_event(self, _: i3ipc.connection.Connection,
|
|
e: i3ipc.events.ModeEvent) -> None:
|
|
self.write({
|
|
"type": "mode",
|
|
"title": e.change
|
|
})
|
|
|
|
def __init__(self) -> None:
|
|
self.i3 = i3ipc.Connection()
|
|
self.i3.on(i3ipc.Event.WINDOW, self.on_window_event)
|
|
self.i3.on(i3ipc.Event.MODE, self.on_mode_event)
|
|
|
|
self.csv_path = os.path.join(
|
|
os.path.expanduser(
|
|
os.getenv('XDG_CACHE_PATH', '~/.cache/')),
|
|
'screentime.csv')
|
|
if not os.path.isfile(self.csv_path):
|
|
with open(self.csv_path, 'w') as typedfd:
|
|
writer = csv.DictWriter(typedfd, fieldnames=self.FIELDS)
|
|
writer.writeheader()
|
|
self.write({"type": "start"})
|
|
|
|
def main(self) -> None:
|
|
self.i3.main()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
ST = ScreenTime()
|
|
ST.main()
|