Index

lognestmonster / d00611e

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

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
10211 Sep 2019 19:054cd6577Update getch for Windows NT supportJosh Stockin12612N

Blob @ lognestmonster / parser / utils.py

application/x-python3021 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# get character from stdin
21
22try:
23 import termios, sys, tty
24 def 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
39except ImportError: # Windows NT, no term control
40 import msvrct
41 def getch(cbyte=1):
42 buf = b""
43 for x in range(0, cbyte):
44 buf += msvrct.getch().encode("utf-8")
45 if buf == b"\x1b":
46 while True:
47 ch = msvrct.getch().encode("utf-8")
48 buf += ch
49 if ch.isalpha():
50 break
51 return buf
52
53# array to string
54
55def array_stringify(array):
56 index = 0
57 for i in array:
58 array[index] = str(i)
59 index += 1
60 return array
61
62# echo output
63
64import sys
65def output(*array, end="\n"):
66 array = array_stringify(list(array))
67 s = "".join(array)
68 sys.stdout.write(s.rstrip() + end)
69
70def output_lines(array):
71 for line in array:
72 output(line)
73
74# terminfo stuff
75
76from shutil import get_terminal_size
77def term_size():
78 os_ts = get_terminal_size((80, 24))
79 lines = os_ts.lines
80 cols = os_ts.columns
81 if cols > 80: cols = 80
82 return (lines, cols-1)
83
84# drawing
85
86import curses
87def curses_window():
88 # init curses
89 screen = curses.initscr()
90 curses.start_color()
91 curses.use_default_colors()
92
93 curses.init_pair(1, curses.COLOR_BLACK, -1)
94 curses.init_pair(2, curses.COLOR_BLUE, -1)
95 curses.init_pair(3, curses.COLOR_CYAN, -1)
96 curses.init_pair(4, curses.COLOR_GREEN, -1)
97 curses.init_pair(5, curses.COLOR_MAGENTA, -1)
98 curses.init_pair(6, curses.COLOR_RED, -1)
99 curses.init_pair(7, curses.COLOR_WHITE, -1)
100 curses.init_pair(8, curses.COLOR_YELLOW, -1)
101
102 # get screen size and create new window
103 screen_size = term_size()
104
105 # set cursor visibility to 0
106 curses.curs_set(0)
107
108 # set new window to current
109 curses_refresh(screen)
110
111 return screen
112
113def curses_clear(screen):
114 screen.clear()
115 screen.touchwin()
116 curses_refresh(screen)
117
118def curses_refresh(screen):
119 if screen.is_wintouched():
120 screen.refresh()
121
122def curses_reset():
123 try:
124 if not curses.isendwin():
125 curses.endwin()
126 except:
127 pass
128