/
clock.py
272 lines (230 loc) · 13.1 KB
/
clock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# -*- coding: utf-8 -*-
###############################################################################
#
# file: clock.py
#
# Purpose: refer to python doc for documentation details.
#
# Note: This file is part of Termsaver application, and should not be used
# or executed separately.
#
###############################################################################
#
# Copyright 2012 Termsaver
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
###############################################################################
"""
This module contains a simple screen that displays current time on terminal.
See additional information in the class itself.
The screen class available here is:
* `ClockScreen`
"""
#
# Python mobdules
#
import datetime
import time
#
# Internal modules
#
from termsaverlib.screen.base import ScreenBase
from termsaverlib.screen.helper.position import PositionHelperBase
from termsaverlib import common
from termsaverlib.i18n import _
class ClockScreen(ScreenBase, PositionHelperBase):
"""
Simple screen that displays the time on the terminal window.
The ascii font used was retrieved from Figlet application
(http://www.figlet.org/).
From its base classes, the functionality provided here bases on the
settings defined below:
* clean up each cycle: True
this will force the screen to be cleaned (cleared) before each new
cycle is displayed
"""
ampm = False
"""
Defines the format of the datetime to be displayed.
"""
big = 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
"""
lineindigimap = 6
digmap = {
'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',
'4': ' _ _ \n| || | \n| || | \n|__ | \n |_| \n \n',
'5': ' ____ \n| ___| \n|___ \ \n ___) |\n|____/ \n \n',
'6': ' __ \n / /_ \n| \'_ \ \n| (_) |\n \___/ \n \n',
'7': ' _____ \n|___ |\n / / \n / / \n /_/ \n \n',
'8': ' ___ \n ( _ ) \n / _ \ \n| (_) |\n \___/ \n \n',
'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',
'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/)
"""
digimapbig = {
'1' : ' ███ \n ███ \n ███ \n██████ \n██████ \n██████ \n ███ \n ███ \n ███ \n ███ \n ███ \n ███ \n█████████ \n█████████ \n█████████ \n',
'2' : '█████████ \n█████████ \n█████████ \n ███ \n ███ \n ███ \n█████████ \n█████████ \n█████████ \n███ \n███ \n███ \n█████████ \n█████████ \n█████████ \n',
'3' : '█████████ \n█████████ \n█████████ \n ███ \n ███ \n ███ \n ██████ \n ██████ \n ██████ \n ███ \n ███ \n ███ \n█████████ \n█████████ \n█████████ \n',
'4' : '███ ███ \n███ ███ \n███ ███ \n███ ███ \n███ ███ \n███ ███ \n█████████ \n█████████ \n█████████ \n ███ \n ███ \n ███ \n ███ \n ███ \n ███ \n',
'5' : '█████████ \n█████████ \n█████████ \n███ \n███ \n███ \n█████████ \n█████████ \n█████████ \n ███ \n ███ \n ███ \n█████████ \n█████████ \n█████████ \n',
'6' : '█████████ \n█████████ \n█████████ \n███ \n███ \n███ \n█████████ \n█████████ \n█████████ \n███ ███ \n███ ███ \n███ ███ \n█████████ \n█████████ \n█████████ \n',
'7' : '█████████ \n█████████ \n█████████ \n ███ \n ███ \n ███ \n ███ \n ███ \n ███ \n ███ \n ███ \n ███ \n ███ \n ███ \n ███ \n',
'8' : '█████████ \n█████████ \n█████████ \n███ ███ \n███ ███ \n███ ███ \n█████████ \n█████████ \n█████████ \n███ ███ \n███ ███ \n███ ███ \n█████████ \n█████████ \n█████████ \n',
'9' : '█████████ \n█████████ \n█████████ \n███ ███ \n███ ███ \n███ ███ \n█████████ \n█████████ \n█████████ \n ███ \n ███ \n ███ \n█████████ \n█████████ \n█████████ \n',
'0' : '█████████ \n█████████ \n█████████ \n███ ███ \n███ ███ \n███ ███ \n███ ███ \n███ ███ \n███ ███ \n███ ███ \n███ ███ \n███ ███ \n█████████ \n█████████ \n█████████ \n',
':' : ' \n \n \n███ \n███ \n███ \n \n \n \n███ \n███ \n███ \n \n \n \n',
'a' : ' \n \n \n \n \n \n \n \n \n \n ██ \n█ █ \n█ █ \n ███ \n \n',
'm' : ' \n \n \n \n \n \n \n \n \n \n \n█████ \n█ █ █ \n█ █ █ \n \n',
'p' : ' \n \n \n \n \n \n \n \n \n \n \n███ \n█ █ \n███ \n█ \n',
' ' : ' \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n',
}
def __init__(self):
"""
The constructor of this class.
"""
ScreenBase.__init__(self,
"clock",
_("displays a digital clock on screen"),
{'opts': 'hmb', 'long_opts': ['help', 'ampm', "big"]},
)
self.cleanup_per_cycle = True
def _run_cycle(self):
"""
Executes a cycle of this screen.
"""
# calculate random position based on screen size
self.get_terminal_size()
date_time = datetime.datetime.now()
text = """
%s
%s
""" % (
date_time.strftime('%A, %%d%%s %B %Y') % (date_time.day,
common.get_day_suffix(date_time.day)),
self.get_ascii_time(date_time),
)
text = self.center_text_horizontally(text)
text = self.center_text_vertically(text)
print text
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):
"""
Describe here the options and examples of this screen.
The method `_parse_args` will be handling the parsing of the options
documented here.
Additionally, this is dependent on the values exposed in `cli_opts`,
passed to this class during its instantiation. Only values properly
configured there will be accepted here.
"""
print _("""
Options:
-h, --help Displays this help message
-m, --ampm Shows the clock in am/pm 12-hour format, without seconds.
-b, --big Shows the clock big number format, without seconds.
""")
def _parse_args(self, prepared_args):
"""
Handles the special command-line arguments available for this screen.
Although this is a base screen, having these options prepared here
can save coding for screens that will not change the default options.
See `_usage_options_example` method for documentation on each of the
options being parsed here.
Additionally, this is dependent on the values exposed in `cli_opts`,
passed to this class during its instantiation. Only values properly
configured there will be accepted here.
"""
for o, __ in prepared_args[0]: # optlist, args
if o in ("-h", "--help"):
self.usage()
self.screen_exit()
elif o in ("-m", "--ampm"):
self.ampm = True
elif o in ("-b", "--big"):
self.big = True
self.lineindigimap = 15
self.digmap = self.digimapbig
else:
# this should never happen!
raise Exception(_("Unhandled option. See --help for details."))
def get_ascii_time(self, date_time):
"""
Returns the ASCII representation of a date.
"""
# shows/hides separator for a blinking effect
# Moved here so as not to duplicate in big number. Default used self.cseparator
separator = ""
if self.show_separator:
separator = self.cseparator
self.show_separator = False
else:
separator = " "
self.show_separator = True
# 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
clock = "%s%s%s%s" % (hour, separator, date_time.strftime('%M'), suffix)
elif self.big:
clock = date_time.strftime('%H' + separator + '%M')
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])
output = ''
for i in range(self.lineindigimap): # loop lines of chars - Increased to six for extra font line
temp = ''
for item in items:
temp += item.split('\n')[i]
output += temp + '\n'
return output