lemonbar: Breakout condition logic to Shelf class

This commit is contained in:
kuben
2020-01-06 23:01:52 +01:00
committed by Jakub Fojt
parent 3ad338dbe4
commit a57aa206aa
3 changed files with 36 additions and 24 deletions

View File

@@ -91,36 +91,22 @@ class DateTimeModule(LemonModule):
def _start_module(self):
class DateTimeThread(threading.Thread):
def __init__(self, secs_mode = False):
self.secs_mode = secs_mode
self.readline = self.__readline_first
self.output = ''
self.secs_mode = secs_mode
self.last_output = ''
lock = threading.Lock()
self.output_ready = threading.Condition(lock)
self.clock_tick = threading.Condition(lock) # Needed, because sleep may be interrupted when user
# changes H:M to H:M:S
self.shelf = common.Shelf()
self.readline = self.shelf.get
self.clock_tick = threading.Condition(self.shelf.lock) # Needed, because sleep may be
# interrupted when user changes H:M to H:M:S
super().__init__()
def __readline_first(self):
# Should return instantly the first time
self.readline = self.__readline_blocking
return self.output
def __readline_blocking(self):
self.output_ready.acquire()
self.output_ready.wait()
self.output_ready.release()
return self.output
def run(self):
while True:
fmt = "%a %d %b %H:%M:%S" if self.secs_mode else "%a %d %b %H:%M"
timestr = time.strftime(fmt, time.localtime())
if timestr != self.output:
self.output = timestr
self.output_ready.acquire()
self.output_ready.notify()
self.output_ready.release()
if timestr != self.last_output:
self.last_output = timestr
self.shelf.put(timestr)
self.sleep_until_change()
def sleep_until_change(self):