diff --git a/.i3/lemonbar/i3_lemonbar_modules.py b/.i3/lemonbar/i3_lemonbar_modules.py index 2cbadd9..2c78416 100644 --- a/.i3/lemonbar/i3_lemonbar_modules.py +++ b/.i3/lemonbar/i3_lemonbar_modules.py @@ -79,19 +79,37 @@ class DateTimeModule(LemonModule): def _start_module(self): class DateTimeThread(threading.Thread): - def __init__(self): + def __init__(self, secs_mode = False): + self.secs_mode = secs_mode self.last_out = '' + self.w_condition = threading.Condition() self.out_queue = queue.Queue() self.out_queue.readline = self.out_queue.get super().__init__() def run(self): while True: - timestr = time.strftime("%a %d %b %H:%M:%S", time.gmtime()) + fmt = "%a %d %b %H:%M:%S" if self.secs_mode else "%a %d %b %H:%M" + timestr = time.strftime(fmt, time.gmtime()) if timestr != self.last_out: self.last_out = timestr self.out_queue.put('{}\n'.format(timestr)) - time.sleep(0.1) + self.sleep_until_change() + + def sleep_until_change(self): + # Sleep for an appropriate amount of time (depending on if showing seconds or not) + # with the possibility of being woken early + if self.secs_mode: + rem = 0.1 + else: + rem = time.time() % 5.0 # 5 seconds, 60 is unnecessarily long + self.w_condition.acquire() + self.w_condition.wait(rem) + + def wake(self): + self.w_condition.acquire() + self.w_condition.notify() + self.w_condition.release() self.dt_thread = DateTimeThread() self.dt_thread.start() @@ -118,7 +136,7 @@ class DateTimeModule(LemonModule): def _parse_data(self, data): # Date and time self.date.items = [(config.icon_clock, ' '.join(data[0:3]))] - self.time.items = [('', data[3] if self.show_secs else data[3][:-3])] + self.time.items = [('', data[3])] def date_comm(self): subprocess.Popen(['yad', '--no-buttons', '--calendar', '--sticky' @@ -126,7 +144,8 @@ class DateTimeModule(LemonModule): , '--close-on-unfocus']) def toggle_secs(self): - self.show_secs = not self.show_secs + self.dt_thread.secs_mode = not self.dt_thread.secs_mode + self.dt_thread.wake() class ConkyFastModule(LemonModule): conky_config_str = """