-
Notifications
You must be signed in to change notification settings - Fork 7
/
welcome.py
85 lines (67 loc) · 1.92 KB
/
welcome.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
import os
import struct
from datetime import datetime
from functools import wraps
from random import choice
from settings import *
def _get_terminal_size_linux():
def ioctl_GWINSZ(fd):
try:
import fcntl
import termios
cr = struct.unpack(
'hh',
fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'),
)
return cr
except:
pass
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
if not cr:
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
cr = ioctl_GWINSZ(fd)
os.close(fd)
except:
pass
if not cr:
try:
cr = (os.environ['LINES'], os.environ['COLUMNS'])
except:
return None
return int(cr[1]), int(cr[0])
def print_center_terminal(string):
twidth = _get_terminal_size_linux()[0]
print('\n'.join(
('{:^%s}' % twidth).format(s) for s in string.split('\n')
))
def add_color(color=33):
def _add_color(func):
def _decorator(*args, **kwargs):
print("\033[%sm" % color)
func(*args, **kwargs)
print("\033[0m")
return wraps(func)(_decorator)
return _add_color
@add_color()
def print_ascii_art(filepath):
with open(filepath, 'r') as filename:
ascii_art = filename.read()
print_center_terminal(ascii_art)
@add_color(color=97)
def print_welcome_message():
global WELCOME_MESSAGE
welcome_msg = WELCOME_MESSAGE + ', ' + \
os.environ.get("USER") + "... " + choice(RANDOM_MESSAGES)
print_center_terminal(welcome_msg)
@add_color(color=42)
def print_time():
time = datetime.now()
time_msg = time.strftime('%b %d, %Y')
print_center_terminal(time_msg)
def main():
print_ascii_art(os.path.join(BASE_DIR, ASCII_FILE))
print_welcome_message()
print_time()
if __name__ == "__main__":
main()