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 |
|
22 | try: |
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 |
39 | except 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 |
|
55 | def 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 |
|
64 | import sys |
65 | def output(*array, end="\n"): |
66 | array = array_stringify(list(array)) |
67 | s = "".join(array) |
68 | sys.stdout.write(s.rstrip() + end) |
69 |
|
70 | def output_lines(array): |
71 | for line in array: |
72 | output(line) |
73 |
|
74 | # terminfo stuff |
75 |
|
76 | from shutil import get_terminal_size |
77 | def 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 |
|
85 | import curses |
86 | def curses_window(): |
87 | # init curses |
88 | screen = curses.initscr() |
89 | curses.start_color() |
90 | curses.use_default_colors() |
91 |
|
92 | curses.init_pair(1, curses.COLOR_BLACK, -1) |
93 | curses.init_pair(2, curses.COLOR_BLUE, -1) |
94 | curses.init_pair(3, curses.COLOR_CYAN, -1) |
95 | curses.init_pair(4, curses.COLOR_GREEN, -1) |
96 | curses.init_pair(5, curses.COLOR_MAGENTA, -1) |
97 | curses.init_pair(6, curses.COLOR_RED, -1) |
98 | curses.init_pair(7, curses.COLOR_WHITE, -1) |
99 | curses.init_pair(8, curses.COLOR_YELLOW, -1) |
100 |
|
101 | # get screen size and create new window |
102 | screen_size = term_size() |
103 |
|
104 | # set cursor visibility to 0 |
105 | curses.curs_set(0) |
106 |
|
107 | # set new window to current |
108 | curses_refresh(screen) |
109 |
|
110 | return screen |
111 |
|
112 | def curses_clear(screen): |
113 | screen.clear() |
114 | screen.touchwin() |
115 | curses_refresh(screen) |
116 |
|
117 | def curses_refresh(screen): |
118 | if screen.is_wintouched(): |
119 | screen.refresh() |
120 |
|
121 | def curses_reset(): |
122 | try: |
123 | if not curses.isendwin(): |
124 | curses.endwin() |
125 | except: |
126 | pass |
127 |
|