Skip to content

Instantly share code, notes, and snippets.

@hexnickk
Created October 3, 2019 09:20
Show Gist options
  • Select an option

  • Save hexnickk/37b8037341e8572d43951a0118b8bdaf to your computer and use it in GitHub Desktop.

Select an option

Save hexnickk/37b8037341e8572d43951a0118b8bdaf to your computer and use it in GitHub Desktop.
Dummy osx utility, which helps to work with pomodoro timer intervals
#!/usr/bin/env python3
from subprocess import run, Popen
from re import search
from time import sleep
# NOTE: you will need to install `brightness` utility
# `brew install brightness`
BRIGHTNESS_STEP = 0.2
MAX_BRIGHTNESS = 1
MIN_BRIGHTNESS = 0
POMODORO_ITERATIONS = 3
POMODORO_WORK_TIME = 0.3 * 60
POMODORO_SWITCH_TIME = 0.3 * 60
POMODORO_REST_TIME = 0.3 * 60
# System utils
def execBackground(command, options):
return Popen([command] + options)
def execSync(command, options):
result = run(
[command] + options, capture_output=True
)
if result.returncode:
exit(result.returncode)
return result
def lockComputer():
execSync('pmset', ['displaysleepnow'])
def wakeComputer():
execBackground('caffeinate', ['-u', '-t', '10'])
def notifyUser(message):
execSync(
'osascript',
['-e', 'display notification "%s" with title "Pymodoro"' % message],
)
# Brightness utils
def getCurrentBrightness():
brightness_call = execSync('brightness', ['-l'])
brightness_output = brightness_call.stdout.decode('utf8')
brightness = search("brightness ([0-9.]+)", brightness_output).group(1)
return float(brightness)
def updateBrightness(birghtness_step):
current_brightness = getCurrentBrightness()
calc_brightness = current_brightness + birghtness_step
next_brightness = max(min(calc_brightness, MAX_BRIGHTNESS), MIN_BRIGHTNESS)
execSync('brightness', ['-d', '0', str(next_brightness)])
def main():
for _ in range(POMODORO_ITERATIONS):
notifyUser('It\'s time to work hard')
sleep(POMODORO_WORK_TIME)
notifyUser('Hey man, you need to go rest soon, wrap it up')
updateBrightness(-BRIGHTNESS_STEP)
sleep(POMODORO_SWITCH_TIME)
notifyUser('5')
sleep(1)
notifyUser('4')
sleep(1)
notifyUser('3')
sleep(1)
notifyUser('2')
sleep(1)
notifyUser('1')
sleep(1)
updateBrightness(BRIGHTNESS_STEP)
lockComputer()
sleep(POMODORO_REST_TIME)
wakeComputer()
notifyUser('Go and get some reeeealy good rest')
if (__name__ == "__main__"):
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment