dotfiles/polybar/clock.py

92 lines
2.6 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import sys
from datetime import datetime
import time
import pytz
FILL_CLOCK_FACE = False
try:
# if len(sys.argv) < 3:
# print("Usage: {scriptname} timezone_left timezone_right".format(scriptname=sys.argv[0]), file=sys.stderr)
# exit(1)
2018-05-17 17:01:38 +02:00
# try:
# tzleft = pytz.timezone(sys.argv[1])
# tzright = pytz.timezone(sys.argv[2])
# except pytz.exceptions.UnknownTimeZoneError as e:
# print('Unknown time zone "{}"'.format(e.args[0]), file=sys.stderr)
# exit(1)
2018-05-17 17:01:38 +02:00
hourhands = ""
minutehands = ""
2018-05-17 17:01:38 +02:00
def face_for(t):
minute = minutehands[ t.minute // 5 ]
hour = hourhands [ t.hour % 12 ]
if FILL_CLOCK_FACE:
return color_for(t) + "%{T8}%{F#000}%{O-27}" + hour + "%{O-27}" + minute + "%{T-}%{F}"
else:
return "%{O1}%{T8}" + color_for(t) + "%{O-27}" + hour + "%{O-27}" + minute + "%{T-}%{F}"
2018-05-17 17:01:38 +02:00
def color_for(t):
if t.hour < 5 or t.hour > 22:
2018-05-20 23:44:30 +02:00
return "%{F#888}"
2018-05-17 17:01:38 +02:00
elif t.hour < 10:
2018-05-20 23:44:30 +02:00
return "%{F#DB9}"
2018-05-17 17:01:38 +02:00
elif t.hour < 14:
2018-05-20 23:44:30 +02:00
return "%{F#DDB}"
2018-05-17 17:01:38 +02:00
elif t.hour < 19:
2018-05-20 23:44:30 +02:00
return "%{F#DDD}"
2018-05-17 17:01:38 +02:00
else:
2018-05-20 23:44:30 +02:00
return "%{F#D9B}"
2018-05-17 17:01:38 +02:00
while True:
utcnow = datetime.now(tz=pytz.utc)
localnow = datetime.now()
# leftnow = utcnow.astimezone(tzleft )
# rightnow = utcnow.astimezone(tzright)
2018-05-20 23:44:30 +02:00
localcolor = color_for(localnow)
khal_link = lambda x: "%{A1:i3-sensible-terminal -x khal interactive &:}" + x + "%{A}"
clock_link = lambda x, args: "%{A1:i3-sensible-terminal -x tty-clock -cC3 " + args + " &:}" + x + "%{A}"
2018-05-17 17:01:38 +02:00
print(
khal_link(
localcolor +
localnow.strftime("%A") +
"%{F} "
) +
# face_for(leftnow) +
clock_link(" %{T4}" + localcolor + localnow.strftime("%H") + "%{F}", "") +
clock_link("%{O2}%{T8}" + face_for(localnow), "-D") +
clock_link("%{O2}%{T4}" + localcolor + localnow.strftime("%M") + "%{T-}%{F} ", "-s") +
# face_for(rightnow) + " " +
khal_link(
" " +
localcolor +
localnow.strftime("%d %b") +
"%{F}"
),
2018-05-17 17:01:38 +02:00
flush = True
)
2018-05-17 17:01:38 +02:00
delay = 60 - localnow.second - (localnow.microsecond * 10e-7)
time.sleep(delay)
2018-05-17 17:01:38 +02:00
except Exception as e:
print(e, file=sys.stderr)