| 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 |
|
| 52 | # wrap lines |
| 53 |
|
| 54 | def wrap(string, width, delimiter=None): |
| 55 | string_length = len(string) |
| 56 | if string_length <= width: return [string.replace(delimiter or "\0", " ")] |
| 57 | lines = [] |
| 58 | line = "" |
| 59 | words = string.split(delimiter) |
| 60 | for word in words: |
| 61 | line += " " |
| 62 | word_length = len(word) |
| 63 | if word_length == 0: continue |
| 64 | if word_length > width: |
| 65 | for char in word: |
| 66 | line += char |
| 67 | line = line.strip() |
| 68 | if len(line) == width: |
| 69 | lines.append(line) |
| 70 | line = "" |
| 71 | continue |
| 72 | if len(line) + word_length > width: |
| 73 | lines.append(line) |
| 74 | line = word |
| 75 | else: |
| 76 | line += word |
| 77 | line = line.strip() |
| 78 | line = line.strip() |
| 79 | if len(line) > 0: |
| 80 | lines.append(line) |
| 81 | return lines |
| 82 |
|
| 83 | def columnize(items, width): |
| 84 | string = "" |
| 85 | for item in items: |
| 86 | length = int(item[0]) |
| 87 | content = item[1].strip()[:length] |
| 88 | string += content.ljust(length, " ") |
| 89 | return string[:width] |
| 90 |
|
| 91 | def pad(string, padding, width): |
| 92 | length = len(string) |
| 93 | if length >= width: return string[:width] |
| 94 | padding_length = int((width-length)/2) |
| 95 | return (padding*padding_length + string + padding*padding_length)[:width] |
| 96 |
|
| 97 | def margin(string, m=" "): |
| 98 | return m + string + m |
| 99 |
|
| 100 | def expand(string1, string2, width): |
| 101 | if len(string1) >= width: return string1[:width] |
| 102 | full = string1 + string2 |
| 103 | if len(full) >= width: return full[:width] |
| 104 | left = width - len(full) |
| 105 | return (string1 + " "*left + string2)[:width] |
| 106 |
|