Skip to content

Commit

Permalink
Closes #13 - Accepting changes from contributor, with slight changes …
Browse files Browse the repository at this point in the history
…documented in the issue itself.
  • Loading branch information
Bruno Braga committed Jan 13, 2013
1 parent 51c9b98 commit 892d225
Showing 1 changed file with 62 additions and 40 deletions.
102 changes: 62 additions & 40 deletions termsaverlib/screen/clock.py
Expand Up @@ -63,9 +63,23 @@ class ClockScreen(ScreenBase, PositionHelperBase):
cycle is displayed
"""

cformat = "24h"
ampm = False
"""
Defines the format of the datetime to be displayed.
"""

cseparator = ":"
"""
Do not change this (if you do, change also its ASCII representation)
"""

show_separator = True
"""
Defines if the clock separator should be displayed
"""

digmap = {
'0': ' ___ \n / _ \ \n| | | |\n| |_| |\n \___/ \n',
'0': ' ___ \n / _ \ \n| | | |\n| |_| |\n \___/ \n \n',
'1': ' _ \n / | \n | | \n | | \n |_| \n \n',
'2': ' ____ \n|___ \ \n __) |\n / __/ \n|_____|\n \n',
'3': ' _____ \n|___ / \n |_ \ \n ___) |\n|____/ \n \n',
Expand All @@ -77,19 +91,19 @@ class ClockScreen(ScreenBase, PositionHelperBase):
'9': ' ___ \n / _ \ \n| (_) |\n \__, |\n /_/ \n \n',
':': ' \n _ \n (_) \n _ \n (_) \n \n',
'm': ' \n _ _ _ \n| |\n| | | |\n|_|_|_|\n \n',
'p': ' \n _ __ \n| `_ \ \n| |_) |\n| .__/ \n|_| \n', #Added 6th line to use lowercase "p" tail
'p': ' \n _ __ \n| `_ \ \n| |_) |\n| .__/ \n|_| \n',
'a': ' \n __ _ \n / _` |\n| (_| |\n \__,_|\n \n',

' ': ' \n \n \n \n \n \n',
}
"""
Holds the ascii characters to be used by this screen. It is the
simplification of:
___ _ ____ _____ _ _ ____ __ _____ ___ ___
/ _ \ / ||___ \ |___ / | || | | ___| / /_ |___ | ( _ ) / _ \ _ _ _ _ _ __ __ _
___ _ ____ _____ _ _ ____ __ _____ ___ ___
/ _ \ / ||___ \ |___ / | || | | ___| / /_ |___ | ( _ ) / _ \ _ _ _ _ _ __ __ _
| | | || | __) | |_ \ | || |_ |___ \ | '_ \ / / / _ \ | (_) |(_) | | | `_ \ / _` |
| |_| || | / __/ ___) |__ _| ___) || (_) | / / | (_) | \__, | _ | | | | | |_) | | (_| |
\___/ |_||_____||____/ |_| |____/ \___/ /_/ \___/ /_/ (_) |_|_|_| | .__/ \__,_|
|_|
|_|
Extracted from standard font of Figlet (http://www.figlet.org/)
"""
Expand All @@ -101,7 +115,7 @@ def __init__(self):
ScreenBase.__init__(self,
"clock",
_("displays a digital clock on screen"),
{'opts': 'hf', 'long_opts': ['help', 'format']},
{'opts': 'hm', 'long_opts': ['help', 'ampm']},
)
self.cleanup_per_cycle = True

Expand All @@ -128,7 +142,12 @@ def _run_cycle(self):

print text

time.sleep(1)
sleep_time = 1 # usually one cycle per second
if self.ampm:
# special case to show blinking separator
sleep_time = 0.7

time.sleep(sleep_time)

def _usage_options_example(self):
"""
Expand All @@ -145,9 +164,9 @@ def _usage_options_example(self):
Options:
-h, --help Displays this help message
-f, --format Shows the clock in 12 hour format
-m, --ampm Shows the clock in am/pm 12-hour format, without seconds.
""")

Expand All @@ -168,8 +187,8 @@ def _parse_args(self, prepared_args):
if o in ("-h", "--help"):
self.usage()
self.screen_exit()
elif o in ("-f"):
self.cformat = "12h"
elif o in ("-m", "--ampm"):
self.ampm = True
else:
# this should never happen!
raise Exception(_("Unhandled option. See --help for details."))
Expand All @@ -178,32 +197,35 @@ def get_ascii_time(self, date_time):
"""
Returns the ASCII representation of a date.
"""
#Calculates 12 hour clock output if -f is set
if self.cformat == "12h":
hour = date_time.strftime('%H')

pm = "am"

if int(hour) == 12:
pm = "pm"

if int(hour) == 0:
hour = 12

if int(hour) < 2:
hour = 1

if int(hour) > 12:
hour = int(hour) - 12
pm = "pm"
if int(hour) == 12:
pm = "am"

clock = str(hour) + date_time.strftime(':%M') + pm
else: #Uses 24 hour format if -f is not set
hour = date_time.strftime('%H')
clock = str(hour) + date_time.strftime(':%M')


# define clock string based on options (12/24)
if self.ampm:
hour = int(date_time.strftime('%H'))

suffix = "am"
if hour >= 12:
suffix = "pm"

# fix the hour value into modulus of 12
hour = hour % 12
# fix the zero hour value
if hour == 0:
hour = 12

# shows/hides separator for a blinking effect
separator = ""
if self.show_separator:
separator = self.cseparator
self.show_separator = False
else:
separator = " "
self.show_separator = True

clock = "%s%s%s%s" % (hour, separator, date_time.strftime('%M'), suffix)
else:
# 24hs format includes seconds
clock = date_time.strftime('%H' + self.cseparator + '%M' + self.cseparator + '%S')

items = []
for c in clock:
items.append(self.digmap[c])
Expand Down

0 comments on commit 892d225

Please sign in to comment.