blob: bc1fbf00d3722f1e8f8f09b0b1996cd3db57fedf [file] [log] [blame]
Zachary Turnerc1b7cd72015-11-05 19:22:28 +00001from __future__ import absolute_import
Zachary Turner19474e12015-11-03 19:20:39 +00002
Zachary Turnerc1b7cd72015-11-05 19:22:28 +00003# System modules
4import curses
5import curses.panel
Greg Clayton87349242015-09-22 00:35:20 +00006import sys
7import time
Greg Clayton1827fc22015-09-19 00:39:09 +00008
Zachary Turnerc1b7cd72015-11-05 19:22:28 +00009# Third-party modules
10import six
11
12# LLDB modules
13
Greg Clayton1827fc22015-09-19 00:39:09 +000014class Point(object):
15 def __init__(self, x, y):
16 self.x = x
17 self.y = y
18
19 def __repr__(self):
20 return str(self)
21
22 def __str__(self):
23 return "(x=%u, y=%u)" % (self.x, self.y)
Greg Claytonc12cc592015-10-16 00:34:18 +000024
25 def __eq__(self, rhs):
26 return self.x == rhs.x and self.y == rhs.y
27
28 def __ne__(self, rhs):
29 return self.x != rhs.x or self.y != rhs.y
Greg Clayton1827fc22015-09-19 00:39:09 +000030
31 def is_valid_coordinate(self):
32 return self.x >= 0 and self.y >= 0
33
34class Size(object):
35 def __init__(self, w, h):
36 self.w = w
37 self.h = h
38
39 def __repr__(self):
40 return str(self)
41
42 def __str__(self):
43 return "(w=%u, h=%u)" % (self.w, self.h)
44
Greg Claytonc12cc592015-10-16 00:34:18 +000045 def __eq__(self, rhs):
46 return self.w == rhs.w and self.h == rhs.h
47
48 def __ne__(self, rhs):
49 return self.w != rhs.w or self.h != rhs.h
50
Greg Clayton1827fc22015-09-19 00:39:09 +000051class Rect(object):
52 def __init__(self, x=0, y=0, w=0, h=0):
53 self.origin = Point(x, y)
54 self.size = Size(w, h)
55
56 def __repr__(self):
57 return str(self)
58
59 def __str__(self):
60 return "{ %s, %s }" % (str(self.origin), str(self.size))
61
62 def get_min_x(self):
63 return self.origin.x
64
65 def get_max_x(self):
66 return self.origin.x + self.size.w
67
68 def get_min_y(self):
69 return self.origin.y
70
71 def get_max_y(self):
72 return self.origin.y + self.size.h
73
74 def contains_point(self, pt):
75 if pt.x < self.get_max_x():
76 if pt.y < self.get_max_y():
77 if pt.x >= self.get_min_y():
78 return pt.y >= self.get_min_y()
79 return False
80
Greg Claytonc12cc592015-10-16 00:34:18 +000081 def __eq__(self, rhs):
82 return self.origin == rhs.origin and self.size == rhs.size
83
84 def __ne__(self, rhs):
85 return self.origin != rhs.origin or self.size != rhs.size
86
Greg Clayton5ea44832015-10-15 00:49:36 +000087class QuitException(Exception):
88 def __init__(self):
89 super(QuitException, self).__init__('QuitException')
90
Greg Clayton1827fc22015-09-19 00:39:09 +000091class Window(object):
Greg Clayton87349242015-09-22 00:35:20 +000092 def __init__(self, window, delegate = None, can_become_first_responder = True):
Greg Clayton1827fc22015-09-19 00:39:09 +000093 self.window = window
Greg Clayton87349242015-09-22 00:35:20 +000094 self.parent = None
95 self.delegate = delegate
96 self.children = list()
Greg Clayton37191a22015-10-07 20:00:28 +000097 self.first_responders = list()
Greg Clayton87349242015-09-22 00:35:20 +000098 self.can_become_first_responder = can_become_first_responder
Greg Clayton414dba52015-09-24 00:19:42 +000099 self.key_actions = dict()
Greg Clayton87349242015-09-22 00:35:20 +0000100
101 def add_child(self, window):
102 self.children.append(window)
103 window.parent = self
Greg Claytonc12cc592015-10-16 00:34:18 +0000104
105 def resize(self, size):
106 self.window.resize(size.h, size.w)
Greg Clayton414dba52015-09-24 00:19:42 +0000107
Greg Claytonc12cc592015-10-16 00:34:18 +0000108 def resize_child(self, child, delta_size, adjust_neighbors):
109 if child in self.children:
110 frame = self.get_frame()
111 orig_frame = child.get_frame()
112 new_frame = Rect(x=orig_frame.origin.x, y=orig_frame.origin.y, w=orig_frame.size.w + delta_size.w, h=orig_frame.size.h + delta_size.h)
113 old_child_max_x = orig_frame.get_max_x()
114 new_child_max_x = new_frame.get_max_x()
115 window_max_x = frame.get_max_x()
116 if new_child_max_x < window_max_x:
117 child.resize(new_frame.size)
118 if old_child_max_x == window_max_x:
119 new_frame.origin.x += window_max_x - new_child_max_x
120 child.set_position(new_frame.origin)
121 elif new_child_max_x > window_max_x:
122 new_frame.origin.x -= new_child_max_x - window_max_x
123 child.set_position(new_frame.origin)
124 child.resize(new_frame.size)
125
126 if adjust_neighbors:
Zachary Turner35d017f2015-10-23 17:04:29 +0000127 #print('orig_frame = %s\r\n' % (str(orig_frame)), end='')
Greg Claytonc12cc592015-10-16 00:34:18 +0000128 for curr_child in self.children:
129 if curr_child is child:
130 continue
131 curr_child_frame = curr_child.get_frame()
132 if delta_size.w != 0:
Zachary Turner35d017f2015-10-23 17:04:29 +0000133 #print('curr_child_frame = %s\r\n' % (str(curr_child_frame)), end='')
Greg Claytonc12cc592015-10-16 00:34:18 +0000134 if curr_child_frame.get_min_x() == orig_frame.get_max_x():
135 curr_child_frame.origin.x += delta_size.w
136 curr_child_frame.size.w -= delta_size.w
Zachary Turner35d017f2015-10-23 17:04:29 +0000137 #print('adjusted curr_child_frame = %s\r\n' % (str(curr_child_frame)), end='')
Greg Claytonc12cc592015-10-16 00:34:18 +0000138 curr_child.resize (curr_child_frame.size)
139 curr_child.slide_position (Size(w=delta_size.w, h=0))
140 elif curr_child_frame.get_max_x() == orig_frame.get_min_x():
141 curr_child_frame.size.w -= delta_size.w
Zachary Turner35d017f2015-10-23 17:04:29 +0000142 #print('adjusted curr_child_frame = %s\r\n' % (str(curr_child_frame)), end='')
Greg Claytonc12cc592015-10-16 00:34:18 +0000143 curr_child.resize (curr_child_frame.size)
144
Greg Clayton414dba52015-09-24 00:19:42 +0000145 def add_key_action(self, arg, callback, decription):
146 if isinstance(arg, list):
147 for key in arg:
148 self.add_key_action(key, callback, description)
149 else:
Zachary Turnerf67f7e32015-10-26 16:51:09 +0000150 if isinstance(arg, six.integer_types):
Greg Clayton414dba52015-09-24 00:19:42 +0000151 key_action_dict = { 'key' : arg,
152 'callback' : callback,
153 'description' : decription }
154 self.key_actions[arg] = key_action_dict
155 elif isinstance(arg, basestring):
156 key_integer = ord(arg)
157 key_action_dict = { 'key' : key_integer,
158 'callback' : callback,
159 'description' : decription }
160 self.key_actions[key_integer] = key_action_dict
161 else:
162 raise ValueError
Greg Clayton72d51442015-10-13 23:16:29 +0000163
164 def draw_title_box(self, title):
165 is_in_first_responder_chain = self.is_in_first_responder_chain()
166 if is_in_first_responder_chain:
167 self.attron (curses.A_REVERSE)
168 self.box()
169 if is_in_first_responder_chain:
170 self.attroff (curses.A_REVERSE)
171 if title:
172 self.addstr(Point(x=2, y=0), ' ' + title + ' ')
173
Greg Clayton87349242015-09-22 00:35:20 +0000174 def remove_child(self, window):
175 self.children.remove(window)
Greg Clayton37191a22015-10-07 20:00:28 +0000176
177 def get_first_responder(self):
178 if len(self.first_responders):
179 return self.first_responders[-1]
180 else:
181 return None
182
Greg Clayton87349242015-09-22 00:35:20 +0000183 def set_first_responder(self, window):
184 if window.can_become_first_responder:
Zachary Turnercd236b82015-10-26 18:48:24 +0000185 if six.callable(getattr(window, "hidden", None)) and window.hidden():
Greg Clayton87349242015-09-22 00:35:20 +0000186 return False
187 if not window in self.children:
188 self.add_child(window)
Greg Clayton37191a22015-10-07 20:00:28 +0000189 # See if we have a current first responder, and if we do, let it know that
190 # it will be resigning as first responder
191 first_responder = self.get_first_responder()
192 if first_responder:
193 first_responder.relinquish_first_responder()
194 # Now set the first responder to "window"
195 if len(self.first_responders) == 0:
196 self.first_responders.append(window)
197 else:
198 self.first_responders[-1] = window
Greg Clayton87349242015-09-22 00:35:20 +0000199 return True
200 else:
201 return False
202
Greg Clayton37191a22015-10-07 20:00:28 +0000203 def push_first_responder(self, window):
204 # Only push the window as the new first responder if the window isn't already the first responder
205 if window != self.get_first_responder():
206 self.first_responders.append(window)
207
208 def pop_first_responder(self, window):
209 # Only pop the window from the first responder list if it is the first responder
210 if window == self.get_first_responder():
211 old_first_responder = self.first_responders.pop()
212 old_first_responder.relinquish_first_responder()
213 return True
214 else:
215 return False
216
217 def relinquish_first_responder(self):
218 '''Override if there is something that you need to do when you lose first responder status.'''
219 pass
220
221 # def resign_first_responder(self, remove_from_parent, new_first_responder):
222 # success = False
223 # if self.parent:
224 # if self.is_first_responder():
225 # self.relinquish_first_responder()
226 # if len(self.parent.first_responder):
227 # self.parent.first_responder = None
228 # success = True
229 # if remove_from_parent:
230 # self.parent.remove_child(self)
231 # if new_first_responder:
232 # self.parent.set_first_responder(new_first_responder)
233 # else:
234 # self.parent.select_next_first_responder()
235 # return success
Greg Clayton1827fc22015-09-19 00:39:09 +0000236
Greg Clayton87349242015-09-22 00:35:20 +0000237 def is_first_responder(self):
238 if self.parent:
Greg Clayton37191a22015-10-07 20:00:28 +0000239 return self.parent.get_first_responder() == self
240 else:
241 return False
242
243 def is_in_first_responder_chain(self):
244 if self.parent:
245 return self in self.parent.first_responders
Greg Clayton87349242015-09-22 00:35:20 +0000246 else:
247 return False
248
249 def select_next_first_responder(self):
Greg Clayton37191a22015-10-07 20:00:28 +0000250 if len(self.first_responders) > 1:
251 self.pop_first_responder(self.first_responders[-1])
252 else:
253 num_children = len(self.children)
254 if num_children == 1:
255 return self.set_first_responder(self.children[0])
256 for (i,window) in enumerate(self.children):
257 if window.is_first_responder():
258 break
259 if i < num_children:
260 for i in range(i+1,num_children):
261 if self.set_first_responder(self.children[i]):
262 return True
263 for i in range(0, i):
264 if self.set_first_responder(self.children[i]):
265 return True
Greg Clayton87349242015-09-22 00:35:20 +0000266
Greg Clayton1827fc22015-09-19 00:39:09 +0000267 def point_in_window(self, pt):
268 size = self.get_size()
269 return pt.x >= 0 and pt.x < size.w and pt.y >= 0 and pt.y < size.h
Greg Clayton37191a22015-10-07 20:00:28 +0000270
Greg Clayton72d51442015-10-13 23:16:29 +0000271 def addch(self, c):
272 try:
273 self.window.addch(c)
274 except:
275 pass
276
277 def addch_at_point(self, pt, c):
Greg Clayton37191a22015-10-07 20:00:28 +0000278 try:
279 self.window.addch(pt.y, pt.x, c)
280 except:
281 pass
Greg Clayton1827fc22015-09-19 00:39:09 +0000282
283 def addstr(self, pt, str):
284 try:
285 self.window.addstr(pt.y, pt.x, str)
286 except:
287 pass
288
Greg Clayton72d51442015-10-13 23:16:29 +0000289 def addnstr_at_point(self, pt, str, n):
Greg Clayton1827fc22015-09-19 00:39:09 +0000290 try:
291 self.window.addnstr(pt.y, pt.x, str, n)
292 except:
293 pass
Greg Clayton72d51442015-10-13 23:16:29 +0000294 def addnstr(self, str, n):
295 try:
296 self.window.addnstr(str, n)
297 except:
298 pass
Greg Clayton1827fc22015-09-19 00:39:09 +0000299
Greg Clayton87349242015-09-22 00:35:20 +0000300 def attron(self, attr):
301 return self.window.attron (attr)
302
303 def attroff(self, attr):
304 return self.window.attroff (attr)
305
306 def box(self, vertch=0, horch=0):
307 if vertch == 0:
308 vertch = curses.ACS_VLINE
309 if horch == 0:
310 horch = curses.ACS_HLINE
311 self.window.box(vertch, horch)
Greg Clayton1827fc22015-09-19 00:39:09 +0000312
313 def get_contained_rect(self, top_inset=0, bottom_inset=0, left_inset=0, right_inset=0, height=-1, width=-1):
314 '''Get a rectangle based on the top "height" lines of this window'''
315 rect = self.get_frame()
316 x = rect.origin.x + left_inset
317 y = rect.origin.y + top_inset
318 if height == -1:
319 h = rect.size.h - (top_inset + bottom_inset)
320 else:
321 h = height
322 if width == -1:
323 w = rect.size.w - (left_inset + right_inset)
324 else:
325 w = width
326 return Rect (x = x, y = y, w = w, h = h)
327
328 def erase(self):
329 self.window.erase()
Greg Clayton72d51442015-10-13 23:16:29 +0000330
331 def get_cursor(self):
332 (y, x) = self.window.getyx()
333 return Point(x=x, y=y)
Greg Clayton1827fc22015-09-19 00:39:09 +0000334
335 def get_frame(self):
336 position = self.get_position()
337 size = self.get_size()
338 return Rect(x=position.x, y=position.y, w=size.w, h=size.h)
339
Greg Claytonc12cc592015-10-16 00:34:18 +0000340 def get_frame_in_parent(self):
341 position = self.get_position_in_parent()
342 size = self.get_size()
343 return Rect(x=position.x, y=position.y, w=size.w, h=size.h)
344
345 def get_position_in_parent(self):
346 (y, x) = self.window.getparyx()
347 return Point(x, y)
348
Greg Clayton1827fc22015-09-19 00:39:09 +0000349 def get_position(self):
350 (y, x) = self.window.getbegyx()
351 return Point(x, y)
352
353 def get_size(self):
354 (y, x) = self.window.getmaxyx()
355 return Size(w=x, h=y)
Greg Clayton37191a22015-10-07 20:00:28 +0000356
Greg Clayton72d51442015-10-13 23:16:29 +0000357 def move(self, pt):
358 self.window.move(pt.y, pt.x)
359
Greg Clayton1827fc22015-09-19 00:39:09 +0000360 def refresh(self):
Greg Clayton87349242015-09-22 00:35:20 +0000361 self.update()
Greg Clayton1827fc22015-09-19 00:39:09 +0000362 curses.panel.update_panels()
Greg Clayton72d51442015-10-13 23:16:29 +0000363 self.move(Point(x=0, y=0))
Greg Clayton1827fc22015-09-19 00:39:09 +0000364 return self.window.refresh()
365
366 def resize(self, size):
Greg Clayton87349242015-09-22 00:35:20 +0000367 return self.window.resize(size.h, size.w)
Greg Clayton1827fc22015-09-19 00:39:09 +0000368
Greg Clayton87349242015-09-22 00:35:20 +0000369 def timeout(self, timeout_msec):
370 return self.window.timeout(timeout_msec)
371
372 def handle_key(self, key, check_parent=True):
373 '''Handle a key press in this window.'''
374
375 # First try the first responder if this window has one, but don't allow
376 # it to check with its parent (False second parameter) so we don't recurse
377 # and get a stack overflow
Greg Clayton37191a22015-10-07 20:00:28 +0000378 for first_responder in reversed(self.first_responders):
379 if first_responder.handle_key(key, False):
Greg Clayton87349242015-09-22 00:35:20 +0000380 return True
Greg Clayton414dba52015-09-24 00:19:42 +0000381
382 # Check our key map to see if we have any actions. Actions don't take
383 # any arguments, they must be callable
384 if key in self.key_actions:
385 key_action = self.key_actions[key]
386 key_action['callback']()
387 return True
388 # Check if there is a wildcard key for any key
389 if -1 in self.key_actions:
390 key_action = self.key_actions[-1]
391 key_action['callback']()
392 return True
Greg Clayton87349242015-09-22 00:35:20 +0000393 # Check if the window delegate wants to handle this key press
394 if self.delegate:
Zachary Turnercd236b82015-10-26 18:48:24 +0000395 if six.callable(getattr(self.delegate, "handle_key", None)):
Greg Clayton87349242015-09-22 00:35:20 +0000396 if self.delegate.handle_key(self, key):
397 return True
398 if self.delegate(self, key):
399 return True
400 # Check if we have a parent window and if so, let the parent
401 # window handle the key press
402 if check_parent and self.parent:
403 return self.parent.handle_key(key, True)
404 else:
405 return False # Key not handled
406
407 def update(self):
408 for child in self.children:
409 child.update()
Greg Clayton5ea44832015-10-15 00:49:36 +0000410
411 def quit_action(self):
412 raise QuitException
Greg Clayton87349242015-09-22 00:35:20 +0000413
Greg Clayton3fd1f742015-10-16 23:34:40 +0000414 def get_key(self, timeout_msec=-1):
415 self.timeout(timeout_msec)
416 done = False
417 c = self.window.getch()
418 if c == 27:
419 self.timeout(0)
420 escape_key = 0
421 while True:
422 escape_key = self.window.getch()
423 if escape_key == -1:
424 break
425 else:
426 c = c << 8 | escape_key
427 self.timeout(timeout_msec)
428 return c
429
Zachary Turnerda3dea62015-10-26 16:51:20 +0000430 def key_event_loop(self, timeout_msec=-1, n=sys.maxsize):
Greg Clayton87349242015-09-22 00:35:20 +0000431 '''Run an event loop to receive key presses and pass them along to the
432 responder chain.
433
434 timeout_msec is the timeout it milliseconds. If the value is -1, an
435 infinite wait will be used. It the value is zero, a non-blocking mode
436 will be used, and if greater than zero it will wait for a key press
437 for timeout_msec milliseconds.
438
439 n is the number of times to go through the event loop before exiting'''
Greg Clayton5ea44832015-10-15 00:49:36 +0000440 done = False
441 while not done and n > 0:
Greg Clayton258c1642015-10-21 21:55:16 +0000442 c = self.get_key(timeout_msec)
Greg Clayton87349242015-09-22 00:35:20 +0000443 if c != -1:
Greg Clayton37191a22015-10-07 20:00:28 +0000444 try:
445 self.handle_key(c)
Greg Clayton5ea44832015-10-15 00:49:36 +0000446 except QuitException:
447 done = True
Greg Clayton87349242015-09-22 00:35:20 +0000448 n -= 1
449
Greg Clayton1827fc22015-09-19 00:39:09 +0000450class Panel(Window):
Greg Clayton87349242015-09-22 00:35:20 +0000451 def __init__(self, frame, delegate = None, can_become_first_responder = True):
Greg Clayton1827fc22015-09-19 00:39:09 +0000452 window = curses.newwin(frame.size.h,frame.size.w, frame.origin.y, frame.origin.x)
Greg Clayton87349242015-09-22 00:35:20 +0000453 super(Panel, self).__init__(window, delegate, can_become_first_responder)
Greg Clayton1827fc22015-09-19 00:39:09 +0000454 self.panel = curses.panel.new_panel(window)
455
Greg Clayton87349242015-09-22 00:35:20 +0000456 def hide(self):
457 return self.panel.hide()
458
459 def hidden(self):
460 return self.panel.hidden()
461
462 def show(self):
463 return self.panel.show()
464
Greg Clayton1827fc22015-09-19 00:39:09 +0000465 def top(self):
Greg Clayton87349242015-09-22 00:35:20 +0000466 return self.panel.top()
Greg Clayton1827fc22015-09-19 00:39:09 +0000467
468 def set_position(self, pt):
469 self.panel.move(pt.y, pt.x)
470
Greg Claytonc12cc592015-10-16 00:34:18 +0000471 def slide_position(self, size):
Greg Clayton1827fc22015-09-19 00:39:09 +0000472 new_position = self.get_position()
Greg Claytonc12cc592015-10-16 00:34:18 +0000473 new_position.x = new_position.x + size.w
474 new_position.y = new_position.y + size.h
Greg Clayton1827fc22015-09-19 00:39:09 +0000475 self.set_position(new_position)
476
477class BoxedPanel(Panel):
Greg Clayton87349242015-09-22 00:35:20 +0000478 def __init__(self, frame, title, delegate = None, can_become_first_responder = True):
479 super(BoxedPanel, self).__init__(frame, delegate, can_become_first_responder)
Greg Clayton1827fc22015-09-19 00:39:09 +0000480 self.title = title
481 self.lines = list()
482 self.first_visible_idx = 0
Greg Clayton87349242015-09-22 00:35:20 +0000483 self.selected_idx = -1
Greg Clayton37191a22015-10-07 20:00:28 +0000484 self.add_key_action(curses.KEY_UP, self.select_prev, "Select the previous item")
485 self.add_key_action(curses.KEY_DOWN, self.select_next, "Select the next item")
486 self.add_key_action(curses.KEY_HOME, self.scroll_begin, "Go to the beginning of the list")
487 self.add_key_action(curses.KEY_END, self.scroll_end, "Go to the end of the list")
Greg Claytonc12cc592015-10-16 00:34:18 +0000488 self.add_key_action(0x1b4f48, self.scroll_begin, "Go to the beginning of the list")
489 self.add_key_action(0x1b4f46, self.scroll_end, "Go to the end of the list")
Greg Clayton37191a22015-10-07 20:00:28 +0000490 self.add_key_action(curses.KEY_PPAGE, self.scroll_page_backward, "Scroll to previous page")
491 self.add_key_action(curses.KEY_NPAGE, self.scroll_page_forward, "Scroll to next forward")
Greg Clayton1827fc22015-09-19 00:39:09 +0000492 self.update()
493
Greg Claytond13c4fb2015-09-22 17:18:15 +0000494 def clear(self, update=True):
495 self.lines = list()
496 self.first_visible_idx = 0
497 self.selected_idx = -1
498 if update:
499 self.update()
500
Greg Clayton1827fc22015-09-19 00:39:09 +0000501 def get_usable_width(self):
502 '''Valid usable width is 0 to (width - 3) since the left and right lines display the box around
503 this frame and we skip a leading space'''
504 w = self.get_size().w
505 if w > 3:
506 return w-3
507 else:
508 return 0
509
510 def get_usable_height(self):
511 '''Valid line indexes are 0 to (height - 2) since the top and bottom lines display the box around this frame.'''
512 h = self.get_size().h
513 if h > 2:
514 return h-2
515 else:
516 return 0
517
518 def get_point_for_line(self, global_line_idx):
519 '''Returns the point to use when displaying a line whose index is "line_idx"'''
520 line_idx = global_line_idx - self.first_visible_idx
521 num_lines = self.get_usable_height()
522 if line_idx < num_lines:
523 return Point(x=2, y=1+line_idx)
524 else:
525 return Point(x=-1, y=-1) # return an invalid coordinate if the line index isn't valid
526
527 def set_title (self, title, update=True):
528 self.title = title
529 if update:
530 self.update()
531
Greg Claytonc12cc592015-10-16 00:34:18 +0000532 def scroll_to_line (self, idx):
533 if idx < len(self.lines):
534 self.selected_idx = idx
535 max_visible_lines = self.get_usable_height()
536 if idx < self.first_visible_idx or idx >= self.first_visible_idx + max_visible_lines:
537 self.first_visible_idx = idx
538 self.refresh()
539
Greg Clayton87349242015-09-22 00:35:20 +0000540 def scroll_begin (self):
541 self.first_visible_idx = 0
542 if len(self.lines) > 0:
543 self.selected_idx = 0
544 else:
545 self.selected_idx = -1
546 self.update()
547
548 def scroll_end (self):
549 max_visible_lines = self.get_usable_height()
550 num_lines = len(self.lines)
Greg Clayton414dba52015-09-24 00:19:42 +0000551 if num_lines > max_visible_lines:
Greg Clayton87349242015-09-22 00:35:20 +0000552 self.first_visible_idx = num_lines - max_visible_lines
553 else:
554 self.first_visible_idx = 0
555 self.selected_idx = num_lines-1
556 self.update()
Greg Clayton37191a22015-10-07 20:00:28 +0000557
558 def scroll_page_backward(self):
559 num_lines = len(self.lines)
560 max_visible_lines = self.get_usable_height()
561 new_index = self.first_visible_idx - max_visible_lines
562 if new_index < 0:
563 self.first_visible_idx = 0
564 else:
565 self.first_visible_idx = new_index
566 self.refresh()
567
568 def scroll_page_forward(self):
569 max_visible_lines = self.get_usable_height()
570 self.first_visible_idx += max_visible_lines
571 self._adjust_first_visible_line()
572 self.refresh()
573
Greg Clayton87349242015-09-22 00:35:20 +0000574 def select_next (self):
575 self.selected_idx += 1
576 if self.selected_idx >= len(self.lines):
577 self.selected_idx = len(self.lines) - 1
Greg Clayton37191a22015-10-07 20:00:28 +0000578 self.refresh()
Greg Clayton87349242015-09-22 00:35:20 +0000579
580 def select_prev (self):
581 self.selected_idx -= 1
582 if self.selected_idx < 0:
583 if len(self.lines) > 0:
584 self.selected_idx = 0
585 else:
586 self.selected_idx = -1
Greg Clayton37191a22015-10-07 20:00:28 +0000587 self.refresh()
Greg Clayton87349242015-09-22 00:35:20 +0000588
589 def get_selected_idx(self):
590 return self.selected_idx
591
Greg Clayton1827fc22015-09-19 00:39:09 +0000592 def _adjust_first_visible_line(self):
593 num_lines = len(self.lines)
594 max_visible_lines = self.get_usable_height()
Greg Clayton37191a22015-10-07 20:00:28 +0000595 if (self.first_visible_idx >= num_lines) or (num_lines - self.first_visible_idx) > max_visible_lines:
Greg Clayton1827fc22015-09-19 00:39:09 +0000596 self.first_visible_idx = num_lines - max_visible_lines
597
598 def append_line(self, s, update=True):
599 self.lines.append(s)
600 self._adjust_first_visible_line()
601 if update:
602 self.update()
603
604 def set_line(self, line_idx, s, update=True):
605 '''Sets a line "line_idx" within the boxed panel to be "s"'''
606 if line_idx < 0:
607 return
608 while line_idx >= len(self.lines):
609 self.lines.append('')
610 self.lines[line_idx] = s
611 self._adjust_first_visible_line()
612 if update:
613 self.update()
614
615 def update(self):
Greg Clayton72d51442015-10-13 23:16:29 +0000616 self.erase()
617 self.draw_title_box(self.title)
Greg Clayton1827fc22015-09-19 00:39:09 +0000618 max_width = self.get_usable_width()
619 for line_idx in range(self.first_visible_idx, len(self.lines)):
620 pt = self.get_point_for_line(line_idx)
621 if pt.is_valid_coordinate():
Greg Clayton87349242015-09-22 00:35:20 +0000622 is_selected = line_idx == self.selected_idx
623 if is_selected:
624 self.attron (curses.A_REVERSE)
Greg Claytonc12cc592015-10-16 00:34:18 +0000625 self.move(pt)
626 self.addnstr(self.lines[line_idx], max_width)
Greg Clayton87349242015-09-22 00:35:20 +0000627 if is_selected:
628 self.attroff (curses.A_REVERSE)
Greg Clayton1827fc22015-09-19 00:39:09 +0000629 else:
630 return
631
Greg Claytonc12cc592015-10-16 00:34:18 +0000632 def load_file(self, path):
633 f = open(path)
634 if f:
635 self.lines = f.read().splitlines()
636 for (idx, line) in enumerate(self.lines):
637 # Remove any tabs from lines since they hose up the display
638 if "\t" in line:
639 self.lines[idx] = (8*' ').join(line.split('\t'))
640 self.selected_idx = 0
641 self.first_visible_idx = 0
642 self.refresh()
643
Greg Clayton37191a22015-10-07 20:00:28 +0000644class Item(object):
645 def __init__(self, title, action):
646 self.title = title
647 self.action = action
Greg Clayton72d51442015-10-13 23:16:29 +0000648
Greg Clayton5ea44832015-10-15 00:49:36 +0000649class TreeItemDelegate(object):
650
651 def might_have_children(self):
652 return False
653
654 def update_children(self, item):
655 '''Return a list of child Item objects'''
656 return None
657
658 def draw_item_string(self, tree_window, item, s):
659 pt = tree_window.get_cursor()
660 width = tree_window.get_size().w - 1
661 if width > pt.x:
662 tree_window.addnstr(s, width - pt.x)
663
664 def draw_item(self, tree_window, item):
665 self.draw_item_string(tree_window, item, item.title)
Greg Claytonc12cc592015-10-16 00:34:18 +0000666
667 def do_action(self):
668 pass
Greg Clayton5ea44832015-10-15 00:49:36 +0000669
Greg Clayton72d51442015-10-13 23:16:29 +0000670class TreeItem(object):
671 def __init__(self, delegate, parent = None, title = None, action = None, is_expanded = False):
672 self.parent = parent
673 self.title = title
674 self.action = action
675 self.delegate = delegate
676 self.is_expanded = not parent or is_expanded == True
Greg Clayton5ea44832015-10-15 00:49:36 +0000677 self._might_have_children = None
Greg Clayton72d51442015-10-13 23:16:29 +0000678 self.children = None
Greg Clayton5ea44832015-10-15 00:49:36 +0000679 self._children_might_have_children = False
Greg Clayton72d51442015-10-13 23:16:29 +0000680
681 def get_children(self):
682 if self.is_expanded and self.might_have_children():
683 if self.children is None:
Greg Clayton5ea44832015-10-15 00:49:36 +0000684 self._children_might_have_children = False
Greg Clayton72d51442015-10-13 23:16:29 +0000685 self.children = self.update_children()
Greg Clayton5ea44832015-10-15 00:49:36 +0000686 for child in self.children:
687 if child.might_have_children():
688 self._children_might_have_children = True
689 break
Greg Clayton72d51442015-10-13 23:16:29 +0000690 else:
Greg Clayton5ea44832015-10-15 00:49:36 +0000691 self._children_might_have_children = False
Greg Clayton72d51442015-10-13 23:16:29 +0000692 self.children = None
693 return self.children
Greg Clayton5ea44832015-10-15 00:49:36 +0000694
Greg Clayton72d51442015-10-13 23:16:29 +0000695 def append_visible_items(self, items):
696 items.append(self)
697 children = self.get_children()
698 if children:
699 for child in children:
700 child.append_visible_items(items)
701
702 def might_have_children(self):
Greg Clayton5ea44832015-10-15 00:49:36 +0000703 if self._might_have_children is None:
Greg Clayton72d51442015-10-13 23:16:29 +0000704 if not self.parent:
705 # Root item always might have children
Greg Clayton5ea44832015-10-15 00:49:36 +0000706 self._might_have_children = True
Greg Clayton72d51442015-10-13 23:16:29 +0000707 else:
708 # Check with the delegate to see if the item might have children
Greg Clayton5ea44832015-10-15 00:49:36 +0000709 self._might_have_children = self.delegate.might_have_children()
710 return self._might_have_children
711
712 def children_might_have_children(self):
713 return self._children_might_have_children
714
Greg Clayton72d51442015-10-13 23:16:29 +0000715 def update_children(self):
716 if self.is_expanded and self.might_have_children():
717 self.children = self.delegate.update_children(self)
718 for child in self.children:
719 child.update_children()
720 else:
721 self.children = None
722 return self.children
Greg Clayton37191a22015-10-07 20:00:28 +0000723
Greg Clayton72d51442015-10-13 23:16:29 +0000724 def get_num_visible_rows(self):
725 rows = 1
726 if self.is_expanded:
727 children = self.get_children()
Greg Claytonc12cc592015-10-16 00:34:18 +0000728 if children:
729 for child in children:
730 rows += child.get_num_visible_rows()
Greg Clayton72d51442015-10-13 23:16:29 +0000731 return rows
732 def draw(self, tree_window, row):
733 display_row = tree_window.get_display_row(row)
734 if display_row >= 0:
735 tree_window.move(tree_window.get_item_draw_point(row))
736 if self.parent:
737 self.parent.draw_tree_for_child(tree_window, self, 0)
738 if self.might_have_children():
739 tree_window.addch (curses.ACS_DIAMOND)
740 tree_window.addch (curses.ACS_HLINE)
Greg Clayton5ea44832015-10-15 00:49:36 +0000741 elif self.parent and self.parent.children_might_have_children():
Greg Claytonc12cc592015-10-16 00:34:18 +0000742 if self.parent.parent:
743 tree_window.addch (curses.ACS_HLINE)
744 tree_window.addch (curses.ACS_HLINE)
745 else:
746 tree_window.addch (' ')
747 tree_window.addch (' ')
Greg Clayton72d51442015-10-13 23:16:29 +0000748 is_selected = tree_window.is_selected(row)
749 if is_selected:
750 tree_window.attron (curses.A_REVERSE)
751 self.delegate.draw_item(tree_window, self)
752 if is_selected:
753 tree_window.attroff (curses.A_REVERSE)
754
755 def draw_tree_for_child (self, tree_window, child, reverse_depth):
756 if self.parent:
757 self.parent.draw_tree_for_child (tree_window, self, reverse_depth + 1)
758 if self.children[-1] == child:
759 # Last child
760 if reverse_depth == 0:
761 tree_window.addch (curses.ACS_LLCORNER)
762 tree_window.addch (curses.ACS_HLINE)
763 else:
764 tree_window.addch (' ')
765 tree_window.addch (' ')
766 else:
767 # Middle child
768 if reverse_depth == 0:
769 tree_window.addch (curses.ACS_LTEE)
770 tree_window.addch (curses.ACS_HLINE)
771 else:
772 tree_window.addch (curses.ACS_VLINE)
773 tree_window.addch (' ')
774
Greg Claytonc12cc592015-10-16 00:34:18 +0000775 def was_selected(self):
776 self.delegate.do_action()
Greg Clayton72d51442015-10-13 23:16:29 +0000777
778class TreePanel(Panel):
779 def __init__(self, frame, title, root_item):
780 self.root_item = root_item
781 self.title = title
782 self.first_visible_idx = 0
783 self.selected_idx = 0
784 self.items = None
785 super(TreePanel, self).__init__(frame)
786 self.add_key_action(curses.KEY_UP, self.select_prev, "Select the previous item")
787 self.add_key_action(curses.KEY_DOWN, self.select_next, "Select the next item")
788 self.add_key_action(curses.KEY_RIGHT,self.right_arrow, "Expand an item")
789 self.add_key_action(curses.KEY_LEFT, self.left_arrow, "Unexpand an item or navigate to parent")
Greg Claytonc12cc592015-10-16 00:34:18 +0000790 self.add_key_action(curses.KEY_HOME, self.scroll_begin, "Go to the beginning of the tree")
791 self.add_key_action(curses.KEY_END, self.scroll_end, "Go to the end of the tree")
792 self.add_key_action(0x1b4f48, self.scroll_begin, "Go to the beginning of the tree")
793 self.add_key_action(0x1b4f46, self.scroll_end, "Go to the end of the tree")
Greg Clayton72d51442015-10-13 23:16:29 +0000794 self.add_key_action(curses.KEY_PPAGE, self.scroll_page_backward, "Scroll to previous page")
795 self.add_key_action(curses.KEY_NPAGE, self.scroll_page_forward, "Scroll to next forward")
796
797 def get_selected_item(self):
798 if self.selected_idx < len(self.items):
799 return self.items[self.selected_idx]
800 else:
801 return None
802
803 def select_item(self, item):
804 if self.items and item in self.items:
805 self.selected_idx = self.items.index(item)
806 return True
807 else:
808 return False
809
810 def get_visible_items(self):
811 # Clear self.items when you want to update all chidren
812 if self.items is None:
813 self.items = list()
814 children = self.root_item.get_children()
815 if children:
816 for child in children:
817 child.append_visible_items(self.items)
818 return self.items
819
820 def update(self):
821 self.erase()
822 self.draw_title_box(self.title)
823 visible_items = self.get_visible_items()
824 for (row, child) in enumerate(visible_items):
825 child.draw(self, row)
826
827 def get_item_draw_point(self, row):
828 display_row = self.get_display_row(row)
829 if display_row >= 0:
830 return Point(2, display_row + 1)
831 else:
832 return Point(-1, -1)
833
834 def get_display_row(self, row):
835 if row >= self.first_visible_idx:
836 display_row = row - self.first_visible_idx
837 if display_row < self.get_size().h-2:
838 return display_row
839 return -1
840
841 def is_selected(self, row):
842 return row == self.selected_idx
843
Greg Claytonc12cc592015-10-16 00:34:18 +0000844 def get_num_lines(self):
845 self.get_visible_items()
846 return len(self.items)
Greg Clayton72d51442015-10-13 23:16:29 +0000847
848 def get_num_visible_lines(self):
849 return self.get_size().h-2
850 def select_next (self):
851 self.selected_idx += 1
852 num_lines = self.get_num_lines()
853 if self.selected_idx >= num_lines:
Greg Claytonc12cc592015-10-16 00:34:18 +0000854 self.selected_idx = num_lines - 1
855 self._selection_changed()
Greg Clayton72d51442015-10-13 23:16:29 +0000856 self.refresh()
857
858 def select_prev (self):
859 self.selected_idx -= 1
860 if self.selected_idx < 0:
861 num_lines = self.get_num_lines()
862 if num_lines > 0:
863 self.selected_idx = 0
864 else:
865 self.selected_idx = -1
Greg Claytonc12cc592015-10-16 00:34:18 +0000866 self._selection_changed()
Greg Clayton72d51442015-10-13 23:16:29 +0000867 self.refresh()
868
869 def scroll_begin (self):
870 self.first_visible_idx = 0
871 num_lines = self.get_num_lines()
872 if num_lines > 0:
873 self.selected_idx = 0
874 else:
875 self.selected_idx = -1
Greg Claytonc12cc592015-10-16 00:34:18 +0000876 self.refresh()
Greg Clayton72d51442015-10-13 23:16:29 +0000877
878 def redisplay_tree(self):
879 self.items = None
880 self.refresh()
881
882 def right_arrow(self):
883 selected_item = self.get_selected_item()
884 if selected_item and selected_item.is_expanded == False:
885 selected_item.is_expanded = True
886 self.redisplay_tree()
887
888 def left_arrow(self):
889 selected_item = self.get_selected_item()
890 if selected_item:
891 if selected_item.is_expanded == True:
892 selected_item.is_expanded = False
893 self.redisplay_tree()
894 elif selected_item.parent:
895 if self.select_item(selected_item.parent):
896 self.refresh()
897
898
899 def scroll_end (self):
900 num_visible_lines = self.get_num_visible_lines()
Greg Claytonc12cc592015-10-16 00:34:18 +0000901 num_lines = self.get_num_lines()
Greg Clayton72d51442015-10-13 23:16:29 +0000902 if num_lines > num_visible_lines:
903 self.first_visible_idx = num_lines - num_visible_lines
904 else:
905 self.first_visible_idx = 0
906 self.selected_idx = num_lines-1
Greg Claytonc12cc592015-10-16 00:34:18 +0000907 self.refresh()
Greg Clayton72d51442015-10-13 23:16:29 +0000908
909 def scroll_page_backward(self):
910 num_visible_lines = self.get_num_visible_lines()
Greg Claytonc12cc592015-10-16 00:34:18 +0000911 new_index = self.selected_idx - num_visible_lines
Greg Clayton72d51442015-10-13 23:16:29 +0000912 if new_index < 0:
Greg Claytonc12cc592015-10-16 00:34:18 +0000913 self.selected_idx = 0
Greg Clayton72d51442015-10-13 23:16:29 +0000914 else:
Greg Claytonc12cc592015-10-16 00:34:18 +0000915 self.selected_idx = new_index
916 self._selection_changed()
Greg Clayton72d51442015-10-13 23:16:29 +0000917 self.refresh()
918
919 def scroll_page_forward(self):
Greg Claytonc12cc592015-10-16 00:34:18 +0000920 num_lines = self.get_num_lines()
Greg Clayton72d51442015-10-13 23:16:29 +0000921 num_visible_lines = self.get_num_visible_lines()
Greg Claytonc12cc592015-10-16 00:34:18 +0000922 new_index = self.selected_idx + num_visible_lines
923 if new_index >= num_lines:
924 new_index = num_lines - 1
925 self.selected_idx = new_index
926 self._selection_changed()
Greg Clayton72d51442015-10-13 23:16:29 +0000927 self.refresh()
928
Greg Claytonc12cc592015-10-16 00:34:18 +0000929 def _selection_changed(self):
930 num_lines = self.get_num_lines()
Greg Clayton72d51442015-10-13 23:16:29 +0000931 num_visible_lines = self.get_num_visible_lines()
Greg Claytonc12cc592015-10-16 00:34:18 +0000932 last_visible_index = self.first_visible_idx + num_visible_lines
933 if self.selected_idx >= last_visible_index:
934 self.first_visible_idx += (self.selected_idx - last_visible_index + 1)
935 if self.selected_idx < self.first_visible_idx:
936 self.first_visible_idx = self.selected_idx
937 if self.selected_idx >= 0 and self.selected_idx < len(self.items):
938 item = self.items[self.selected_idx]
939 item.was_selected()
Greg Clayton72d51442015-10-13 23:16:29 +0000940
941
Greg Clayton37191a22015-10-07 20:00:28 +0000942class Menu(BoxedPanel):
943 def __init__(self, title, items):
944 max_title_width = 0
945 for item in items:
946 if max_title_width < len(item.title):
947 max_title_width = len(item.title)
948 frame = Rect(x=0, y=0, w=max_title_width+4, h=len(items)+2)
949 super(Menu, self).__init__(frame, title=None, delegate=None, can_become_first_responder=True)
950 self.selected_idx = 0
951 self.title = title
952 self.items = items
953 for (item_idx, item) in enumerate(items):
954 self.set_line(item_idx, item.title)
955 self.hide()
956
957 def update(self):
958 super(Menu, self).update()
959
960 def relinquish_first_responder(self):
961 if not self.hidden():
962 self.hide()
963
964 def perform_action(self):
965 selected_idx = self.get_selected_idx()
966 if selected_idx < len(self.items):
967 action = self.items[selected_idx].action
968 if action:
969 action()
970
971class MenuBar(Panel):
972 def __init__(self, frame):
973 super(MenuBar, self).__init__(frame, can_become_first_responder=True)
974 self.menus = list()
975 self.selected_menu_idx = -1
976 self.add_key_action(curses.KEY_LEFT, self.select_prev, "Select the previous menu")
977 self.add_key_action(curses.KEY_RIGHT, self.select_next, "Select the next menu")
978 self.add_key_action(curses.KEY_DOWN, lambda: self.select(0), "Select the first menu")
979 self.add_key_action(27, self.relinquish_first_responder, "Hide current menu")
980 self.add_key_action(curses.KEY_ENTER, self.perform_action, "Select the next menu item")
981 self.add_key_action(10, self.perform_action, "Select the next menu item")
982
Zachary Turnerda3dea62015-10-26 16:51:20 +0000983 def insert_menu(self, menu, index=sys.maxsize):
Greg Clayton37191a22015-10-07 20:00:28 +0000984 if index >= len(self.menus):
985 self.menus.append(menu)
986 else:
987 self.menus.insert(index, menu)
988 pt = self.get_position()
989 for menu in self.menus:
990 menu.set_position(pt)
991 pt.x += len(menu.title) + 5
992
993 def perform_action(self):
994 '''If no menu is visible, show the first menu. If a menu is visible, perform the action
995 associated with the selected menu item in the menu'''
996 menu_visible = False
997 for menu in self.menus:
998 if not menu.hidden():
999 menu_visible = True
1000 break
1001 if menu_visible:
1002 menu.perform_action()
1003 self.selected_menu_idx = -1
1004 self._selected_menu_changed()
1005 else:
1006 self.select(0)
1007
1008 def relinquish_first_responder(self):
1009 if self.selected_menu_idx >= 0:
1010 self.selected_menu_idx = -1
1011 self._selected_menu_changed()
1012
1013 def _selected_menu_changed(self):
1014 for (menu_idx, menu) in enumerate(self.menus):
1015 is_hidden = menu.hidden()
1016 if menu_idx != self.selected_menu_idx:
1017 if not is_hidden:
1018 if self.parent.pop_first_responder(menu) == False:
1019 menu.hide()
1020 for (menu_idx, menu) in enumerate(self.menus):
1021 is_hidden = menu.hidden()
1022 if menu_idx == self.selected_menu_idx:
1023 if is_hidden:
1024 menu.show()
1025 self.parent.push_first_responder(menu)
1026 menu.top()
1027 self.parent.refresh()
1028
1029 def select(self, index):
1030 if index < len(self.menus):
1031 self.selected_menu_idx = index
1032 self._selected_menu_changed()
1033
1034 def select_next (self):
1035 num_menus = len(self.menus)
1036 if self.selected_menu_idx == -1:
1037 if num_menus > 0:
1038 self.selected_menu_idx = 0
1039 self._selected_menu_changed()
1040 else:
1041 if self.selected_menu_idx + 1 < num_menus:
1042 self.selected_menu_idx += 1
1043 else:
1044 self.selected_menu_idx = -1
1045 self._selected_menu_changed()
1046
1047 def select_prev (self):
1048 num_menus = len(self.menus)
1049 if self.selected_menu_idx == -1:
1050 if num_menus > 0:
1051 self.selected_menu_idx = num_menus - 1
1052 self._selected_menu_changed()
1053 else:
1054 if self.selected_menu_idx - 1 >= 0:
1055 self.selected_menu_idx -= 1
1056 else:
1057 self.selected_menu_idx = -1
1058 self._selected_menu_changed()
1059
1060 def update(self):
1061 self.erase()
1062 is_in_first_responder_chain = self.is_in_first_responder_chain()
1063 if is_in_first_responder_chain:
1064 self.attron (curses.A_REVERSE)
1065 pt = Point(x=0, y=0)
1066 for menu in self.menus:
1067 self.addstr(pt, '| ' + menu.title + ' ')
1068 pt.x += len(menu.title) + 5
1069 self.addstr(pt, '|')
1070 width = self.get_size().w
1071 while pt.x < width:
Greg Clayton72d51442015-10-13 23:16:29 +00001072 self.addch_at_point(pt, ' ')
Greg Clayton37191a22015-10-07 20:00:28 +00001073 pt.x += 1
1074 if is_in_first_responder_chain:
1075 self.attroff (curses.A_REVERSE)
1076
1077 for menu in self.menus:
1078 menu.update()
1079
1080
Greg Clayton1827fc22015-09-19 00:39:09 +00001081class StatusPanel(Panel):
1082 def __init__(self, frame):
Greg Clayton87349242015-09-22 00:35:20 +00001083 super(StatusPanel, self).__init__(frame, delegate=None, can_become_first_responder=False)
Greg Clayton1827fc22015-09-19 00:39:09 +00001084 self.status_items = list()
1085 self.status_dicts = dict()
1086 self.next_status_x = 1
1087
1088 def add_status_item(self, name, title, format, width, value, update=True):
1089 status_item_dict = { 'name': name,
1090 'title' : title,
1091 'width' : width,
1092 'format' : format,
1093 'value' : value,
1094 'x' : self.next_status_x }
1095 index = len(self.status_items)
1096 self.status_items.append(status_item_dict)
1097 self.status_dicts[name] = index
1098 self.next_status_x += width + 2;
1099 if update:
1100 self.update()
1101
1102 def increment_status(self, name, update=True):
1103 if name in self.status_dicts:
1104 status_item_idx = self.status_dicts[name]
1105 status_item_dict = self.status_items[status_item_idx]
1106 status_item_dict['value'] = status_item_dict['value'] + 1
1107 if update:
1108 self.update()
1109
1110 def update_status(self, name, value, update=True):
1111 if name in self.status_dicts:
1112 status_item_idx = self.status_dicts[name]
1113 status_item_dict = self.status_items[status_item_idx]
1114 status_item_dict['value'] = status_item_dict['format'] % (value)
1115 if update:
1116 self.update()
1117 def update(self):
1118 self.erase();
1119 for status_item_dict in self.status_items:
Greg Clayton72d51442015-10-13 23:16:29 +00001120 self.addnstr_at_point(Point(x=status_item_dict['x'], y=0), '%s: %s' % (status_item_dict['title'], status_item_dict['value']), status_item_dict['width'])
Greg Clayton1827fc22015-09-19 00:39:09 +00001121
1122stdscr = None
1123
1124def intialize_curses():
1125 global stdscr
1126 stdscr = curses.initscr()
1127 curses.noecho()
1128 curses.cbreak()
1129 stdscr.keypad(1)
1130 try:
1131 curses.start_color()
1132 except:
1133 pass
1134 return Window(stdscr)
1135
1136def terminate_curses():
1137 global stdscr
1138 if stdscr:
1139 stdscr.keypad(0)
1140 curses.echo()
1141 curses.nocbreak()
1142 curses.endwin()
1143