Workspace Stopwatch?
Nice question!
The script below creates a logfile: ~/viewport_log.txt
in your home directory, where it reports the current session's viewport (workspace) usage time per viewport.
The report is updated once per two seconds, looking like (in a quick run):
workspace1 0:00:24
workspace2 0:00:05
workspace6 0:00:04
workspace8 0:00:05
in the format
hours:minutse:seconds
As you can see, I only used workspace 1, 2, 6 and 8.
How to use
The script uses the wmctrl -d
command to get the current viewport data, so you need to install it first:
sudo apt-get install wmctrl
Then:
- Copy the script below into an empty file, save it as
workspace_log.py
-
Test-run it by the command:
python3 /path/to/workspace_log.py
Navigate through the different workspaces and open the file
~/viewport_log.txt
to see the result (alternatively, run in a terminalcat ~/viewport_log.txt
for convenient reading, since the log is updated once per second). -
if all works as expected, add the command to your startup applications. Since it will most likely crash if the script is started too early (before the desktop is fully loaded), you probably need to add a small break in the startup command to make it work as a startup application, so the command then is:
/bin/bash -c "sleep 15&&python3 /path/to/workspace_log.py"
To add it to Startup Applications: Dash > Startup Applications > Add, and add the command.
The script
import subprocess
import os
import time
# define / clear log file
home = os.environ["HOME"]
logfile = home+"/"+"viewport_log.txt"
open(logfile, "wt").write("")
vplist = []
def get_res():
# get resolution
xr = subprocess.check_output(["xrandr"]).decode("utf-8").split()
pos = xr.index("current")
return [int(xr[pos+1]), int(xr[pos+3].replace(",", "") )]
def get_dt():
# get the current viewport
res = get_res()
vp_data = subprocess.check_output(["wmctrl", "-d"]).decode("utf-8").split()
dt = [int(n) for n in vp_data[3].split("x")]
cols = int(dt[0]/res[0])
curr_vpdata = [int(n) for n in vp_data[5].split(",")]
curr_col = int(curr_vpdata[0]/res[0])+1; curr_row = int(curr_vpdata[1]/res[1])
return str(curr_col+curr_row*cols)
def time_format(s):
# convert time format from seconds to h:m:s
m, s = divmod(s, 60)
h, m = divmod(m, 60)
return "%d:%02d:%02d" % (h, m, s)
current_time1 = float(time.time())
curr_dt1 = get_dt()
while True:
time.sleep(2)
curr_dt2 = get_dt()
if curr_dt2 == curr_dt1:
current_time2 = float(time.time())
span = current_time2-current_time1
vp = "workspace "+curr_dt1+" . "*10
vplist.sort(key=lambda x: x[0])
if not vp in [v[0] for v in vplist]:
vplist.append([vp, span])
else:
index = vplist.index([vplist[i] for i in range(len(vplist)) if vplist[i][0] == vp][0])
vplist[index][1] = float(vplist[index][1])+span
with open(logfile, "wt") as out:
for item in vplist:
out.write(item[0]+" "+time_format(item[1])+"\n")
current_time1 = current_time2
curr_dt1 = curr_dt2
Properties of the script
The script calculates the exact time span between two moments i.c.w. the used workspaces of those moments (2 seconds as it is, the interval in the line time.sleep(2)
) if the workspaces on both moments are the same, the time is added to the corresponding workspace' total usage time.
If the workspaces on both moments is different, it is clear that there was a workspace switch and the time is added to no workspace's productive time; the time in the overview in ~/viewport_log.txt
is therefore rounded to two seconds per period per workspace.
Edit
Running the script above in the background, you can view the current usage time(s) per workspace by putting the script below under a key combination:
#!/bin/bash
lines="$( cat ~/viewport_log.txt )"
zenity --info --title='Usage per Viewport' --text="$lines"
- Copy the script into an empty file, save it as
view_vplog.sh
-
Run it, while the first script is running in the background, by the command:
sh /path/to/view_vplog.sh
-
Make it available (after testing) with a shortcut key combination: choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command to a key combination of your choice.