Index

lognestmonster / d582544

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

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
9407 Sep 2019 22:046177dd6updatesJosh Stockin156N

Blob @ lognestmonster / parser / utils.py

application/x-python3062 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# get character from stdin
22
23import termios, sys, tty, os, fcntl
24def getch(cbytes=1):
25 fd = sys.stdin.fileno()
26 old_settings = termios.tcgetattr(fd)
27 try:
28 tty.setraw(fd)
29 buf = sys.stdin.read(cbytes).encode("utf-8")
30 if buf == b"\x1b": # if escaped
31 while True:
32 ch = sys.stdin.read(1).encode("utf-8")
33 buf += ch
34 if ch.isalpha():
35 break
36 finally:
37 termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
38 return buf
39
40# command
41
42import subprocess
43def command(*args):
44 cmd_string = ""
45 for i in args:
46 cmd_string += i + " "
47 cmd_string.rstrip()
48 cmd_array = cmd_string.split(" ")
49 readable = subprocess.Popen(cmd_array, stdout=subprocess.PIPE)
50 return readable.stdout.read().decode("latin-1").strip()
51
52# array to string
53
54def array_stringify(array):
55 index = 0
56 for i in array:
57 array[index] = str(i)
58 index += 1
59 return array
60
61# echo output
62
63import sys
64def output(*array, end="\n"):
65 array = array_stringify(list(array))
66 s = "".join(array)
67 sys.stdout.write(s.rstrip() + end)
68
69def output_lines(array):
70 for line in array:
71 output(line)
72
73# terminfo stuff
74
75def term_size():
76 lines = int(command("tput lines"))
77 cols = int(command("tput cols"))
78 if cols > 80: cols = 80
79 return (lines, cols-1)
80
81# drawing
82
83try:
84 import curses
85except ImportError:
86 output("curses not installed. if on Windows run `pip install windows-curses`")
87 exit(1)
88def curses_window():
89 # init curses
90 screen = curses.initscr()
91 curses.start_color()
92 curses.use_default_colors()
93
94 curses.init_pair(1, curses.COLOR_BLACK, -1)
95 curses.init_pair(2, curses.COLOR_BLUE, -1)
96 curses.init_pair(3, curses.COLOR_CYAN, -1)
97 curses.init_pair(4, curses.COLOR_GREEN, -1)
98 curses.init_pair(5, curses.COLOR_MAGENTA, -1)
99 curses.init_pair(6, curses.COLOR_RED, -1)
100 curses.init_pair(7, curses.COLOR_WHITE, -1)
101 curses.init_pair(8, curses.COLOR_YELLOW, -1)
102
103 # get screen size and create new window
104 screen_size = term_size()
105
106 # set cursor visibility to 0
107 curses.curs_set(0)
108
109 # set new window to current
110 curses_refresh(screen)
111
112 return screen
113
114def curses_clear(screen):
115 screen.clear()
116 screen.touchwin()
117 curses_refresh(screen)
118
119def curses_refresh(screen):
120 if screen.is_wintouched():
121 screen.refresh()
122
123def curses_reset():
124 try:
125 if not curses.isendwin():
126 curses.endwin()
127 except:
128 pass
129