1 | # lognestmonster Copyright (c) 2019 Joshua 'joshuas3' Stockin |
2 | # <https://github.com/JoshuaS3/lognestmonster/>. |
3 |
|
4 |
|
5 | # This file is part of lognestmonster. |
6 |
|
7 | # lognestmonster is free software: you can redistribute it and/or modify |
8 | # it under the terms of the GNU General Public License as published by |
9 | # the Free Software Foundation, either version 3 of the License, or |
10 | # (at your option) any later version. |
11 |
|
12 | # lognestmonster is distributed in the hope that it will be useful, |
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | # GNU General Public License for more details. |
16 |
|
17 | # You should have received a copy of the GNU General Public License |
18 | # along with lognestmonster. If not, see <https://www.gnu.org/licenses/>. |
19 |
|
20 |
|
21 | # ANSI codes for output |
22 |
|
23 | import platform |
24 | if platform.system() == "Windows": |
25 | import os |
26 | os.system("color") |
27 |
|
28 | RESET = "\033[0m" |
29 | BOLD = "\033[1m" |
30 | UNDERLINED = "\033[4m" |
31 | CONTRAST = "\033[7m" |
32 |
|
33 | TEXT_RED = "\033[91m" |
34 | TEXT_GREEN = "\033[92m" |
35 | TEXT_YELLOW = "\033[93m" |
36 | TEXT_MAGENTA = "\033[95m" |
37 | TEXT_CYAN = "\033[96m" |
38 |
|
39 | BACK_RED = "\033[101m" |
40 | BACK_GREEN = "\033[102m" |
41 | BACK_YELLOW = "\033[103m" |
42 | BACK_MAGENTA = "\033[105m" |
43 | BACK_CYAN = "\033[106m" |
44 |
|
45 | UP = b"\x1b[A" |
46 | DOWN = b"\x1b[B" |
47 | RIGHT = b"\x1b[C" |
48 | LEFT = b"\x1b[D" |
49 | CTRLC = b"\x03" |
50 |
|
51 | # formatted byte count |
52 |
|
53 | TB = 1024**4 |
54 | GB = 1024**3 |
55 | MB = 1024**2 |
56 | KB = 1024**1 |
57 | def bytecount_string(cbytes): |
58 | if cbytes > TB: |
59 | return str(cbytes/TB) + "TB" |
60 | elif cbytes > GB: |
61 | return str(cbytes/GB) + "GB" |
62 | elif cbytes > MB: |
63 | return str(cbytes/MB) + "MB" |
64 | elif cbytes > KB: |
65 | return str(cbytes/KB) + "KB" |
66 | else: |
67 | return str(cbytes) + "B" |
68 |
|
69 | # truncate float to certain decimal point |
70 |
|
71 | def truncate_decimal(f, places): |
72 | return int(f * (10**places)) / (10**places) |
73 |
|
74 | # wrap lines |
75 |
|
76 | def wrap(string, width, delimiter=None): |
77 | string_length = len(string) |
78 | if string_length <= width: return [string.replace(delimiter or "\0", " ")] |
79 | lines = [] |
80 | line = "" |
81 | words = string.split(delimiter) |
82 | for word in words: |
83 | line += " " |
84 | word_length = len(word) |
85 | if word_length == 0: continue |
86 | if word_length > width: |
87 | for char in word: |
88 | line += char |
89 | line = line.strip() |
90 | if len(line) == width: |
91 | lines.append(line) |
92 | line = "" |
93 | continue |
94 | if len(line) + word_length > width: |
95 | lines.append(line) |
96 | line = word |
97 | else: |
98 | line += word |
99 | line = line.strip() |
100 | line = line.strip() |
101 | if len(line) > 0: |
102 | lines.append(line) |
103 | return lines |
104 |
|
105 | def columnize(items, width): |
106 | string = "" |
107 | for item in items: |
108 | length = int(item[0]) |
109 | content = item[1].strip()[:length] |
110 | string += content.ljust(length, " ") |
111 | return string[:width] |
112 |
|
113 | def pad(string, padding, width): |
114 | length = len(string) |
115 | if length >= width: return string[:width] |
116 | padding_length = int((width-length)/2) |
117 | return (padding*padding_length + string + padding*padding_length)[:width] |
118 |
|
119 | def margin(string, m=" "): |
120 | return m + string + m |
121 |
|
122 | def expand(string1, string2, width): |
123 | if len(string1) >= width: return string1[:width] |
124 | full = string1 + string2 |
125 | if len(full) >= width: return full[:width] |
126 | left = width - len(full) |
127 | return (string1 + " "*left + string2)[:width] |
128 |
|