~yujiri/cfg

31a5001a40f28b9b153617c0251a318a6fc82f82 — Evin Yulo 2 months ago 8f6c154 master
remove old timer script
2 files changed, 0 insertions(+), 57 deletions(-)

M config.fish
D scripts/timer.py
M config.fish => config.fish +0 -5
@@ 65,11 65,6 @@ function alarm
		echo 'pamixer -u -i 100; mpv ~/music >/dev/null 2>&1' | at $argv
	end
end
# tracking time spent on things
function timer
	~/.config/scripts/timer.py $argv &
	disown
end

# fix the delay caused by the did-you-mean behavior in the Artix package.
function fish_command_not_found

D scripts/timer.py => scripts/timer.py +0 -52
@@ 1,52 0,0 @@
#!/usr/bin/python3
import os, sys, json, time, signal

def save(data):
	with open(DIR+'timer.json', 'w') as f:
		json.dump(data, f, indent = 4)

DIR = f'{os.environ["HOME"]}/.config/scripts/'
# The PID of any existing timer.
old_pid = None

# Load data.json.
try:
	with open(DIR+'timer.json') as f:
		data = json.load(f)
except FileNotFoundError:
	data = {}
except json.decoder.JSONDecodeError:
	print("data file corrupt. exiting.", file=sys.stderr)
	sys.exit(1)
# Check timer.pid for info about any existing timer.
try:
	with open(DIR+'timer.pid') as f:
		old_pid, _, old_task = f.read().partition(' ')
except ValueError:
	print("pid file corrupt. exiting.", file=sys.stderr)
	sys.exit(1)
except FileNotFoundError: pass

task = ' '.join(sys.argv[1:])
# With no argument, print status and exit.
if not task:
	json.dump(data, sys.stdout, indent = 4)
	if old_pid: print(f"\nworking on {old_task.strip()} with pid {old_pid}.")
	else: print("not running.")
	sys.exit()

# Kill any old timer regardless of whether we're starting a new one or just stopping.
if old_pid: os.kill(int(old_pid), signal.SIGTERM)
if task == 'stop':
	if not old_pid: print("timer not running.")
	else: os.remove(DIR+'timer.pid')
	sys.exit()

# Write the pid file.
with open(DIR+'timer.pid', 'w') as f:
	print(os.getpid(), task, file=f)

while True:
	time.sleep(60)
	data[task] = data.get(task, 0) + 1
	save(data)