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