Index

lognestmonster / 7b6bdef

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

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
12817 Nov 2019 17:0613604edBring updates to presentJosh Stockin1220N

Blob @ lognestmonster / parser / format.py

application/x-python3125 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# formatted byte count
52
53TB = 1024**4
54GB = 1024**3
55MB = 1024**2
56KB = 1024**1
57def bytecount_string(cbytes):
58 if cbytes > TB:
59 return str(cbytes/TB) + "TB"
60 elif cbytes > GB:
61 return str(cbytes/GB) + "GB"
62 elif cbytes > MB:
63 return str(cbytes/MB) + "MB"
64 elif cbytes > KB:
65 return str(cbytes/KB) + "KB"
66 else:
67 return str(cbytes) + "B"
68
69# truncate float to certain decimal point
70
71def truncate_decimal(f, places):
72 return int(f * (10**places)) / (10**places)
73
74# wrap lines
75
76def wrap(string, width, delimiter=None):
77 string_length = len(string)
78 if string_length <= width: return [string.replace(delimiter or "\0", " ")]
79 lines = []
80 line = ""
81 words = string.split(delimiter)
82 for word in words:
83 line += " "
84 word_length = len(word)
85 if word_length == 0: continue
86 if word_length > width:
87 for char in word:
88 line += char
89 line = line.strip()
90 if len(line) == width:
91 lines.append(line)
92 line = ""
93 continue
94 if len(line) + word_length > width:
95 lines.append(line)
96 line = word
97 else:
98 line += word
99 line = line.strip()
100 line = line.strip()
101 if len(line) > 0:
102 lines.append(line)
103 return lines
104
105def columnize(items, width):
106 string = ""
107 for item in items:
108 length = int(item[0])
109 content = item[1].strip()[:length]
110 string += content.ljust(length, " ")
111 return string[:width]
112
113def pad(string, padding, width):
114 length = len(string)
115 if length >= width: return string[:width]
116 padding_length = int((width-length)/2)
117 return (padding*padding_length + string + padding*padding_length)[:width]
118
119def margin(string, m=" "):
120 return m + string + m
121
122def expand(string1, string2, width):
123 if len(string1) >= width: return string1[:width]
124 full = string1 + string2
125 if len(full) >= width: return full[:width]
126 left = width - len(full)
127 return (string1 + " "*left + string2)[:width]
128