blob: 81ad92a367caaf2c243eb2388c8f481d517afbd3 [file] [log] [blame]
Zachary Turnerf67f7e32015-10-26 16:51:09 +00001import lldb_shared
2
Greg Clayton1827fc22015-09-19 00:39:09 +00003import curses, curses.panel
Greg Clayton87349242015-09-22 00:35:20 +00004import sys
Zachary Turnerf67f7e32015-10-26 16:51:09 +00005import six
Greg Clayton87349242015-09-22 00:35:20 +00006import time
Greg Clayton1827fc22015-09-19 00:39:09 +00007
8class Point(object):
9 def __init__(self, x, y):
10 self.x = x
11 self.y = y
12
13 def __repr__(self):
14 return str(self)
15
16 def __str__(self):
17 return "(x=%u, y=%u)" % (self.x, self.y)
Greg Claytonc12cc592015-10-16 00:34:18 +000018
19 def __eq__(self, rhs):
20 return self.x == rhs.x and self.y == rhs.y
21
22 def __ne__(self, rhs):
23 return self.x != rhs.x or self.y != rhs.y
Greg Clayton1827fc22015-09-19 00:39:09 +000024
25 def is_valid_coordinate(self):
26 return self.x >= 0 and self.y >= 0
27
28class Size(object):
29 def __init__(self, w, h):
30 self.w = w
31 self.h = h
32
33 def __repr__(self):
34 return str(self)
35
36 def __str__(self):
37 return "(w=%u, h=%u)" % (self.w, self.h)
38
Greg Claytonc12cc592015-10-16 00:34:18 +000039 def __eq__(self, rhs):
40 return self.w == rhs.w and self.h == rhs.h
41
42 def __ne__(self, rhs):
43 return self.w != rhs.w or self.h != rhs.h
44
Greg Clayton1827fc22015-09-19 00:39:09 +000045class Rect(object):
46 def __init__(self, x=0, y=0, w=0, h=0):
47 self.origin = Point(x, y)
48 self.size = Size(w, h)
49
50 def __repr__(self):
51 return str(self)
52
53 def __str__(self):
54 return "{ %s, %s }" % (str(self.origin), str(self.size))
55
56 def get_min_x(self):
57 return self.origin.x
58
59 def get_max_x(self):
60 return self.origin.x + self.size.w
61
62 def get_min_y(self):
63 return self.origin.y
64
65 def get_max_y(self):
66 return self.origin.y + self.size.h
67
68 def contains_point(self, pt):
69 if pt.x < self.get_max_x():
70 if pt.y < self.get_max_y():
71 if pt.x >= self.get_min_y():
72 return pt.y >= self.get_min_y()
73 return False
74
Greg Claytonc12cc592015-10-16 00:34:18 +000075 def __eq__(self, rhs):
76 return self.origin == rhs.origin and self.size == rhs.size
77
78 def __ne__(self, rhs):
79 return self.origin != rhs.origin or self.size != rhs.size
80
Greg Clayton5ea44832015-10-15 00:49:36 +000081class QuitException(Exception):
82 def __init__(self):
83 super(QuitException, self).__init__('QuitException')
84
Greg Clayton1827fc22015-09-19 00:39:09 +000085class Window(object):
Greg Clayton87349242015-09-22 00:35:20 +000086 def __init__(self, window, delegate = None, can_become_first_responder = True):
Greg Clayton1827fc22015-09-19 00:39:09 +000087 self.window = window
Greg Clayton87349242015-09-22 00:35:20 +000088 self.parent = None
89 self.delegate = delegate
90 self.children = list()
Greg Clayton37191a22015-10-07 20:00:28 +000091 self.first_responders = list()
Greg Clayton87349242015-09-22 00:35:20 +000092 self.can_become_first_responder = can_become_first_responder
Greg Clayton414dba52015-09-24 00:19:42 +000093 self.key_actions = dict()
Greg Clayton87349242015-09-22 00:35:20 +000094
95 def add_child(self, window):
96 self.children.append(window)
97 window.parent = self
Greg Claytonc12cc592015-10-16 00:34:18 +000098
99 def resize(self, size):
100 self.window.resize(size.h, size.w)
Greg Clayton414dba52015-09-24 00:19:42 +0000101
Greg Claytonc12cc592015-10-16 00:34:18 +0000102 def resize_child(self, child, delta_size, adjust_neighbors):
103 if child in self.children:
104 frame = self.get_frame()
105 orig_frame = child.get_frame()
106 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)
107 old_child_max_x = orig_frame.get_max_x()
108 new_child_max_x = new_frame.get_max_x()
109 window_max_x = frame.get_max_x()
110 if new_child_max_x < window_max_x:
111 child.resize(new_frame.size)
112 if old_child_max_x == window_max_x:
113 new_frame.origin.x += window_max_x - new_child_max_x
114 child.set_position(new_frame.origin)
115 elif new_child_max_x > window_max_x:
116 new_frame.origin.x -= new_child_max_x - window_max_x
117 child.set_position(new_frame.origin)
118 child.resize(new_frame.size)
119
120 if adjust_neighbors:
Zachary Turner35d017f2015-10-23 17:04:29 +0000121 #print('orig_frame = %s\r\n' % (str(orig_frame)), end='')
Greg Claytonc12cc592015-10-16 00:34:18 +0000122 for curr_child in self.children:
123 if curr_child is child:
124 continue
125 curr_child_frame = curr_child.get_frame()
126 if delta_size.w != 0:
Zachary Turner35d017f2015-10-23 17:04:29 +0000127 #print('curr_child_frame = %s\r\n' % (str(curr_child_frame)), end='')
Greg Claytonc12cc592015-10-16 00:34:18 +0000128 if curr_child_frame.get_min_x() == orig_frame.get_max_x():
129 curr_child_frame.origin.x += delta_size.w
130 curr_child_frame.size.w -= delta_size.w
Zachary Turner35d017f2015-10-23 17:04:29 +0000131 #print('adjusted curr_child_frame = %s\r\n' % (str(curr_child_frame)), end='')
Greg Claytonc12cc592015-10-16 00:34:18 +0000132 curr_child.resize (curr_child_frame.size)
133 curr_child.slide_position (Size(w=delta_size.w, h=0))
134 elif curr_child_frame.get_max_x() == orig_frame.get_min_x():
135 curr_child_frame.size.w -= delta_size.w
Zachary Turner35d017f2015-10-23 17:04:29 +0000136 #print('adjusted curr_child_frame = %s\r\n' % (str(curr_child_frame)), end='')
Greg Claytonc12cc592015-10-16 00:34:18 +0000137 curr_child.resize (curr_child_frame.size)
138
Greg Clayton414dba52015-09-24 00:19:42 +0000139 def add_key_action(self, arg, callback, decription):
140 if isinstance(arg, list):
141 for key in arg:
142 self.add_key_action(key, callback, description)
143 else:
Zachary Turnerf67f7e32015-10-26 16:51:09 +0000144 if isinstance(arg, six.integer_types):
Greg Clayton414dba52015-09-24 00:19:42 +0000145 key_action_dict = { 'key' : arg,
146 'callback' : callback,
147 'description' : decription }
148 self.key_actions[arg] = key_action_dict
149 elif isinstance(arg, basestring):
150 key_integer = ord(arg)
151 key_action_dict = { 'key' : key_integer,
152 'callback' : callback,
153 'description' : decription }
154 self.key_actions[key_integer] = key_action_dict
155 else:
156 raise ValueError
Greg Clayton72d51442015-10-13 23:16:29 +0000157
158 def draw_title_box(self, title):
159 is_in_first_responder_chain = self.is_in_first_responder_chain()
160 if is_in_first_responder_chain:
161 self.attron (curses.A_REVERSE)
162 self.box()
163 if is_in_first_responder_chain:
164 self.attroff (curses.A_REVERSE)
165 if title:
166 self.addstr(Point(x=2, y=0), ' ' + title + ' ')
167
Greg Clayton87349242015-09-22 00:35:20 +0000168 def remove_child(self, window):
169 self.children.remove(window)
Greg Clayton37191a22015-10-07 20:00:28 +0000170
171 def get_first_responder(self):
172 if len(self.first_responders):
173 return self.first_responders[-1]
174 else:
175 return None
176
Greg Clayton87349242015-09-22 00:35:20 +0000177 def set_first_responder(self, window):
178 if window.can_become_first_responder:
179 if callable(getattr(window, "hidden", None)) and window.hidden():
180 return False
181 if not window in self.children:
182 self.add_child(window)
Greg Clayton37191a22015-10-07 20:00:28 +0000183 # See if we have a current first responder, and if we do, let it know that
184 # it will be resigning as first responder
185 first_responder = self.get_first_responder()
186 if first_responder:
187 first_responder.relinquish_first_responder()
188 # Now set the first responder to "window"
189 if len(self.first_responders) == 0:
190 self.first_responders.append(window)
191 else:
192 self.first_responders[-1] = window
Greg Clayton87349242015-09-22 00:35:20 +0000193 return True
194 else:
195 return False
196
Greg Clayton37191a22015-10-07 20:00:28 +0000197 def push_first_responder(self, window):
198 # Only push the window as the new first responder if the window isn't already the first responder
199 if window != self.get_first_responder():
200 self.first_responders.append(window)
201
202 def pop_first_responder(self, window):
203 # Only pop the window from the first responder list if it is the first responder
204 if window == self.get_first_responder():
205 old_first_responder = self.first_responders.pop()
206 old_first_responder.relinquish_first_responder()
207 return True
208 else:
209 return False
210
211 def relinquish_first_responder(self):
212 '''Override if there is something that you need to do when you lose first responder status.'''
213 pass
214
215 # def resign_first_responder(self, remove_from_parent, new_first_responder):
216 # success = False
217 # if self.parent:
218 # if self.is_first_responder():
219 # self.relinquish_first_responder()
220 # if len(self.parent.first_responder):
221 # self.parent.first_responder = None
222 # success = True
223 # if remove_from_parent:
224 # self.parent.remove_child(self)
225 # if new_first_responder:
226 # self.parent.set_first_responder(new_first_responder)
227 # else:
228 # self.parent.select_next_first_responder()
229 # return success
Greg Clayton1827fc22015-09-19 00:39:09 +0000230
Greg Clayton87349242015-09-22 00:35:20 +0000231 def is_first_responder(self):
232 if self.parent:
Greg Clayton37191a22015-10-07 20:00:28 +0000233 return self.parent.get_first_responder() == self
234 else:
235 return False
236
237 def is_in_first_responder_chain(self):
238 if self.parent:
239 return self in self.parent.first_responders
Greg Clayton87349242015-09-22 00:35:20 +0000240 else:
241 return False
242
243 def select_next_first_responder(self):
Greg Clayton37191a22015-10-07 20:00:28 +0000244 if len(self.first_responders) > 1:
245 self.pop_first_responder(self.first_responders[-1])
246 else:
247 num_children = len(self.children)
248 if num_children == 1:
249 return self.set_first_responder(self.children[0])
250 for (i,window) in enumerate(self.children):
251 if window.is_first_responder():
252 break
253 if i < num_children:
254 for i in range(i+1,num_children):
255 if self.set_first_responder(self.children[i]):
256 return True
257 for i in range(0, i):
258 if self.set_first_responder(self.children[i]):
259 return True
Greg Clayton87349242015-09-22 00:35:20 +0000260
Greg Clayton1827fc22015-09-19 00:39:09 +0000261 def point_in_window(self, pt):
262 size = self.get_size()
263 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 +0000264
Greg Clayton72d51442015-10-13 23:16:29 +0000265 def addch(self, c):
266 try:
267 self.window.addch(c)
268 except:
269 pass
270
271 def addch_at_point(self, pt, c):
Greg Clayton37191a22015-10-07 20:00:28 +0000272 try:
273 self.window.addch(pt.y, pt.x, c)
274 except:
275 pass
Greg Clayton1827fc22015-09-19 00:39:09 +0000276
277 def addstr(self, pt, str):
278 try:
279 self.window.addstr(pt.y, pt.x, str)
280 except:
281 pass
282
Greg Clayton72d51442015-10-13 23:16:29 +0000283 def addnstr_at_point(self, pt, str, n):
Greg Clayton1827fc22015-09-19 00:39:09 +0000284 try:
285 self.window.addnstr(pt.y, pt.x, str, n)
286 except:
287 pass
Greg Clayton72d51442015-10-13 23:16:29 +0000288 def addnstr(self, str, n):
289 try:
290 self.window.addnstr(str, n)
291 except:
292 pass
Greg Clayton1827fc22015-09-19 00:39:09 +0000293
Greg Clayton87349242015-09-22 00:35:20 +0000294 def attron(self, attr):
295 return self.window.attron (attr)
296
297 def attroff(self, attr):
298 return self.window.attroff (attr)
299
300 def box(self, vertch=0, horch=0):
301 if vertch == 0:
302 vertch = curses.ACS_VLINE
303 if horch == 0:
304 horch = curses.ACS_HLINE
305 self.window.box(vertch, horch)
Greg Clayton1827fc22015-09-19 00:39:09 +0000306
307 def get_contained_rect(self, top_inset=0, bottom_inset=0, left_inset=0, right_inset=0, height=-1, width=-1):
308 '''Get a rectangle based on the top "height" lines of this window'''
309 rect = self.get_frame()
310 x = rect.origin.x + left_inset
311 y = rect.origin.y + top_inset
312 if height == -1:
313 h = rect.size.h - (top_inset + bottom_inset)
314 else:
315 h = height
316 if width == -1:
317 w = rect.size.w - (left_inset + right_inset)
318 else:
319 w = width
320 return Rect (x = x, y = y, w = w, h = h)
321
322 def erase(self):
323 self.window.erase()
Greg Clayton72d51442015-10-13 23:16:29 +0000324
325 def get_cursor(self):
326 (y, x) = self.window.getyx()
327 return Point(x=x, y=y)
Greg Clayton1827fc22015-09-19 00:39:09 +0000328
329 def get_frame(self):
330 position = self.get_position()
331 size = self.get_size()
332 return Rect(x=position.x, y=position.y, w=size.w, h=size.h)
333
Greg Claytonc12cc592015-10-16 00:34:18 +0000334 def get_frame_in_parent(self):
335 position = self.get_position_in_parent()
336 size = self.get_size()
337 return Rect(x=position.x, y=position.y, w=size.w, h=size.h)
338
339 def get_position_in_parent(self):
340 (y, x) = self.window.getparyx()
341 return Point(x, y)
342
Greg Clayton1827fc22015-09-19 00:39:09 +0000343 def get_position(self):
344 (y, x) = self.window.getbegyx()
345 return Point(x, y)
346
347 def get_size(self):
348 (y, x) = self.window.getmaxyx()
349 return Size(w=x, h=y)
Greg Clayton37191a22015-10-07 20:00:28 +0000350
Greg Clayton72d51442015-10-13 23:16:29 +0000351 def move(self, pt):
352 self.window.move(pt.y, pt.x)
353
Greg Clayton1827fc22015-09-19 00:39:09 +0000354 def refresh(self):
Greg Clayton87349242015-09-22 00:35:20 +0000355 self.update()
Greg Clayton1827fc22015-09-19 00:39:09 +0000356 curses.panel.update_panels()
Greg Clayton72d51442015-10-13 23:16:29 +0000357 self.move(Point(x=0, y=0))
Greg Clayton1827fc22015-09-19 00:39:09 +0000358 return self.window.refresh()
359
360 def resize(self, size):
Greg Clayton87349242015-09-22 00:35:20 +0000361 return self.window.resize(size.h, size.w)
Greg Clayton1827fc22015-09-19 00:39:09 +0000362
Greg Clayton87349242015-09-22 00:35:20 +0000363 def timeout(self, timeout_msec):
364 return self.window.timeout(timeout_msec)
365
366 def handle_key(self, key, check_parent=True):
367 '''Handle a key press in this window.'''
368
369 # First try the first responder if this window has one, but don't allow
370 # it to check with its parent (False second parameter) so we don't recurse
371 # and get a stack overflow
Greg Clayton37191a22015-10-07 20:00:28 +0000372 for first_responder in reversed(self.first_responders):
373 if first_responder.handle_key(key, False):
Greg Clayton87349242015-09-22 00:35:20 +0000374 return True
Greg Clayton414dba52015-09-24 00:19:42 +0000375
376 # Check our key map to see if we have any actions. Actions don't take
377 # any arguments, they must be callable
378 if key in self.key_actions:
379 key_action = self.key_actions[key]
380 key_action['callback']()
381 return True
382 # Check if there is a wildcard key for any key
383 if -1 in self.key_actions:
384 key_action = self.key_actions[-1]
385 key_action['callback']()
386 return True
Greg Clayton87349242015-09-22 00:35:20 +0000387 # Check if the window delegate wants to handle this key press
388 if self.delegate:
389 if callable(getattr(self.delegate, "handle_key", None)):
390 if self.delegate.handle_key(self, key):
391 return True
392 if self.delegate(self, key):
393 return True
394 # Check if we have a parent window and if so, let the parent
395 # window handle the key press
396 if check_parent and self.parent:
397 return self.parent.handle_key(key, True)
398 else:
399 return False # Key not handled
400
401 def update(self):
402 for child in self.children:
403 child.update()
Greg Clayton5ea44832015-10-15 00:49:36 +0000404
405 def quit_action(self):
406 raise QuitException
Greg Clayton87349242015-09-22 00:35:20 +0000407
Greg Clayton3fd1f742015-10-16 23:34:40 +0000408 def get_key(self, timeout_msec=-1):
409 self.timeout(timeout_msec)
410 done = False
411 c = self.window.getch()
412 if c == 27:
413 self.timeout(0)
414 escape_key = 0
415 while True:
416 escape_key = self.window.getch()
417 if escape_key == -1:
418 break
419 else:
420 c = c << 8 | escape_key
421 self.timeout(timeout_msec)
422 return c
423
Zachary Turnerda3dea62015-10-26 16:51:20 +0000424 def key_event_loop(self, timeout_msec=-1, n=sys.maxsize):
Greg Clayton87349242015-09-22 00:35:20 +0000425 '''Run an event loop to receive key presses and pass them along to the
426 responder chain.
427
428 timeout_msec is the timeout it milliseconds. If the value is -1, an
429 infinite wait will be used. It the value is zero, a non-blocking mode
430 will be used, and if greater than zero it will wait for a key press
431 for timeout_msec milliseconds.
432
433 n is the number of times to go through the event loop before exiting'''
Greg Clayton5ea44832015-10-15 00:49:36 +0000434 done = False
435 while not done and n > 0:
Greg Clayton258c1642015-10-21 21:55:16 +0000436 c = self.get_key(timeout_msec)
Greg Clayton87349242015-09-22 00:35:20 +0000437 if c != -1:
Greg Clayton37191a22015-10-07 20:00:28 +0000438 try:
439 self.handle_key(c)
Greg Clayton5ea44832015-10-15 00:49:36 +0000440 except QuitException:
441 done = True
Greg Clayton87349242015-09-22 00:35:20 +0000442 n -= 1
443
Greg Clayton1827fc22015-09-19 00:39:09 +0000444class Panel(Window):
Greg Clayton87349242015-09-22 00:35:20 +0000445 def __init__(self, frame, delegate = None, can_become_first_responder = True):
Greg Clayton1827fc22015-09-19 00:39:09 +0000446 window = curses.newwin(frame.size.h,frame.size.w, frame.origin.y, frame.origin.x)
Greg Clayton87349242015-09-22 00:35:20 +0000447 super(Panel, self).__init__(window, delegate, can_become_first_responder)
Greg Clayton1827fc22015-09-19 00:39:09 +0000448 self.panel = curses.panel.new_panel(window)
449
Greg Clayton87349242015-09-22 00:35:20 +0000450 def hide(self):
451 return self.panel.hide()
452
453 def hidden(self):
454 return self.panel.hidden()
455
456 def show(self):
457 return self.panel.show()
458
Greg Clayton1827fc22015-09-19 00:39:09 +0000459 def top(self):
Greg Clayton87349242015-09-22 00:35:20 +0000460 return self.panel.top()
Greg Clayton1827fc22015-09-19 00:39:09 +0000461
462 def set_position(self, pt):
463 self.panel.move(pt.y, pt.x)
464
Greg Claytonc12cc592015-10-16 00:34:18 +0000465 def slide_position(self, size):
Greg Clayton1827fc22015-09-19 00:39:09 +0000466 new_position = self.get_position()
Greg Claytonc12cc592015-10-16 00:34:18 +0000467 new_position.x = new_position.x + size.w
468 new_position.y = new_position.y + size.h
Greg Clayton1827fc22015-09-19 00:39:09 +0000469 self.set_position(new_position)
470
471class BoxedPanel(Panel):
Greg Clayton87349242015-09-22 00:35:20 +0000472 def __init__(self, frame, title, delegate = None, can_become_first_responder = True):
473 super(BoxedPanel, self).__init__(frame, delegate, can_become_first_responder)
Greg Clayton1827fc22015-09-19 00:39:09 +0000474 self.title = title
475 self.lines = list()
476 self.first_visible_idx = 0
Greg Clayton87349242015-09-22 00:35:20 +0000477 self.selected_idx = -1
Greg Clayton37191a22015-10-07 20:00:28 +0000478 self.add_key_action(curses.KEY_UP, self.select_prev, "Select the previous item")
479 self.add_key_action(curses.KEY_DOWN, self.select_next, "Select the next item")
480 self.add_key_action(curses.KEY_HOME, self.scroll_begin, "Go to the beginning of the list")
481 self.add_key_action(curses.KEY_END, self.scroll_end, "Go to the end of the list")
Greg Claytonc12cc592015-10-16 00:34:18 +0000482 self.add_key_action(0x1b4f48, self.scroll_begin, "Go to the beginning of the list")
483 self.add_key_action(0x1b4f46, self.scroll_end, "Go to the end of the list")
Greg Clayton37191a22015-10-07 20:00:28 +0000484 self.add_key_action(curses.KEY_PPAGE, self.scroll_page_backward, "Scroll to previous page")
485 self.add_key_action(curses.KEY_NPAGE, self.scroll_page_forward, "Scroll to next forward")
Greg Clayton1827fc22015-09-19 00:39:09 +0000486 self.update()
487
Greg Claytond13c4fb2015-09-22 17:18:15 +0000488 def clear(self, update=True):
489 self.lines = list()
490 self.first_visible_idx = 0
491 self.selected_idx = -1
492 if update:
493 self.update()
494
Greg Clayton1827fc22015-09-19 00:39:09 +0000495 def get_usable_width(self):
496 '''Valid usable width is 0 to (width - 3) since the left and right lines display the box around
497 this frame and we skip a leading space'''
498 w = self.get_size().w
499 if w > 3:
500 return w-3
501 else:
502 return 0
503
504 def get_usable_height(self):
505 '''Valid line indexes are 0 to (height - 2) since the top and bottom lines display the box around this frame.'''
506 h = self.get_size().h
507 if h > 2:
508 return h-2
509 else:
510 return 0
511
512 def get_point_for_line(self, global_line_idx):
513 '''Returns the point to use when displaying a line whose index is "line_idx"'''
514 line_idx = global_line_idx - self.first_visible_idx
515 num_lines = self.get_usable_height()
516 if line_idx < num_lines:
517 return Point(x=2, y=1+line_idx)
518 else:
519 return Point(x=-1, y=-1) # return an invalid coordinate if the line index isn't valid
520
521 def set_title (self, title, update=True):
522 self.title = title
523 if update:
524 self.update()
525
Greg Claytonc12cc592015-10-16 00:34:18 +0000526 def scroll_to_line (self, idx):
527 if idx < len(self.lines):
528 self.selected_idx = idx
529 max_visible_lines = self.get_usable_height()
530 if idx < self.first_visible_idx or idx >= self.first_visible_idx + max_visible_lines:
531 self.first_visible_idx = idx
532 self.refresh()
533
Greg Clayton87349242015-09-22 00:35:20 +0000534 def scroll_begin (self):
535 self.first_visible_idx = 0
536 if len(self.lines) > 0:
537 self.selected_idx = 0
538 else:
539 self.selected_idx = -1
540 self.update()
541
542 def scroll_end (self):
543 max_visible_lines = self.get_usable_height()
544 num_lines = len(self.lines)
Greg Clayton414dba52015-09-24 00:19:42 +0000545 if num_lines > max_visible_lines:
Greg Clayton87349242015-09-22 00:35:20 +0000546 self.first_visible_idx = num_lines - max_visible_lines
547 else:
548 self.first_visible_idx = 0
549 self.selected_idx = num_lines-1
550 self.update()
Greg Clayton37191a22015-10-07 20:00:28 +0000551
552 def scroll_page_backward(self):
553 num_lines = len(self.lines)
554 max_visible_lines = self.get_usable_height()
555 new_index = self.first_visible_idx - max_visible_lines
556 if new_index < 0:
557 self.first_visible_idx = 0
558 else:
559 self.first_visible_idx = new_index
560 self.refresh()
561
562 def scroll_page_forward(self):
563 max_visible_lines = self.get_usable_height()
564 self.first_visible_idx += max_visible_lines
565 self._adjust_first_visible_line()
566 self.refresh()
567
Greg Clayton87349242015-09-22 00:35:20 +0000568 def select_next (self):
569 self.selected_idx += 1
570 if self.selected_idx >= len(self.lines):
571 self.selected_idx = len(self.lines) - 1
Greg Clayton37191a22015-10-07 20:00:28 +0000572 self.refresh()
Greg Clayton87349242015-09-22 00:35:20 +0000573
574 def select_prev (self):
575 self.selected_idx -= 1
576 if self.selected_idx < 0:
577 if len(self.lines) > 0:
578 self.selected_idx = 0
579 else:
580 self.selected_idx = -1
Greg Clayton37191a22015-10-07 20:00:28 +0000581 self.refresh()
Greg Clayton87349242015-09-22 00:35:20 +0000582
583 def get_selected_idx(self):
584 return self.selected_idx
585
Greg Clayton1827fc22015-09-19 00:39:09 +0000586 def _adjust_first_visible_line(self):
587 num_lines = len(self.lines)
588 max_visible_lines = self.get_usable_height()
Greg Clayton37191a22015-10-07 20:00:28 +0000589 if (self.first_visible_idx >= num_lines) or (num_lines - self.first_visible_idx) > max_visible_lines:
Greg Clayton1827fc22015-09-19 00:39:09 +0000590 self.first_visible_idx = num_lines - max_visible_lines
591
592 def append_line(self, s, update=True):
593 self.lines.append(s)
594 self._adjust_first_visible_line()
595 if update:
596 self.update()
597
598 def set_line(self, line_idx, s, update=True):
599 '''Sets a line "line_idx" within the boxed panel to be "s"'''
600 if line_idx < 0:
601 return
602 while line_idx >= len(self.lines):
603 self.lines.append('')
604 self.lines[line_idx] = s
605 self._adjust_first_visible_line()
606 if update:
607 self.update()
608
609 def update(self):
Greg Clayton72d51442015-10-13 23:16:29 +0000610 self.erase()
611 self.draw_title_box(self.title)
Greg Clayton1827fc22015-09-19 00:39:09 +0000612 max_width = self.get_usable_width()
613 for line_idx in range(self.first_visible_idx, len(self.lines)):
614 pt = self.get_point_for_line(line_idx)
615 if pt.is_valid_coordinate():
Greg Clayton87349242015-09-22 00:35:20 +0000616 is_selected = line_idx == self.selected_idx
617 if is_selected:
618 self.attron (curses.A_REVERSE)
Greg Claytonc12cc592015-10-16 00:34:18 +0000619 self.move(pt)
620 self.addnstr(self.lines[line_idx], max_width)
Greg Clayton87349242015-09-22 00:35:20 +0000621 if is_selected:
622 self.attroff (curses.A_REVERSE)
Greg Clayton1827fc22015-09-19 00:39:09 +0000623 else:
624 return
625
Greg Claytonc12cc592015-10-16 00:34:18 +0000626 def load_file(self, path):
627 f = open(path)
628 if f:
629 self.lines = f.read().splitlines()
630 for (idx, line) in enumerate(self.lines):
631 # Remove any tabs from lines since they hose up the display
632 if "\t" in line:
633 self.lines[idx] = (8*' ').join(line.split('\t'))
634 self.selected_idx = 0
635 self.first_visible_idx = 0
636 self.refresh()
637
Greg Clayton37191a22015-10-07 20:00:28 +0000638class Item(object):
639 def __init__(self, title, action):
640 self.title = title
641 self.action = action
Greg Clayton72d51442015-10-13 23:16:29 +0000642
Greg Clayton5ea44832015-10-15 00:49:36 +0000643class TreeItemDelegate(object):
644
645 def might_have_children(self):
646 return False
647
648 def update_children(self, item):
649 '''Return a list of child Item objects'''
650 return None
651
652 def draw_item_string(self, tree_window, item, s):
653 pt = tree_window.get_cursor()
654 width = tree_window.get_size().w - 1
655 if width > pt.x:
656 tree_window.addnstr(s, width - pt.x)
657
658 def draw_item(self, tree_window, item):
659 self.draw_item_string(tree_window, item, item.title)
Greg Claytonc12cc592015-10-16 00:34:18 +0000660
661 def do_action(self):
662 pass
Greg Clayton5ea44832015-10-15 00:49:36 +0000663
Greg Clayton72d51442015-10-13 23:16:29 +0000664class TreeItem(object):
665 def __init__(self, delegate, parent = None, title = None, action = None, is_expanded = False):
666 self.parent = parent
667 self.title = title
668 self.action = action
669 self.delegate = delegate
670 self.is_expanded = not parent or is_expanded == True
Greg Clayton5ea44832015-10-15 00:49:36 +0000671 self._might_have_children = None
Greg Clayton72d51442015-10-13 23:16:29 +0000672 self.children = None
Greg Clayton5ea44832015-10-15 00:49:36 +0000673 self._children_might_have_children = False
Greg Clayton72d51442015-10-13 23:16:29 +0000674
675 def get_children(self):
676 if self.is_expanded and self.might_have_children():
677 if self.children is None:
Greg Clayton5ea44832015-10-15 00:49:36 +0000678 self._children_might_have_children = False
Greg Clayton72d51442015-10-13 23:16:29 +0000679 self.children = self.update_children()
Greg Clayton5ea44832015-10-15 00:49:36 +0000680 for child in self.children:
681 if child.might_have_children():
682 self._children_might_have_children = True
683 break
Greg Clayton72d51442015-10-13 23:16:29 +0000684 else:
Greg Clayton5ea44832015-10-15 00:49:36 +0000685 self._children_might_have_children = False
Greg Clayton72d51442015-10-13 23:16:29 +0000686 self.children = None
687 return self.children
Greg Clayton5ea44832015-10-15 00:49:36 +0000688
Greg Clayton72d51442015-10-13 23:16:29 +0000689 def append_visible_items(self, items):
690 items.append(self)
691 children = self.get_children()
692 if children:
693 for child in children:
694 child.append_visible_items(items)
695
696 def might_have_children(self):
Greg Clayton5ea44832015-10-15 00:49:36 +0000697 if self._might_have_children is None:
Greg Clayton72d51442015-10-13 23:16:29 +0000698 if not self.parent:
699 # Root item always might have children
Greg Clayton5ea44832015-10-15 00:49:36 +0000700 self._might_have_children = True
Greg Clayton72d51442015-10-13 23:16:29 +0000701 else:
702 # Check with the delegate to see if the item might have children
Greg Clayton5ea44832015-10-15 00:49:36 +0000703 self._might_have_children = self.delegate.might_have_children()
704 return self._might_have_children
705
706 def children_might_have_children(self):
707 return self._children_might_have_children
708
Greg Clayton72d51442015-10-13 23:16:29 +0000709 def update_children(self):
710 if self.is_expanded and self.might_have_children():
711 self.children = self.delegate.update_children(self)
712 for child in self.children:
713 child.update_children()
714 else:
715 self.children = None
716 return self.children
Greg Clayton37191a22015-10-07 20:00:28 +0000717
Greg Clayton72d51442015-10-13 23:16:29 +0000718 def get_num_visible_rows(self):
719 rows = 1
720 if self.is_expanded:
721 children = self.get_children()
Greg Claytonc12cc592015-10-16 00:34:18 +0000722 if children:
723 for child in children:
724 rows += child.get_num_visible_rows()
Greg Clayton72d51442015-10-13 23:16:29 +0000725 return rows
726 def draw(self, tree_window, row):
727 display_row = tree_window.get_display_row(row)
728 if display_row >= 0:
729 tree_window.move(tree_window.get_item_draw_point(row))
730 if self.parent:
731 self.parent.draw_tree_for_child(tree_window, self, 0)
732 if self.might_have_children():
733 tree_window.addch (curses.ACS_DIAMOND)
734 tree_window.addch (curses.ACS_HLINE)
Greg Clayton5ea44832015-10-15 00:49:36 +0000735 elif self.parent and self.parent.children_might_have_children():
Greg Claytonc12cc592015-10-16 00:34:18 +0000736 if self.parent.parent:
737 tree_window.addch (curses.ACS_HLINE)
738 tree_window.addch (curses.ACS_HLINE)
739 else:
740 tree_window.addch (' ')
741 tree_window.addch (' ')
Greg Clayton72d51442015-10-13 23:16:29 +0000742 is_selected = tree_window.is_selected(row)
743 if is_selected:
744 tree_window.attron (curses.A_REVERSE)
745 self.delegate.draw_item(tree_window, self)
746 if is_selected:
747 tree_window.attroff (curses.A_REVERSE)
748
749 def draw_tree_for_child (self, tree_window, child, reverse_depth):
750 if self.parent:
751 self.parent.draw_tree_for_child (tree_window, self, reverse_depth + 1)
752 if self.children[-1] == child:
753 # Last child
754 if reverse_depth == 0:
755 tree_window.addch (curses.ACS_LLCORNER)
756 tree_window.addch (curses.ACS_HLINE)
757 else:
758 tree_window.addch (' ')
759 tree_window.addch (' ')
760 else:
761 # Middle child
762 if reverse_depth == 0:
763 tree_window.addch (curses.ACS_LTEE)
764 tree_window.addch (curses.ACS_HLINE)
765 else:
766 tree_window.addch (curses.ACS_VLINE)
767 tree_window.addch (' ')
768
Greg Claytonc12cc592015-10-16 00:34:18 +0000769 def was_selected(self):
770 self.delegate.do_action()
Greg Clayton72d51442015-10-13 23:16:29 +0000771
772class TreePanel(Panel):
773 def __init__(self, frame, title, root_item):
774 self.root_item = root_item
775 self.title = title
776 self.first_visible_idx = 0
777 self.selected_idx = 0
778 self.items = None
779 super(TreePanel, self).__init__(frame)
780 self.add_key_action(curses.KEY_UP, self.select_prev, "Select the previous item")
781 self.add_key_action(curses.KEY_DOWN, self.select_next, "Select the next item")
782 self.add_key_action(curses.KEY_RIGHT,self.right_arrow, "Expand an item")
783 self.add_key_action(curses.KEY_LEFT, self.left_arrow, "Unexpand an item or navigate to parent")
Greg Claytonc12cc592015-10-16 00:34:18 +0000784 self.add_key_action(curses.KEY_HOME, self.scroll_begin, "Go to the beginning of the tree")
785 self.add_key_action(curses.KEY_END, self.scroll_end, "Go to the end of the tree")
786 self.add_key_action(0x1b4f48, self.scroll_begin, "Go to the beginning of the tree")
787 self.add_key_action(0x1b4f46, self.scroll_end, "Go to the end of the tree")
Greg Clayton72d51442015-10-13 23:16:29 +0000788 self.add_key_action(curses.KEY_PPAGE, self.scroll_page_backward, "Scroll to previous page")
789 self.add_key_action(curses.KEY_NPAGE, self.scroll_page_forward, "Scroll to next forward")
790
791 def get_selected_item(self):
792 if self.selected_idx < len(self.items):
793 return self.items[self.selected_idx]
794 else:
795 return None
796
797 def select_item(self, item):
798 if self.items and item in self.items:
799 self.selected_idx = self.items.index(item)
800 return True
801 else:
802 return False
803
804 def get_visible_items(self):
805 # Clear self.items when you want to update all chidren
806 if self.items is None:
807 self.items = list()
808 children = self.root_item.get_children()
809 if children:
810 for child in children:
811 child.append_visible_items(self.items)
812 return self.items
813
814 def update(self):
815 self.erase()
816 self.draw_title_box(self.title)
817 visible_items = self.get_visible_items()
818 for (row, child) in enumerate(visible_items):
819 child.draw(self, row)
820
821 def get_item_draw_point(self, row):
822 display_row = self.get_display_row(row)
823 if display_row >= 0:
824 return Point(2, display_row + 1)
825 else:
826 return Point(-1, -1)
827
828 def get_display_row(self, row):
829 if row >= self.first_visible_idx:
830 display_row = row - self.first_visible_idx
831 if display_row < self.get_size().h-2:
832 return display_row
833 return -1
834
835 def is_selected(self, row):
836 return row == self.selected_idx
837
Greg Claytonc12cc592015-10-16 00:34:18 +0000838 def get_num_lines(self):
839 self.get_visible_items()
840 return len(self.items)
Greg Clayton72d51442015-10-13 23:16:29 +0000841
842 def get_num_visible_lines(self):
843 return self.get_size().h-2
844 def select_next (self):
845 self.selected_idx += 1
846 num_lines = self.get_num_lines()
847 if self.selected_idx >= num_lines:
Greg Claytonc12cc592015-10-16 00:34:18 +0000848 self.selected_idx = num_lines - 1
849 self._selection_changed()
Greg Clayton72d51442015-10-13 23:16:29 +0000850 self.refresh()
851
852 def select_prev (self):
853 self.selected_idx -= 1
854 if self.selected_idx < 0:
855 num_lines = self.get_num_lines()
856 if num_lines > 0:
857 self.selected_idx = 0
858 else:
859 self.selected_idx = -1
Greg Claytonc12cc592015-10-16 00:34:18 +0000860 self._selection_changed()
Greg Clayton72d51442015-10-13 23:16:29 +0000861 self.refresh()
862
863 def scroll_begin (self):
864 self.first_visible_idx = 0
865 num_lines = self.get_num_lines()
866 if num_lines > 0:
867 self.selected_idx = 0
868 else:
869 self.selected_idx = -1
Greg Claytonc12cc592015-10-16 00:34:18 +0000870 self.refresh()
Greg Clayton72d51442015-10-13 23:16:29 +0000871
872 def redisplay_tree(self):
873 self.items = None
874 self.refresh()
875
876 def right_arrow(self):
877 selected_item = self.get_selected_item()
878 if selected_item and selected_item.is_expanded == False:
879 selected_item.is_expanded = True
880 self.redisplay_tree()
881
882 def left_arrow(self):
883 selected_item = self.get_selected_item()
884 if selected_item:
885 if selected_item.is_expanded == True:
886 selected_item.is_expanded = False
887 self.redisplay_tree()
888 elif selected_item.parent:
889 if self.select_item(selected_item.parent):
890 self.refresh()
891
892
893 def scroll_end (self):
894 num_visible_lines = self.get_num_visible_lines()
Greg Claytonc12cc592015-10-16 00:34:18 +0000895 num_lines = self.get_num_lines()
Greg Clayton72d51442015-10-13 23:16:29 +0000896 if num_lines > num_visible_lines:
897 self.first_visible_idx = num_lines - num_visible_lines
898 else:
899 self.first_visible_idx = 0
900 self.selected_idx = num_lines-1
Greg Claytonc12cc592015-10-16 00:34:18 +0000901 self.refresh()
Greg Clayton72d51442015-10-13 23:16:29 +0000902
903 def scroll_page_backward(self):
904 num_visible_lines = self.get_num_visible_lines()
Greg Claytonc12cc592015-10-16 00:34:18 +0000905 new_index = self.selected_idx - num_visible_lines
Greg Clayton72d51442015-10-13 23:16:29 +0000906 if new_index < 0:
Greg Claytonc12cc592015-10-16 00:34:18 +0000907 self.selected_idx = 0
Greg Clayton72d51442015-10-13 23:16:29 +0000908 else:
Greg Claytonc12cc592015-10-16 00:34:18 +0000909 self.selected_idx = new_index
910 self._selection_changed()
Greg Clayton72d51442015-10-13 23:16:29 +0000911 self.refresh()
912
913 def scroll_page_forward(self):
Greg Claytonc12cc592015-10-16 00:34:18 +0000914 num_lines = self.get_num_lines()
Greg Clayton72d51442015-10-13 23:16:29 +0000915 num_visible_lines = self.get_num_visible_lines()
Greg Claytonc12cc592015-10-16 00:34:18 +0000916 new_index = self.selected_idx + num_visible_lines
917 if new_index >= num_lines:
918 new_index = num_lines - 1
919 self.selected_idx = new_index
920 self._selection_changed()
Greg Clayton72d51442015-10-13 23:16:29 +0000921 self.refresh()
922
Greg Claytonc12cc592015-10-16 00:34:18 +0000923 def _selection_changed(self):
924 num_lines = self.get_num_lines()
Greg Clayton72d51442015-10-13 23:16:29 +0000925 num_visible_lines = self.get_num_visible_lines()
Greg Claytonc12cc592015-10-16 00:34:18 +0000926 last_visible_index = self.first_visible_idx + num_visible_lines
927 if self.selected_idx >= last_visible_index:
928 self.first_visible_idx += (self.selected_idx - last_visible_index + 1)
929 if self.selected_idx < self.first_visible_idx:
930 self.first_visible_idx = self.selected_idx
931 if self.selected_idx >= 0 and self.selected_idx < len(self.items):
932 item = self.items[self.selected_idx]
933 item.was_selected()
Greg Clayton72d51442015-10-13 23:16:29 +0000934
935
Greg Clayton37191a22015-10-07 20:00:28 +0000936class Menu(BoxedPanel):
937 def __init__(self, title, items):
938 max_title_width = 0
939 for item in items:
940 if max_title_width < len(item.title):
941 max_title_width = len(item.title)
942 frame = Rect(x=0, y=0, w=max_title_width+4, h=len(items)+2)
943 super(Menu, self).__init__(frame, title=None, delegate=None, can_become_first_responder=True)
944 self.selected_idx = 0
945 self.title = title
946 self.items = items
947 for (item_idx, item) in enumerate(items):
948 self.set_line(item_idx, item.title)
949 self.hide()
950
951 def update(self):
952 super(Menu, self).update()
953
954 def relinquish_first_responder(self):
955 if not self.hidden():
956 self.hide()
957
958 def perform_action(self):
959 selected_idx = self.get_selected_idx()
960 if selected_idx < len(self.items):
961 action = self.items[selected_idx].action
962 if action:
963 action()
964
965class MenuBar(Panel):
966 def __init__(self, frame):
967 super(MenuBar, self).__init__(frame, can_become_first_responder=True)
968 self.menus = list()
969 self.selected_menu_idx = -1
970 self.add_key_action(curses.KEY_LEFT, self.select_prev, "Select the previous menu")
971 self.add_key_action(curses.KEY_RIGHT, self.select_next, "Select the next menu")
972 self.add_key_action(curses.KEY_DOWN, lambda: self.select(0), "Select the first menu")
973 self.add_key_action(27, self.relinquish_first_responder, "Hide current menu")
974 self.add_key_action(curses.KEY_ENTER, self.perform_action, "Select the next menu item")
975 self.add_key_action(10, self.perform_action, "Select the next menu item")
976
Zachary Turnerda3dea62015-10-26 16:51:20 +0000977 def insert_menu(self, menu, index=sys.maxsize):
Greg Clayton37191a22015-10-07 20:00:28 +0000978 if index >= len(self.menus):
979 self.menus.append(menu)
980 else:
981 self.menus.insert(index, menu)
982 pt = self.get_position()
983 for menu in self.menus:
984 menu.set_position(pt)
985 pt.x += len(menu.title) + 5
986
987 def perform_action(self):
988 '''If no menu is visible, show the first menu. If a menu is visible, perform the action
989 associated with the selected menu item in the menu'''
990 menu_visible = False
991 for menu in self.menus:
992 if not menu.hidden():
993 menu_visible = True
994 break
995 if menu_visible:
996 menu.perform_action()
997 self.selected_menu_idx = -1
998 self._selected_menu_changed()
999 else:
1000 self.select(0)
1001
1002 def relinquish_first_responder(self):
1003 if self.selected_menu_idx >= 0:
1004 self.selected_menu_idx = -1
1005 self._selected_menu_changed()
1006
1007 def _selected_menu_changed(self):
1008 for (menu_idx, menu) in enumerate(self.menus):
1009 is_hidden = menu.hidden()
1010 if menu_idx != self.selected_menu_idx:
1011 if not is_hidden:
1012 if self.parent.pop_first_responder(menu) == False:
1013 menu.hide()
1014 for (menu_idx, menu) in enumerate(self.menus):
1015 is_hidden = menu.hidden()
1016 if menu_idx == self.selected_menu_idx:
1017 if is_hidden:
1018 menu.show()
1019 self.parent.push_first_responder(menu)
1020 menu.top()
1021 self.parent.refresh()
1022
1023 def select(self, index):
1024 if index < len(self.menus):
1025 self.selected_menu_idx = index
1026 self._selected_menu_changed()
1027
1028 def select_next (self):
1029 num_menus = len(self.menus)
1030 if self.selected_menu_idx == -1:
1031 if num_menus > 0:
1032 self.selected_menu_idx = 0
1033 self._selected_menu_changed()
1034 else:
1035 if self.selected_menu_idx + 1 < num_menus:
1036 self.selected_menu_idx += 1
1037 else:
1038 self.selected_menu_idx = -1
1039 self._selected_menu_changed()
1040
1041 def select_prev (self):
1042 num_menus = len(self.menus)
1043 if self.selected_menu_idx == -1:
1044 if num_menus > 0:
1045 self.selected_menu_idx = num_menus - 1
1046 self._selected_menu_changed()
1047 else:
1048 if self.selected_menu_idx - 1 >= 0:
1049 self.selected_menu_idx -= 1
1050 else:
1051 self.selected_menu_idx = -1
1052 self._selected_menu_changed()
1053
1054 def update(self):
1055 self.erase()
1056 is_in_first_responder_chain = self.is_in_first_responder_chain()
1057 if is_in_first_responder_chain:
1058 self.attron (curses.A_REVERSE)
1059 pt = Point(x=0, y=0)
1060 for menu in self.menus:
1061 self.addstr(pt, '| ' + menu.title + ' ')
1062 pt.x += len(menu.title) + 5
1063 self.addstr(pt, '|')
1064 width = self.get_size().w
1065 while pt.x < width:
Greg Clayton72d51442015-10-13 23:16:29 +00001066 self.addch_at_point(pt, ' ')
Greg Clayton37191a22015-10-07 20:00:28 +00001067 pt.x += 1
1068 if is_in_first_responder_chain:
1069 self.attroff (curses.A_REVERSE)
1070
1071 for menu in self.menus:
1072 menu.update()
1073
1074
Greg Clayton1827fc22015-09-19 00:39:09 +00001075class StatusPanel(Panel):
1076 def __init__(self, frame):
Greg Clayton87349242015-09-22 00:35:20 +00001077 super(StatusPanel, self).__init__(frame, delegate=None, can_become_first_responder=False)
Greg Clayton1827fc22015-09-19 00:39:09 +00001078 self.status_items = list()
1079 self.status_dicts = dict()
1080 self.next_status_x = 1
1081
1082 def add_status_item(self, name, title, format, width, value, update=True):
1083 status_item_dict = { 'name': name,
1084 'title' : title,
1085 'width' : width,
1086 'format' : format,
1087 'value' : value,
1088 'x' : self.next_status_x }
1089 index = len(self.status_items)
1090 self.status_items.append(status_item_dict)
1091 self.status_dicts[name] = index
1092 self.next_status_x += width + 2;
1093 if update:
1094 self.update()
1095
1096 def increment_status(self, name, update=True):
1097 if name in self.status_dicts:
1098 status_item_idx = self.status_dicts[name]
1099 status_item_dict = self.status_items[status_item_idx]
1100 status_item_dict['value'] = status_item_dict['value'] + 1
1101 if update:
1102 self.update()
1103
1104 def update_status(self, name, value, update=True):
1105 if name in self.status_dicts:
1106 status_item_idx = self.status_dicts[name]
1107 status_item_dict = self.status_items[status_item_idx]
1108 status_item_dict['value'] = status_item_dict['format'] % (value)
1109 if update:
1110 self.update()
1111 def update(self):
1112 self.erase();
1113 for status_item_dict in self.status_items:
Greg Clayton72d51442015-10-13 23:16:29 +00001114 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 +00001115
1116stdscr = None
1117
1118def intialize_curses():
1119 global stdscr
1120 stdscr = curses.initscr()
1121 curses.noecho()
1122 curses.cbreak()
1123 stdscr.keypad(1)
1124 try:
1125 curses.start_color()
1126 except:
1127 pass
1128 return Window(stdscr)
1129
1130def terminate_curses():
1131 global stdscr
1132 if stdscr:
1133 stdscr.keypad(0)
1134 curses.echo()
1135 curses.nocbreak()
1136 curses.endwin()
1137