Index

lognestmonster / 7b6bdef

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

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
12425 Sep 2019 15:49ee571caAdd ImportError handling for curses moduleJosh Stockin151N

Blob @ lognestmonster / parser / utils.py

application/x-python3132 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 return (lines, cols-1)
82
83# drawing
84
85try:
86 import curses
87except ImportError:
88 print("Python error: no known module `curses`. If using Windows, please install `windows-curses`.")
89 exit(1)
90def curses_window():
91 # init curses
92 screen = curses.initscr()
93 curses.start_color()
94 curses.use_default_colors()
95
96 curses.init_pair(1, curses.COLOR_BLACK, -1)
97 curses.init_pair(2, curses.COLOR_BLUE, -1)
98 curses.init_pair(3, curses.COLOR_CYAN, -1)
99 curses.init_pair(4, curses.COLOR_GREEN, -1)
100 curses.init_pair(5, curses.COLOR_MAGENTA, -1)
101 curses.init_pair(6, curses.COLOR_RED, -1)
102 curses.init_pair(7, curses.COLOR_WHITE, -1)
103 curses.init_pair(8, curses.COLOR_YELLOW, -1)
104
105 # get screen size and create new window
106 screen_size = term_size()
107
108 # set cursor visibility to 0
109 curses.curs_set(0)
110
111 # set new window to current
112 curses_refresh(screen)
113
114 return screen
115
116def curses_clear(screen):
117 screen.clear()
118 screen.touchwin()
119 curses_refresh(screen)
120
121def curses_refresh(screen):
122 if screen.is_wintouched():
123 screen.refresh()
124
125def curses_reset():
126 try:
127 if not curses.isendwin():
128 curses.endwin()
129 except:
130 pass
131