-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
Expand file tree
/
Copy pathdisplay.py
More file actions
240 lines (189 loc) · 6.49 KB
/
display.py
File metadata and controls
240 lines (189 loc) · 6.49 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
"""Display interface abstractions for the live profiling collector."""
import contextlib
import curses
from abc import ABC, abstractmethod
class DisplayInterface(ABC):
"""Abstract interface for display operations to enable testing."""
@abstractmethod
def get_dimensions(self):
"""Get terminal dimensions as (height, width)."""
pass
@abstractmethod
def clear(self):
"""Clear the screen."""
pass
@abstractmethod
def refresh(self):
"""Refresh the screen to show changes."""
pass
@abstractmethod
def redraw(self):
"""Redraw the entire window."""
pass
@abstractmethod
def add_str(self, line, col, text, attr=0):
"""Add a string at the specified position."""
pass
@abstractmethod
def get_input(self):
"""Get a character from input (non-blocking). Returns -1 if no input."""
pass
@abstractmethod
def set_nodelay(self, flag):
"""Set non-blocking mode for input."""
pass
@abstractmethod
def has_colors(self):
"""Check if terminal supports colors."""
pass
@abstractmethod
def init_color_pair(self, pair_id, fg, bg):
"""Initialize a color pair."""
pass
@abstractmethod
def get_color_pair(self, pair_id):
"""Get a color pair attribute."""
pass
@abstractmethod
def get_attr(self, name):
"""Get a display attribute by name (e.g., 'A_BOLD', 'A_REVERSE')."""
pass
class CursesDisplay(DisplayInterface):
"""Real curses display implementation."""
def __init__(self, stdscr):
self.stdscr = stdscr
def get_dimensions(self):
return self.stdscr.getmaxyx()
def clear(self):
# Use erase() instead of clear() to avoid flickering
# clear() forces a complete screen redraw, erase() just clears the buffer
self.stdscr.erase()
def refresh(self):
self.stdscr.refresh()
def redraw(self):
# Use noutrefresh + doupdate for smoother updates
self.stdscr.noutrefresh()
curses.doupdate()
def add_str(self, line, col, text, attr=0):
try:
height, width = self.get_dimensions()
if 0 <= line < height and 0 <= col < width:
max_len = width - col - 1
if len(text) > max_len:
text = text[:max_len]
self.stdscr.addstr(line, col, text, attr)
except curses.error:
pass
def get_input(self):
try:
return self.stdscr.getch()
except (KeyError, curses.error):
return -1
def set_nodelay(self, flag):
self.stdscr.nodelay(flag)
def has_colors(self):
return curses.has_colors()
def init_color_pair(self, pair_id, fg, bg):
try:
curses.init_pair(pair_id, fg, bg)
except curses.error:
pass
def get_color_pair(self, pair_id):
return curses.color_pair(pair_id)
def get_attr(self, name):
return getattr(curses, name, 0)
class MockDisplay(DisplayInterface):
"""Mock display for testing."""
def __init__(self, height=40, width=160):
self.height = height
self.width = width
self.buffer = {}
self.cleared = False
self.refreshed = False
self.redrawn = False
self.input_queue = []
self.nodelay_flag = True
self.colors_supported = True
self.color_pairs = {}
def get_dimensions(self):
return (self.height, self.width)
def clear(self):
self.buffer.clear()
self.cleared = True
def refresh(self):
self.refreshed = True
def redraw(self):
self.redrawn = True
def add_str(self, line, col, text, attr=0):
if 0 <= line < self.height and 0 <= col < self.width:
max_len = self.width - col - 1
if len(text) > max_len:
text = text[:max_len]
self.buffer[(line, col)] = (text, attr)
def get_input(self):
if self.input_queue:
return self.input_queue.pop(0)
return -1
def set_nodelay(self, flag):
self.nodelay_flag = flag
def has_colors(self):
return self.colors_supported
def init_color_pair(self, pair_id, fg, bg):
self.color_pairs[pair_id] = (fg, bg)
def get_color_pair(self, pair_id):
return pair_id << 8
def get_attr(self, name):
attrs = {
"A_NORMAL": 0,
"A_BOLD": 1 << 16,
"A_REVERSE": 1 << 17,
"A_UNDERLINE": 1 << 18,
"A_DIM": 1 << 19,
}
return attrs.get(name, 0)
def simulate_input(self, char):
"""Helper method for tests to simulate keyboard input."""
self.input_queue.append(char)
def get_text_at(self, line, col):
"""Helper method for tests to inspect buffer content."""
if (line, col) in self.buffer:
return self.buffer[(line, col)][0]
return None
def get_all_lines(self):
"""Get all display content as a list of lines (for testing)."""
if not self.buffer:
return []
max_line = max(pos[0] for pos in self.buffer.keys())
lines = []
for line_num in range(max_line + 1):
line_parts = []
for col in range(self.width):
if (line_num, col) in self.buffer:
text, _ = self.buffer[(line_num, col)]
line_parts.append((col, text))
# Reconstruct line from parts
if line_parts:
line_parts.sort(key=lambda x: x[0])
line = ""
last_col = 0
for col, text in line_parts:
if col > last_col:
line += " " * (col - last_col)
line += text
last_col = col + len(text)
lines.append(line.rstrip())
else:
lines.append("")
# Remove trailing empty lines
while lines and not lines[-1]:
lines.pop()
return lines
def find_text(self, pattern):
"""Find text matching pattern in buffer (for testing). Returns (line, col) or None."""
for (line, col), (text, _) in self.buffer.items():
if pattern in text:
return (line, col)
return None
def contains_text(self, text):
"""Check if display contains the given text anywhere (for testing)."""
return self.find_text(text) is not None