-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathlog.py
99 lines (69 loc) · 2.48 KB
/
log.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
"""
Copyright 2020 Ryan Wick ([email protected])
https://github.com/rrwick/Trycycler
This file is part of Trycycler. Trycycler is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by the Free Software Foundation,
either version 3 of the License, or (at your option) any later version. Trycycler is distributed
in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details. You should have received a copy of the GNU General Public License along with Trycycler.
If not, see <http://www.gnu.org/licenses/>.
"""
import datetime
import os
import textwrap
import sys
def log(message='', end='\n'):
print(message, file=sys.stderr, flush=True, end=end)
def log_with_wrapping(message):
terminal_width, _ = get_terminal_size_stderr()
for line in textwrap.wrap(message, width=terminal_width - 1):
log(line)
def section_header(text):
log()
time = get_timestamp()
time_str = dim('(' + time + ')')
header = bold_yellow_underline(text)
print(header + ' ' + time_str, file=sys.stderr, flush=True)
END_FORMATTING = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
RED = '\033[31m'
GREEN = '\033[32m'
MAGENTA = '\033[35m'
YELLOW = '\033[93m'
DIM = '\033[2m'
def bold(text):
return BOLD + text + END_FORMATTING
def bold_yellow(text):
return YELLOW + BOLD + text + END_FORMATTING
def bold_yellow_underline(text):
return YELLOW + BOLD + UNDERLINE + text + END_FORMATTING
def dim(text):
return DIM + text + END_FORMATTING
def red(text):
return RED + text + END_FORMATTING
def bold_red(text):
return RED + BOLD + text + END_FORMATTING
def explanation(text, indent_size=4):
text = ' ' * indent_size + text
terminal_width, _ = get_terminal_size_stderr()
for line in textwrap.wrap(text, width=terminal_width - 1):
log(dim(line))
log()
def quit_with_error(text):
log()
log_with_wrapping(text)
log()
sys.exit()
def get_terminal_size_stderr(fallback=(80, 24)):
"""
Unlike shutil.get_terminal_size, which looks at stdout, this looks at stderr.
"""
try:
size = os.get_terminal_size(sys.__stderr__.fileno())
except (AttributeError, ValueError, OSError):
size = os.terminal_size(fallback)
return size
def get_timestamp():
return '{:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now())