Index

lognestmonster / 878a8f8

A general-purpose single-header C logging library and parser for event-based logs. (Incomplete)

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
10011 Sep 2019 15:3243f5552Color update for Windows terminalJosh Stockin150N

Blob @ lognestmonster / parser / format.py

application/x-python2665 bytesdownload raw
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
23import platform
24if platform.system() == "Windows":
25 import os
26 os.system("color")
27
28RESET = "\033[0m"
29BOLD = "\033[1m"
30UNDERLINED = "\033[4m"
31CONTRAST = "\033[7m"
32
33TEXT_RED = "\033[91m"
34TEXT_GREEN = "\033[92m"
35TEXT_YELLOW = "\033[93m"
36TEXT_MAGENTA = "\033[95m"
37TEXT_CYAN = "\033[96m"
38
39BACK_RED = "\033[101m"
40BACK_GREEN = "\033[102m"
41BACK_YELLOW = "\033[103m"
42BACK_MAGENTA = "\033[105m"
43BACK_CYAN = "\033[106m"
44
45UP = b"\x1b[A"
46DOWN = b"\x1b[B"
47RIGHT = b"\x1b[C"
48LEFT = b"\x1b[D"
49CTRLC = b"\x03"
50
51
52# wrap lines
53
54def 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
83def 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
91def 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
97def margin(string, m=" "):
98 return m + string + m
99
100def 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