| 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 |
| 24 | def getch(): |
| 25 | fd = sys.stdin.fileno() |
| 26 | old_settings = termios.tcgetattr(fd) |
| 27 | try: |
| 28 | tty.setraw(fd) |
| 29 | buf = sys.stdin.read(1) |
| 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.start_color() |
| 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 | screen.refresh() |
| 106 |
|
| 107 | def curses_reset(): |
| 108 | try: |
| 109 | if not curses.isendwin(): |
| 110 | curses.endwin() |
| 111 | except: |
| 112 | pass |
| 113 |
|