├── .gitignore ├── LICENSE ├── README.md ├── asciicanvas.py └── clock.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | 5 | # C extensions 6 | *.so 7 | 8 | # Distribution / packaging 9 | .Python 10 | env/ 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | *.egg-info/ 23 | .installed.cfg 24 | *.egg 25 | 26 | # PyInstaller 27 | # Usually these files are written by a python script from a template 28 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 29 | *.manifest 30 | *.spec 31 | 32 | # Installer logs 33 | pip-log.txt 34 | pip-delete-this-directory.txt 35 | 36 | # Unit test / coverage reports 37 | htmlcov/ 38 | .tox/ 39 | .coverage 40 | .coverage.* 41 | .cache 42 | nosetests.xml 43 | coverage.xml 44 | *,cover 45 | 46 | # Translations 47 | *.mo 48 | *.pot 49 | 50 | # Django stuff: 51 | *.log 52 | 53 | # Sphinx documentation 54 | docs/_build/ 55 | 56 | # PyBuilder 57 | target/ 58 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Dmitry Alimov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ASCII clock 2 | 3 | Python script that prints out a clock in ASCII art style to the console 4 | 5 | Description: 6 | ------------ 7 | Resizing of the clock could be performed from code. 8 | 9 | The result: 10 | 11 | ![ASCII clock screenshot](http://2.bp.blogspot.com/-KDIbrPoKwj4/VSBjCR64mMI/AAAAAAAAAnA/AyfK4uojn7o/s1600/ascii_clock.png) 12 | 13 | For small clock week day and day are hidden: 14 | 15 | ![ASCII clock smaller screenshot](http://1.bp.blogspot.com/-9kx2lIxH24M/VSF-rpOUHRI/AAAAAAAAAnQ/4FH6rwX8RNo/s1600/ascii_clock_smaller.png) 16 | 17 | For linux version added a set of standard chars to draw the clock items with. Here you can see the Linux version of the clock: 18 | 19 | ![ASCII clock linux screenshot](http://4.bp.blogspot.com/-xY-4HvAaQ4o/VSLoILYUQwI/AAAAAAAAAng/X1x9RhWnDtc/s1600/clock_linux.png) 20 | 21 | License: 22 | -------- 23 | Released under [The MIT License](https://github.com/delimitry/ascii_clock/blob/master/LICENSE). 24 | -------------------------------------------------------------------------------- /asciicanvas.py: -------------------------------------------------------------------------------- 1 | #-*- coding: utf-8 -*- 2 | #----------------------------------------------------------------------- 3 | # Author: delimitry 4 | #----------------------------------------------------------------------- 5 | 6 | 7 | class AsciiCanvas(object): 8 | """ 9 | ASCII canvas for drawing in console using ASCII chars 10 | """ 11 | 12 | def __init__(self, cols, lines, fill_char=' '): 13 | """ 14 | Initialize ASCII canvas 15 | """ 16 | if cols < 1 or cols > 1000 or lines < 1 or lines > 1000: 17 | raise Exception('Canvas cols/lines must be in range [1..1000]') 18 | self.cols = cols 19 | self.lines = lines 20 | if not fill_char: 21 | fill_char = ' ' 22 | elif len(fill_char) > 1: 23 | fill_char = fill_char[0] 24 | self.fill_char = fill_char 25 | self.canvas = [[fill_char] * (cols) for _ in range(lines)] 26 | 27 | def clear(self): 28 | """ 29 | Fill canvas with empty chars 30 | """ 31 | self.canvas = [[self.fill_char] * (self.cols) for _ in range(self.lines)] 32 | 33 | def print_out(self): 34 | """ 35 | Print out canvas to console 36 | """ 37 | print(self.get_canvas_as_str()) 38 | 39 | def add_line(self, x0, y0, x1, y1, fill_char='o'): 40 | """ 41 | Add ASCII line (x0, y0 -> x1, y1) to the canvas, fill line with `fill_char` 42 | """ 43 | if not fill_char: 44 | fill_char = 'o' 45 | elif len(fill_char) > 1: 46 | fill_char = fill_char[0] 47 | if x0 > x1: 48 | # swap A and B 49 | x1, x0 = x0, x1 50 | y1, y0 = y0, y1 51 | # get delta x, y 52 | dx = x1 - x0 53 | dy = y1 - y0 54 | # if a length of line is zero just add point 55 | if dx == 0 and dy == 0: 56 | if self.check_coord_in_range(x0, y0): 57 | self.canvas[y0][x0] = fill_char 58 | return 59 | # when dx >= dy use fill by x-axis, and use fill by y-axis otherwise 60 | if abs(dx) >= abs(dy): 61 | for x in range(x0, x1 + 1): 62 | y = y0 if dx == 0 else y0 + int(round((x - x0) * dy / float((dx)))) 63 | if self.check_coord_in_range(x, y): 64 | self.canvas[y][x] = fill_char 65 | else: 66 | if y0 < y1: 67 | for y in range(y0, y1 + 1): 68 | x = x0 if dy == 0 else x0 + int(round((y - y0) * dx / float((dy)))) 69 | if self.check_coord_in_range(x, y): 70 | self.canvas[y][x] = fill_char 71 | else: 72 | for y in range(y1, y0 + 1): 73 | x = x0 if dy == 0 else x1 + int(round((y - y1) * dx / float((dy)))) 74 | if self.check_coord_in_range(x, y): 75 | self.canvas[y][x] = fill_char 76 | 77 | def add_text(self, x, y, text): 78 | """ 79 | Add text to canvas at position (x, y) 80 | """ 81 | for i, c in enumerate(text): 82 | if self.check_coord_in_range(x + i, y): 83 | self.canvas[y][x + i] = c 84 | 85 | def add_rect(self, x, y, w, h, fill_char=' ', outline_char='o'): 86 | """ 87 | Add rectangle filled with `fill_char` and outline with `outline_char` 88 | """ 89 | if not fill_char: 90 | fill_char = ' ' 91 | elif len(fill_char) > 1: 92 | fill_char = fill_char[0] 93 | if not outline_char: 94 | outline_char = 'o' 95 | elif len(outline_char) > 1: 96 | outline_char = outline_char[0] 97 | for px in range(x, x + w): 98 | for py in range(y, y + h): 99 | if self.check_coord_in_range(px, py): 100 | if px == x or px == x + w - 1 or py == y or py == y + h - 1: 101 | self.canvas[py][px] = outline_char 102 | else: 103 | self.canvas[py][px] = fill_char 104 | 105 | def add_nine_patch_rect(self, x, y, w, h, outline_3x3_chars=None): 106 | """ 107 | Add nine-patch rectangle 108 | """ 109 | default_outline_3x3_chars = ( 110 | '.', '-', '.', 111 | '|', ' ', '|', 112 | '`', '-', "'" 113 | ) 114 | if not outline_3x3_chars: 115 | outline_3x3_chars = default_outline_3x3_chars 116 | # filter chars 117 | filtered_outline_3x3_chars = [] 118 | for index, char in enumerate(outline_3x3_chars[0:9]): 119 | if not char: 120 | char = default_outline_3x3_chars[index] 121 | elif len(char) > 1: 122 | char = char[0] 123 | filtered_outline_3x3_chars.append(char) 124 | for px in range(x, x + w): 125 | for py in range(y, y + h): 126 | if self.check_coord_in_range(px, py): 127 | if px == x and py == y: 128 | self.canvas[py][px] = filtered_outline_3x3_chars[0] 129 | elif px == x and y < py < y + h - 1: 130 | self.canvas[py][px] = filtered_outline_3x3_chars[3] 131 | elif px == x and py == y + h - 1: 132 | self.canvas[py][px] = filtered_outline_3x3_chars[6] 133 | elif x < px < x + w - 1 and py == y: 134 | self.canvas[py][px] = filtered_outline_3x3_chars[1] 135 | elif x < px < x + w - 1 and py == y + h - 1: 136 | self.canvas[py][px] = filtered_outline_3x3_chars[7] 137 | elif px == x + w - 1 and py == y: 138 | self.canvas[py][px] = filtered_outline_3x3_chars[2] 139 | elif px == x + w - 1 and y < py < y + h - 1: 140 | self.canvas[py][px] = filtered_outline_3x3_chars[5] 141 | elif px == x + w - 1 and py == y + h - 1: 142 | self.canvas[py][px] = filtered_outline_3x3_chars[8] 143 | else: 144 | self.canvas[py][px] = filtered_outline_3x3_chars[4] 145 | 146 | def check_coord_in_range(self, x, y): 147 | """ 148 | Check that coordinate (x, y) is in range, to prevent out of range error 149 | """ 150 | return 0 <= x < self.cols and 0 <= y < self.lines 151 | 152 | def get_canvas_as_str(self): 153 | """ 154 | Return canvas as a string 155 | """ 156 | return '\n'.join([''.join(col) for col in self.canvas]) 157 | 158 | def __str__(self): 159 | """ 160 | Return canvas as a string 161 | """ 162 | return self.get_canvas_as_str() 163 | -------------------------------------------------------------------------------- /clock.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | #-*- coding: utf-8 -*- 3 | #----------------------------------------------------------------------- 4 | # Author: delimitry 5 | #----------------------------------------------------------------------- 6 | 7 | import os 8 | import time 9 | import math 10 | import datetime 11 | from asciicanvas import AsciiCanvas 12 | 13 | 14 | x_scale_ratio = 1.75 15 | 16 | # Function to hide the cursor in the terminal 17 | def hide_cursor(): 18 | print('\033[?25l', end='') 19 | # Function to show the cursor in the terminal 20 | def show_cursor(): 21 | print('\033[?25h', end='') 22 | 23 | def draw_second_hand(ascii_canvas, seconds, length, fill_char): 24 | """ 25 | Draw second hand 26 | """ 27 | x0 = int(math.ceil(ascii_canvas.cols / 2.0)) 28 | y0 = int(math.ceil(ascii_canvas.lines / 2.0)) 29 | x1 = x0 + int(math.cos((seconds + 45) * 6 * math.pi / 180) * length * x_scale_ratio) 30 | y1 = y0 + int(math.sin((seconds + 45) * 6 * math.pi / 180) * length) 31 | ascii_canvas.add_line(int(x0), int(y0), int(x1), int(y1), fill_char=fill_char) 32 | 33 | 34 | def draw_minute_hand(ascii_canvas, minutes, length, fill_char): 35 | """ 36 | Draw minute hand 37 | """ 38 | x0 = int(math.ceil(ascii_canvas.cols / 2.0)) 39 | y0 = int(math.ceil(ascii_canvas.lines / 2.0)) 40 | x1 = x0 + int(math.cos((minutes + 45) * 6 * math.pi / 180) * length * x_scale_ratio) 41 | y1 = y0 + int(math.sin((minutes + 45) * 6 * math.pi / 180) * length) 42 | ascii_canvas.add_line(int(x0), int(y0), int(x1), int(y1), fill_char=fill_char) 43 | 44 | 45 | def draw_hour_hand(ascii_canvas, hours, minutes, length, fill_char): 46 | """ 47 | Draw hour hand 48 | """ 49 | x0 = int(math.ceil(ascii_canvas.cols / 2.0)) 50 | y0 = int(math.ceil(ascii_canvas.lines / 2.0)) 51 | total_hours = hours + minutes / 60.0 52 | x1 = x0 + int(math.cos((total_hours + 45) * 30 * math.pi / 180) * length * x_scale_ratio) 53 | y1 = y0 + int(math.sin((total_hours + 45) * 30 * math.pi / 180) * length) 54 | ascii_canvas.add_line(int(x0), int(y0), int(x1), int(y1), fill_char=fill_char) 55 | 56 | 57 | def draw_clock_face(ascii_canvas, radius, mark_char): 58 | """ 59 | Draw clock face with hour and minute marks 60 | """ 61 | x0 = ascii_canvas.cols // 2 62 | y0 = ascii_canvas.lines // 2 63 | # draw marks first 64 | for mark in range(1, 12 * 5 + 1): 65 | x1 = x0 + int(math.cos((mark + 45) * 6 * math.pi / 180) * radius * x_scale_ratio) 66 | y1 = y0 + int(math.sin((mark + 45) * 6 * math.pi / 180) * radius) 67 | if mark % 5 != 0: 68 | ascii_canvas.add_text(x1, y1, mark_char) 69 | # start from 1 because at 0 index - 12 hour 70 | for mark in range(1, 12 + 1): 71 | x1 = x0 + int(math.cos((mark + 45) * 30 * math.pi / 180) * radius * x_scale_ratio) 72 | y1 = y0 + int(math.sin((mark + 45) * 30 * math.pi / 180) * radius) 73 | ascii_canvas.add_text(x1, y1, '%s' % mark) 74 | 75 | 76 | def draw_clock(cols, lines): 77 | """ 78 | Draw clock 79 | """ 80 | if cols < 25 or lines < 25: 81 | print('Too little columns/lines for print out the clock!') 82 | exit() 83 | # prepare chars 84 | single_line_border_chars = ('.', '-', '.', '|', ' ', '|', '`', '-', "'") 85 | second_hand_char = '.' 86 | minute_hand_char = 'o' 87 | hour_hand_char = 'O' 88 | mark_char = '`' 89 | if os.name == 'nt': 90 | single_line_border_chars = ('.', '-', '.', '|', ' ', '|', '`', '-', "'") # ('\xDA', '\xC4', '\xBF', '\xB3', '\x20', '\xB3', '\xC0', '\xC4', '\xD9') 91 | second_hand_char = '.' # '\xFA' 92 | minute_hand_char = 'o' # '\xF9' 93 | hour_hand_char = 'O' # 'o' 94 | mark_char = '`' # '\xF9' 95 | # create ascii canvas for clock and eval vars 96 | ascii_canvas = AsciiCanvas(cols, lines) 97 | center_x = int(math.ceil(cols / 2.0)) 98 | center_y = int(math.ceil(lines / 2.0)) 99 | radius = center_y - 5 100 | second_hand_length = int(radius / 1.17) 101 | minute_hand_length = int(radius / 1.25) 102 | hour_hand_length = int(radius / 1.95) 103 | # add clock region and clock face 104 | # removed rectangle around clock, uncomment to add it again 105 | # ascii_canvas.add_rect(5, 3, int(math.floor(cols / 2.0)) * 2 - 9, int(math.floor(lines / 2.0)) * 2 - 5) 106 | draw_clock_face(ascii_canvas, radius, mark_char) 107 | now = datetime.datetime.now() 108 | # add regions with weekday and day if possible 109 | if center_x > 25: 110 | left_pos = int(radius * x_scale_ratio) / 2 - 4 111 | ascii_canvas.add_nine_patch_rect(int(center_x + left_pos), int(center_y - 1), 5, 3, single_line_border_chars) 112 | ascii_canvas.add_text(int(center_x + left_pos + 1), int(center_y), now.strftime('%a')) 113 | ascii_canvas.add_nine_patch_rect(int(center_x + left_pos + 5), int(center_y - 1), 4, 3, single_line_border_chars) 114 | ascii_canvas.add_text(int(center_x + left_pos + 1 + 5), int(center_y), now.strftime('%d')) 115 | # add clock hands 116 | draw_second_hand(ascii_canvas, now.second, second_hand_length, fill_char=second_hand_char) 117 | draw_minute_hand(ascii_canvas, now.minute, minute_hand_length, fill_char=minute_hand_char) 118 | draw_hour_hand(ascii_canvas, now.hour, now.minute, hour_hand_length, fill_char=hour_hand_char) 119 | # print out canvas 120 | ascii_canvas.print_out() 121 | 122 | def main(): 123 | lines = 40 124 | cols = int(lines * x_scale_ratio) 125 | # Hide the cursor 126 | hide_cursor() 127 | try: # You need to start with a try block 128 | # set console window size and screen buffer size 129 | if os.name == 'nt': 130 | os.system('mode con: cols=%s lines=%s' % (cols + 1, lines + 1)) 131 | while True: 132 | os.system('cls' if os.name == 'nt' else 'clear') 133 | draw_clock(cols, lines) 134 | time.sleep(1) 135 | except KeyboardInterrupt: 136 | # This block catches CTRL-C, allowing for a graceful exit 137 | pass 138 | finally: 139 | # Ensure the cursor is shown again when the script exits 140 | show_cursor() 141 | print("\nGoodbye!") # Optional: print a goodbye message, properly indented 142 | 143 | if __name__ == '__main__': 144 | main() 145 | 146 | --------------------------------------------------------------------------------