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