Index

lognestmonster / e9e65d5

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

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
7524 Aug 2019 21:51988a3ffbegin writing deserialization methodJosh Stockin122N

Blob @ lognestmonster / parser / format.py

application/x-python2577 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
23RESET = "\033[0m"
24BOLD = "\033[1m"
25UNDERLINED = "\033[4m"
26CONTRAST = "\033[7m"
27
28TEXT_RED = "\033[91m"
29TEXT_GREEN = "\033[92m"
30TEXT_YELLOW = "\033[93m"
31TEXT_MAGENTA = "\033[95m"
32TEXT_CYAN = "\033[96m"
33
34BACK_RED = "\033[101m"
35BACK_GREEN = "\033[102m"
36BACK_YELLOW = "\033[103m"
37BACK_MAGENTA = "\033[105m"
38BACK_CYAN = "\033[106m"
39
40UP = "\x1b[A"
41DOWN = "\x1b[B"
42RIGHT = "\x1b[C"
43LEFT = "\x1b[D"
44CTRLC = "\x03"
45
46
47# wrap lines
48
49def 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
78def columnize(items, width):
79 string = ""
80 for item in items:
81 length = int(item[0])
82 content = item[1].strip()[:length]
83 string += content.ljust(length, " ")
84 return string[:width]
85
86def pad(string, padding, width):
87 length = len(string)
88 if length >= width: return string[:width]
89 padding_length = int((width-length)/2)
90 return (padding*padding_length + string + padding*padding_length)[:width]
91
92def margin(string, m=" "):
93 return m + string + m
94
95def 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