Last active
October 29, 2019 13:45
-
-
Save ievans3024/3a607163a91cc6772786d657f8e1b92e to your computer and use it in GitHub Desktop.
A simple command-line binary clock
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import curses | |
from argparse import ArgumentParser | |
from datetime import datetime | |
from time import sleep | |
ON = '\N{BLACK CIRCLE}' | |
OFF = '\N{WHITE CIRCLE}' | |
class BinaryClock(object): | |
def __init__(self, time=None, screen=None, decimal=False, _24_hour=False): | |
""" | |
A simple object to store time and create a string to display it | |
:param screen: An optional curses screen | |
:param time: An optional time to display, instead of now, as 24-hour HH:MM:SS | |
:param decimal: Whether or not to display the decimal values next to each line | |
:param _24_hour: Whether or not to display in 24-hour (00-23) format | |
""" | |
self.__now = datetime.now() | |
self.time = time | |
self.__word_template = '{0:0>6b}' | |
self.__screen = screen | |
self.__decimal = decimal | |
self.__24_hour = _24_hour | |
self.hour = 0 | |
self.minute = 0 | |
self.second = 0 | |
self.update() | |
def __str__(self): | |
hour = [ON if int(d) else OFF for d in self.__word_template.format(self.hour)] | |
minute = [ON if int(d) else OFF for d in self.__word_template.format(self.minute)] | |
second = [ON if int(d) else OFF for d in self.__word_template.format(self.second)] | |
if self.__decimal: | |
hour.append(f'{self.hour:0>2}') | |
minute.append(f'{self.minute:0>2}') | |
second.append(f'{self.second:0>2}') | |
hour = f' {" ".join(hour)} ' | |
minute = f' {" ".join(minute)} ' | |
second = f' {" ".join(second)} ' | |
output = '\n'.join([hour, minute, second]) | |
return output | |
@property | |
def time(self): | |
return self.__time | |
@time.setter | |
def time(self, value): | |
self.__time = value | |
try: | |
self.__now = datetime.strptime(value, '%H:%M:%S') | |
except (ValueError, TypeError): | |
pass | |
def update(self): | |
if self.time is None: | |
# only update now if static time is not set | |
self.__now = datetime.now() | |
if not self.__24_hour: | |
if self.__now.hour > 12: | |
self.hour = self.__now.hour - 12 | |
else: | |
self.hour = self.__now.hour | |
else: | |
self.hour = self.__now.hour | |
self.minute = self.__now.minute | |
self.second = self.__now.second | |
if self.__screen is not None: | |
self.__screen.clear() | |
self.__screen.addstr(self.__str__()) | |
self.__screen.refresh() | |
if __name__ == '__main__': | |
parser = ArgumentParser() | |
parser.add_argument('-d', '--decimal', action='store_true', help='Show decimal values') | |
parser.add_argument('-m', '--twenty-four', action='store_true', help='Show hours as 24-hour (00-23)') | |
parser.add_argument('-t', '--time', default=None, help='A time to display as 24-hour HH:MM:SS') | |
args = parser.parse_args() | |
screen = curses.initscr() | |
curses.curs_set(0) | |
c = BinaryClock(screen=screen, time=args.time, decimal=args.decimal, _24_hour=args.twenty_four) | |
c.update() | |
while True: | |
try: | |
sleep(1) | |
if args.time is None: | |
c.update() | |
except KeyboardInterrupt: | |
curses.endwin() | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment