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 | POSITION = "\033[{0};{1}H" |
41 |
|
42 | UP = "\x1b[A" |
43 | DOWN = "\x1b[B" |
44 | RIGHT = "\x1b[C" |
45 | LEFT = "\x1b[D" |
46 | CTRLC = "\x03" |
47 |
|
48 |
|
49 | # wrap lines |
50 |
|
51 | def wrap(string, width): |
52 | string_length = len(string) |
53 | if string_length <= width: return [string] |
54 | lines = [] |
55 | line = "" |
56 | words = string.split() |
57 | for word in words: |
58 | line += " " |
59 | word_length = len(word) |
60 | if word_length == 0: continue |
61 | if word_length > width: |
62 | for char in word: |
63 | line += char |
64 | line = line.strip() |
65 | if len(line) == width: |
66 | lines.append(line) |
67 | line = "" |
68 | continue |
69 | if len(line) + word_length > width: |
70 | lines.append(line) |
71 | line = word |
72 | else: |
73 | line += word |
74 | line = line.strip() |
75 | line = line.strip() |
76 | if len(line) > 0: |
77 | lines.append(line) |
78 | return lines |
79 |
|
80 | def columnize(items, max): |
81 | string = "" |
82 | for item in items: |
83 | length = item[0] |
84 | content = item[1].strip()[:length] |
85 | string += content.ljust(length, " ") |
86 | return string[:max] |
87 |
|
88 | def pad(string, padding, width): |
89 | length = len(string) |
90 | if length >= width: return string[:length] |
91 | padding_length = (width-length)/2 |
92 | return padding*padding_length + string + padding*padding_length |
93 |
|
94 | def expand(string1, string2, width): |
95 | if len(string1) >= width: return string1[:width] |
96 | full = string1 + string2 |
97 | if len(full) >= width: return full[:width] |
98 | left = width - len(full) |
99 | return (string1 + " "*left + string2)[:width] |
100 |
|