| 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 | from utils import * |
| 21 | from text import * |
| 22 |
|
| 23 | class TUI: |
| 24 | screen = None |
| 25 | clines = None |
| 26 | ccols = None |
| 27 | lines = None |
| 28 | def __init__(self): |
| 29 | self.screen = curses_window() |
| 30 | self.get_size() |
| 31 |
|
| 32 | def get_size(self): |
| 33 | size = term_size() |
| 34 | self.clines = size[0] |
| 35 | self.ccols = size[1] |
| 36 |
|
| 37 | def redraw(self): |
| 38 | self.get_size() |
| 39 | curses_clear(self.screen) |
| 40 | l = 0 |
| 41 | for line in self.lines: |
| 42 | if l == self.clines: break |
| 43 | self.screen.move(l, 0) |
| 44 | line_len = 0 |
| 45 | for string in line: |
| 46 | # ("content string", "attributes string") |
| 47 | content = string[0] |
| 48 |
|
| 49 | self.screen.attrset(0) |
| 50 |
|
| 51 | try: |
| 52 | attr = string[1] |
| 53 | except: |
| 54 | attr = "" |
| 55 |
|
| 56 | if "RESET" in attr: # set curses attributes based on attributes string |
| 57 | self.screen.attron(curses.color_pair(0)) |
| 58 | else: |
| 59 | if "BLACK" in attr: |
| 60 | self.screen.attron(curses.color_pair(1)) |
| 61 | if "BLUE" in attr: |
| 62 | self.screen.attron(curses.color_pair(2)) |
| 63 | if "CYAN" in attr: |
| 64 | self.screen.attron(curses.color_pair(3)) |
| 65 | if "GREEN" in attr: |
| 66 | self.screen.attron(curses.color_pair(4)) |
| 67 | if "MAGENTA" in attr: |
| 68 | self.screen.attron(curses.color_pair(5)) |
| 69 | if "RED" in attr: |
| 70 | self.screen.attron(curses.color_pair(6)) |
| 71 | if "WHITE" in attr: |
| 72 | self.screen.attron(curses.color_pair(7)) |
| 73 | if "YELLOW" in attr: |
| 74 | self.screen.attron(curses.color_pair(8)) |
| 75 | if "BOLD" in attr: |
| 76 | self.screen.attron(curses.A_BOLD) |
| 77 | if "STANDOUT" in attr: |
| 78 | self.screen.attron(curses.A_STANDOUT) |
| 79 | if "UNDERLINE" in attr: |
| 80 | self.screen.attron(curses.A_UNDERLINE) |
| 81 |
|
| 82 | out = content[:self.ccols - line_len] |
| 83 | self.screen.addstr(out) |
| 84 | self.screen.attrset(0) |
| 85 | line_len += len(out) |
| 86 | l += 1 |
| 87 | curses_refresh(self.screen) |
| 88 |
|
| 89 | def get_input(self): |
| 90 | input = getch() |
| 91 | if input == UP: input = "up" |
| 92 | elif input == DOWN: input = "down" |
| 93 | elif input == LEFT: input = "left" |
| 94 | elif input == RIGHT: input = "right" |
| 95 | elif input == CTRLC: input = "exit" |
| 96 | else: input = input.decode("utf-8").lower() |
| 97 | return input |
| 98 |
|
| 99 | folder_name = "log_1565561768719" |
| 100 | divider = " | " |
| 101 | title = TITLE |
| 102 |
|
| 103 | def loop(self): |
| 104 | def tab(string, count=1): |
| 105 | return " "*count + string |
| 106 | input = None |
| 107 | try: |
| 108 | while True: |
| 109 | self.get_size() |
| 110 | screen_width = self.ccols |
| 111 |
|
| 112 | self.lines = [ |
| 113 | [(self.title + " - ", "RESET"), (self.folder_name, "BOLD YELLOW")], |
| 114 | "Size: 235 bytes | Timestamp: 1565561768719", |
| 115 | "7 Statements | 2 Events | 0 Unsaved Data Trees", |
| 116 | pad(margin("STATEMENT 5"), ":", screen_width), |
| 117 | "", |
| 118 | "[[LOG START]]", |
| 119 | "v 7 ITEMS", |
| 120 | tab("1565561768752 - INFO - INIT - HELLO"), |
| 121 | tab("1565561768752 - INFO - INIT - HELLO"), |
| 122 | tab("v 4 ITEMS"), |
| 123 | tab("1565561768752 - INFO - INIT - HELLO", 2), |
| 124 | tab("1565561768752 - INFO - INIT - HELLO", 2), |
| 125 | "", |
| 126 | [(tab("1565561768752 - INFO - INIT - HELLO", 2), "BOLD")], |
| 127 | "", |
| 128 | tab("1565561768752 - INFO - INIT - HELLO", 2), |
| 129 | tab("1565561768752 - INFO - INIT - HELLO"), |
| 130 | "", |
| 131 | str(input), |
| 132 | "[[LOG END]", |
| 133 | "", |
| 134 | "", |
| 135 | "", |
| 136 | pad(margin(CONTROLS_MESSAGE), ":", screen_width) |
| 137 | ] |
| 138 | self.redraw() |
| 139 |
|
| 140 | input = self.get_input() |
| 141 |
|
| 142 | if input == "exit" or input == "q": # exit program on Ctrl + C or `q` |
| 143 | break; |
| 144 | finally: |
| 145 | curses_reset() |
| 146 |
|