# -*- python -*-
from plugins.tzclock import TZClock
from common import *
class MyClock(Entry):
def __init__(self, *args, format = '%Y-%m-%d %T', long_format = '%Y-(%m)%b-%d %T, %a w%V, %Z', tzs = None, **kwargs):
self.default_format = format
self.long_format = long_format
self.clock = G.clock
self.set_format(self.clock, self.default_format)
self.tzs = [] if tzs is None else tzs
self.tzi = 0
self.twilight = '39'
mqueue_map['timefmt'] = self.mqueue
mqueue_map['tz'] = self.mqueue
Entry.__init__(self, *args, **kwargs)
def mqueue(self, args):
if args[0] == 'timefmt':
args = args[1:]
if len(args) == 0:
self.action(0, RIGHT_BUTTON, 0, 0)
else:
self.set_format(self.clock, ' '.join(args))
self.invalidate()
if args[0] == 'tz':
args = args[1:]
if len(args) == 0:
self.action(0, LEFT_BUTTON, 0, 0)
else:
old_clock = self.clock
tz = '/'.join(args)
try:
tzclock = TZClock(timezone = tz, format = self.clock.u_format)
except:
try:
tzclock = TZClock(timezone = tz.upper(), format = self.clock.u_format)
except:
print('%s: unknown typezone: %s' % (sys.argv[0], tz), file = sys.stderr, flush = True)
return
tzclock.u_format = tzclock.format
self.clock = tzclock
if old_clock is not G.clock:
del old_clock
self.invalidate()
def action(self, col, button, x, y):
if button == LEFT_BUTTON:
if self.clock is G.clock:
G.clock.utc = not G.clock.utc
self.set_format(G.clock, self.clock.u_format)
else:
self.set_format(G.clock, self.clock.u_format)
self.clock = G.clock
self.invalidate()
elif button == RIGHT_BUTTON:
if self.clock.u_format == self.default_format:
self.set_format(self.clock, self.long_format)
else:
self.set_format(self.clock, self.default_format)
self.invalidate()
elif button in (SCROLL_UP, SCROLL_DOWN):
i = self.tzi + (+1 if button == SCROLL_UP else -1)
if not (0 <= i <= len(self.tzs)):
return
self.tzi = i
old_clock = self.clock
if i > 0:
tzclock = TZClock(timezone = self.tzs[i - 1], format = self.clock.u_format)
tzclock.u_format = tzclock.format
self.clock = tzclock
else:
self.clock = G.clock
if old_clock is not G.clock:
del old_clock
self.invalidate()
def set_format(self, clck, format):
clck.u_format = format
if not (clck is G.clock and clck.utc):
clck.format = format
else:
fmt, esc = '', False
for c in format:
if esc:
fmt += c
if c == '%' or 'a' <= c.lower() <= 'z':
esc = False
if c == 'Z':
while fmt[-1] != '%':
fmt = fmt[:-1]
fmt = fmt[:-1] + 'UTC'
elif c == '%':
esc = True
fmt += c
else:
fmt += c
clck.format = fmt
def function(self):
clck = self.clock
colour = self.twilight
if clck is not G.clock or clck.utc:
colour += ';7'
return '\033[%sm%s\033[00m' % (colour, clck.read())