Index

lognestmonster / 8afd432

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

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
7221 Aug 2019 21:33f5472c4Argument parsing and processingJosh Stockin161N

Blob @ lognestmonster / parser / utils.py

application/x-python3033 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)
30 if buf == b"\x1b": # if escaped
31 while True:
32 ch = sys.stdin.read(1)
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):
65 array = array_stringify(list(array))
66 s = "".join(array)
67 sys.stdout.write(s.rstrip() + "\n")
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)
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.raw()
92 curses.start_color()
93 curses.use_default_colors()
94
95 curses.init_pair(1, curses.COLOR_BLACK, -1)
96 curses.init_pair(2, curses.COLOR_BLUE, -1)
97 curses.init_pair(3, curses.COLOR_CYAN, -1)
98 curses.init_pair(4, curses.COLOR_GREEN, -1)
99 curses.init_pair(5, curses.COLOR_MAGENTA, -1)
100 curses.init_pair(6, curses.COLOR_RED, -1)
101 curses.init_pair(7, curses.COLOR_WHITE, -1)
102 curses.init_pair(8, curses.COLOR_YELLOW, -1)
103
104 # get screen size and create new window
105 screen_size = term_size()
106
107 # set cursor visibility to 0
108 curses.curs_set(0)
109
110 # set new window to current
111 curses_refresh(screen)
112
113 return screen
114
115def curses_clear(screen):
116 screen.clear()
117 screen.touchwin()
118 curses_refresh(screen)
119
120def curses_refresh(screen):
121 if screen.is_wintouched():
122 screen.refresh()
123
124def curses_reset():
125 try:
126 if not curses.isendwin():
127 curses.endwin()
128 except:
129 pass
130