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 | RESET = "\033[0m" |
24 | BOLD = "\033[1m" |
25 | UNDERLINED = "\033[4m" |
26 | CONTRAST = "\033[7m" |
27 |
|
28 | TEXT_RED = "\033[91m" |
29 | TEXT_GREEN = "\033[92m" |
30 | TEXT_YELLOW = "\033[93m" |
31 | TEXT_MAGENTA = "\033[95m" |
32 | TEXT_CYAN = "\033[96m" |
33 |
|
34 | BACK_RED = "\033[101m" |
35 | BACK_GREEN = "\033[102m" |
36 | BACK_YELLOW = "\033[103m" |
37 | BACK_MAGENTA = "\033[105m" |
38 | BACK_CYAN = "\033[106m" |
39 |
|
40 | UP = "\x1b[A" |
41 | DOWN = "\x1b[B" |
42 | RIGHT = "\x1b[C" |
43 | LEFT = "\x1b[D" |
44 | CTRLC = "\x03" |
45 |
|
46 |
|
47 | # wrap lines |
48 |
|
49 | def wrap(string, width, delimiter=None): |
50 | string_length = len(string) |
51 | if string_length <= width: return [string.replace(delimiter or "\0", " ")] |
52 | lines = [] |
53 | line = "" |
54 | words = string.split(delimiter) |
55 | for word in words: |
56 | line += " " |
57 | word_length = len(word) |
58 | if word_length == 0: continue |
59 | if word_length > width: |
60 | for char in word: |
61 | line += char |
62 | line = line.strip() |
63 | if len(line) == width: |
64 | lines.append(line) |
65 | line = "" |
66 | continue |
67 | if len(line) + word_length > width: |
68 | lines.append(line) |
69 | line = word |
70 | else: |
71 | line += word |
72 | line = line.strip() |
73 | line = line.strip() |
74 | if len(line) > 0: |
75 | lines.append(line) |
76 | return lines |
77 |
|
78 | def columnize(items, width): |
79 | string = "" |
80 | for item in items: |
81 | length = item[0] |
82 | content = item[1].strip()[:length] |
83 | string += content.ljust(length, " ") |
84 | return string[:width] |
85 |
|
86 | def pad(string, padding, width): |
87 | length = len(string) |
88 | if length >= width: return string[:width] |
89 | padding_length = (width-length)/2 |
90 | return (padding*padding_length + string + padding*padding_length)[:width] |
91 |
|
92 | def margin(string, m=" "): |
93 | return m + string + m |
94 |
|
95 | def expand(string1, string2, width): |
96 | if len(string1) >= width: return string1[:width] |
97 | full = string1 + string2 |
98 | if len(full) >= width: return full[:width] |
99 | left = width - len(full) |
100 | return (string1 + " "*left + string2)[:width] |
101 |
|