-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
175 lines (152 loc) · 5.91 KB
/
main.py
File metadata and controls
175 lines (152 loc) · 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import os
import time
from comprehensivetui.events import ResizeEvent
from comprehensivetui.events.event import Event, KeySentEvent
from comprehensivetui.layouts import VerticalLayout
from comprehensivetui.layouts.constraints import Align, Constraints
from comprehensivetui.layouts.horizontal import HorizontalLayout
from comprehensivetui.utils.definitions import CYAN, RESET
from comprehensivetui.utils.definitions import (
BACKSPACE,
ENTER,
LEFT_ARROW,
RIGHT_ARROW,
UP_ARROW,
DOWN_ARROW,
arrow_pressed,
)
from comprehensivetui.widgets import Frame, Label
from comprehensivetui.widgets.border import Border
from comprehensivetui.widgets.editor import Editor
from comprehensivetui.widgets.image import ArrayLikeImage, Image, Palates
from comprehensivetui.widgets.program import Program
from comprehensivetui.widgets.textboard import TextBoard
class CustomEditor(Editor):
__slots__ = ("debug",)
def __init__(self, debug: Label, *args, **kwargs):
super().__init__(*args, **kwargs)
self.debug = debug
@property
def wrap_len(self) -> int:
return super().wrap_len - 3
def prepare_line(self, line: str, current_line: int, line_i: int, sub_line_i: int):
"""Prepares the line by doing edits to it. Can be modified to add line effects
args:
- line: the sub_line of text we are modifying
- current_line: the current drawing-line we are on
- line_i: the current line of text we are on
- sub_line_i: the sub-line (accounting for line-wrapping) line we are on."""
if sub_line_i == 0:
return f"{line_i}) " + self.pad_line(line)
else:
return f"{" "*(len(str(line_i))-1)}.) " + self.pad_line(line)
def handle_event(self, event: Event) -> bool:
if super().handle_event(event):
return True
match event:
case KeySentEvent(codes):
if len(codes) == 1:
if codes[0] not in (BACKSPACE, ENTER):
self.push_char(chr(codes[0]))
elif (
codes[0] == ENTER
): # enter might be \r (like in windows. so we need to manually do this)
self.push_char("\n")
else:
self.backspace()
elif arrow_pressed(codes):
if codes[-1] == LEFT_ARROW:
self.cursor_left()
if codes[-1] == RIGHT_ARROW:
self.cursor_right()
if codes[-1] == UP_ARROW:
self.cursor_up()
if codes[-1] == DOWN_ARROW:
self.cursor_down()
self.debug.text = (
chr(codes[0])
if codes[0] != ENTER
else "\\n" + f" {self.cursor_row} {self.cursor_col} {self.scroll}"
)
# self.debug._dirty = True
return True
return False
class ExampleProgram(Program):
__slots__ = ("label", "last_timer", "highest", "board")
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.board = TextBoard(Align.center)
self.board.lines = [str(i) for i in range(35)]
self.label = Label(f"{CYAN}Test1\nTest4", Align.center, name="1")
self.last_timer = time.perf_counter()
self.highest = 0
image_data = [[(y * 20, 255, x * 10) for x in range(10)] for y in range(10)]
editor = CustomEditor(self.label)
editor.text = "bozo"
self.set_children(
[
Border(
Frame(
[
self.label,
self.board,
Border(
editor,
),
Image(image_data, Palates.regular),
Label(f"{CYAN}bar", Align.right, name="2"),
Label(f"{CYAN}baz", Align.center, name="3"),
],
VerticalLayout(),
name="Bar",
),
)
]
)
self.set_layout(VerticalLayout())
def on_frame(self):
t = time.perf_counter()
# self.board.lines = self.board.lines + [str(t)]
# self.label.text = f"{t}"
dif = t - self.last_timer
if dif > self.highest:
self.label.text = f"perf: {self.highest:f}"
self.highest = dif
self.last_timer = t
def main():
print("Hello from comprehensivetui!")
program = ExampleProgram(rate=60, title="Example Title")
program.start()
# program = Border(
# Frame(
# [
# Border(
# Label(
# f"{CYAN}foo",
# Align.right,
# name="1",
# constraints=Constraints(min_height=8, max_width=20),
# ),
# name="fungus",
# ),
# Label(f"{CYAN}bar", Align.right, name="2"),
# Label(f"{CYAN}baz", Align.center, name="3"),
# ],
# VerticalLayout(),
# name="Bar",
# ),
# )
# program.set_layout(VerticalLayout())
# term_size = os.get_terminal_size()
# resize_event = ResizeEvent(term_size.columns, term_size.lines)
# program.dispatch_event(resize_event)
# print(program.width, program.height)
# print(program.children[0].width, program.children[0].height)
# program.draw()
# # print(len(program.view.lines), term_size.lines)
# # print("-" * term_size.columns)
# print(program.view.to_flat(program.height, program.width))
# print("-" * term_size.columns)
# print(len(program.view.lines))
if __name__ == "__main__":
main()