blob: 6e098f356cebd44f6282bd23ae22aff3753377da [file] [log] [blame]
Greg Clayton1827fc22015-09-19 00:39:09 +00001import curses, curses.panel
Greg Clayton87349242015-09-22 00:35:20 +00002import sys
3import time
Greg Clayton1827fc22015-09-19 00:39:09 +00004
5class Point(object):
6 def __init__(self, x, y):
7 self.x = x
8 self.y = y
9
10 def __repr__(self):
11 return str(self)
12
13 def __str__(self):
14 return "(x=%u, y=%u)" % (self.x, self.y)
15
16 def is_valid_coordinate(self):
17 return self.x >= 0 and self.y >= 0
18
19class Size(object):
20 def __init__(self, w, h):
21 self.w = w
22 self.h = h
23
24 def __repr__(self):
25 return str(self)
26
27 def __str__(self):
28 return "(w=%u, h=%u)" % (self.w, self.h)
29
30class Rect(object):
31 def __init__(self, x=0, y=0, w=0, h=0):
32 self.origin = Point(x, y)
33 self.size = Size(w, h)
34
35 def __repr__(self):
36 return str(self)
37
38 def __str__(self):
39 return "{ %s, %s }" % (str(self.origin), str(self.size))
40
41 def get_min_x(self):
42 return self.origin.x
43
44 def get_max_x(self):
45 return self.origin.x + self.size.w
46
47 def get_min_y(self):
48 return self.origin.y
49
50 def get_max_y(self):
51 return self.origin.y + self.size.h
52
53 def contains_point(self, pt):
54 if pt.x < self.get_max_x():
55 if pt.y < self.get_max_y():
56 if pt.x >= self.get_min_y():
57 return pt.y >= self.get_min_y()
58 return False
59
60class Window(object):
Greg Clayton87349242015-09-22 00:35:20 +000061 def __init__(self, window, delegate = None, can_become_first_responder = True):
Greg Clayton1827fc22015-09-19 00:39:09 +000062 self.window = window
Greg Clayton87349242015-09-22 00:35:20 +000063 self.parent = None
64 self.delegate = delegate
65 self.children = list()
Greg Clayton37191a22015-10-07 20:00:28 +000066 self.first_responders = list()
Greg Clayton87349242015-09-22 00:35:20 +000067 self.can_become_first_responder = can_become_first_responder
Greg Clayton414dba52015-09-24 00:19:42 +000068 self.key_actions = dict()
Greg Clayton87349242015-09-22 00:35:20 +000069
70 def add_child(self, window):
71 self.children.append(window)
72 window.parent = self
Greg Clayton414dba52015-09-24 00:19:42 +000073
74 def add_key_action(self, arg, callback, decription):
75 if isinstance(arg, list):
76 for key in arg:
77 self.add_key_action(key, callback, description)
78 else:
79 if isinstance(arg, ( int, long )):
80 key_action_dict = { 'key' : arg,
81 'callback' : callback,
82 'description' : decription }
83 self.key_actions[arg] = key_action_dict
84 elif isinstance(arg, basestring):
85 key_integer = ord(arg)
86 key_action_dict = { 'key' : key_integer,
87 'callback' : callback,
88 'description' : decription }
89 self.key_actions[key_integer] = key_action_dict
90 else:
91 raise ValueError
92
Greg Clayton87349242015-09-22 00:35:20 +000093 def remove_child(self, window):
94 self.children.remove(window)
Greg Clayton37191a22015-10-07 20:00:28 +000095
96 def get_first_responder(self):
97 if len(self.first_responders):
98 return self.first_responders[-1]
99 else:
100 return None
101
Greg Clayton87349242015-09-22 00:35:20 +0000102 def set_first_responder(self, window):
103 if window.can_become_first_responder:
104 if callable(getattr(window, "hidden", None)) and window.hidden():
105 return False
106 if not window in self.children:
107 self.add_child(window)
Greg Clayton37191a22015-10-07 20:00:28 +0000108 # See if we have a current first responder, and if we do, let it know that
109 # it will be resigning as first responder
110 first_responder = self.get_first_responder()
111 if first_responder:
112 first_responder.relinquish_first_responder()
113 # Now set the first responder to "window"
114 if len(self.first_responders) == 0:
115 self.first_responders.append(window)
116 else:
117 self.first_responders[-1] = window
Greg Clayton87349242015-09-22 00:35:20 +0000118 return True
119 else:
120 return False
121
Greg Clayton37191a22015-10-07 20:00:28 +0000122 def push_first_responder(self, window):
123 # Only push the window as the new first responder if the window isn't already the first responder
124 if window != self.get_first_responder():
125 self.first_responders.append(window)
126
127 def pop_first_responder(self, window):
128 # Only pop the window from the first responder list if it is the first responder
129 if window == self.get_first_responder():
130 old_first_responder = self.first_responders.pop()
131 old_first_responder.relinquish_first_responder()
132 return True
133 else:
134 return False
135
136 def relinquish_first_responder(self):
137 '''Override if there is something that you need to do when you lose first responder status.'''
138 pass
139
140 # def resign_first_responder(self, remove_from_parent, new_first_responder):
141 # success = False
142 # if self.parent:
143 # if self.is_first_responder():
144 # self.relinquish_first_responder()
145 # if len(self.parent.first_responder):
146 # self.parent.first_responder = None
147 # success = True
148 # if remove_from_parent:
149 # self.parent.remove_child(self)
150 # if new_first_responder:
151 # self.parent.set_first_responder(new_first_responder)
152 # else:
153 # self.parent.select_next_first_responder()
154 # return success
Greg Clayton1827fc22015-09-19 00:39:09 +0000155
Greg Clayton87349242015-09-22 00:35:20 +0000156 def is_first_responder(self):
157 if self.parent:
Greg Clayton37191a22015-10-07 20:00:28 +0000158 return self.parent.get_first_responder() == self
159 else:
160 return False
161
162 def is_in_first_responder_chain(self):
163 if self.parent:
164 return self in self.parent.first_responders
Greg Clayton87349242015-09-22 00:35:20 +0000165 else:
166 return False
167
168 def select_next_first_responder(self):
Greg Clayton37191a22015-10-07 20:00:28 +0000169 if len(self.first_responders) > 1:
170 self.pop_first_responder(self.first_responders[-1])
171 else:
172 num_children = len(self.children)
173 if num_children == 1:
174 return self.set_first_responder(self.children[0])
175 for (i,window) in enumerate(self.children):
176 if window.is_first_responder():
177 break
178 if i < num_children:
179 for i in range(i+1,num_children):
180 if self.set_first_responder(self.children[i]):
181 return True
182 for i in range(0, i):
183 if self.set_first_responder(self.children[i]):
184 return True
Greg Clayton87349242015-09-22 00:35:20 +0000185
Greg Clayton1827fc22015-09-19 00:39:09 +0000186 def point_in_window(self, pt):
187 size = self.get_size()
188 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 +0000189
190 def addch(self, pt, c):
191 try:
192 self.window.addch(pt.y, pt.x, c)
193 except:
194 pass
Greg Clayton1827fc22015-09-19 00:39:09 +0000195
196 def addstr(self, pt, str):
197 try:
198 self.window.addstr(pt.y, pt.x, str)
199 except:
200 pass
201
202 def addnstr(self, pt, str, n):
203 try:
204 self.window.addnstr(pt.y, pt.x, str, n)
205 except:
206 pass
207
Greg Clayton87349242015-09-22 00:35:20 +0000208 def attron(self, attr):
209 return self.window.attron (attr)
210
211 def attroff(self, attr):
212 return self.window.attroff (attr)
213
214 def box(self, vertch=0, horch=0):
215 if vertch == 0:
216 vertch = curses.ACS_VLINE
217 if horch == 0:
218 horch = curses.ACS_HLINE
219 self.window.box(vertch, horch)
Greg Clayton1827fc22015-09-19 00:39:09 +0000220
221 def get_contained_rect(self, top_inset=0, bottom_inset=0, left_inset=0, right_inset=0, height=-1, width=-1):
222 '''Get a rectangle based on the top "height" lines of this window'''
223 rect = self.get_frame()
224 x = rect.origin.x + left_inset
225 y = rect.origin.y + top_inset
226 if height == -1:
227 h = rect.size.h - (top_inset + bottom_inset)
228 else:
229 h = height
230 if width == -1:
231 w = rect.size.w - (left_inset + right_inset)
232 else:
233 w = width
234 return Rect (x = x, y = y, w = w, h = h)
235
236 def erase(self):
237 self.window.erase()
238
239 def get_frame(self):
240 position = self.get_position()
241 size = self.get_size()
242 return Rect(x=position.x, y=position.y, w=size.w, h=size.h)
243
244 def get_position(self):
245 (y, x) = self.window.getbegyx()
246 return Point(x, y)
247
248 def get_size(self):
249 (y, x) = self.window.getmaxyx()
250 return Size(w=x, h=y)
Greg Clayton37191a22015-10-07 20:00:28 +0000251
Greg Clayton1827fc22015-09-19 00:39:09 +0000252 def refresh(self):
Greg Clayton87349242015-09-22 00:35:20 +0000253 self.update()
Greg Clayton1827fc22015-09-19 00:39:09 +0000254 curses.panel.update_panels()
255 return self.window.refresh()
256
257 def resize(self, size):
Greg Clayton87349242015-09-22 00:35:20 +0000258 return self.window.resize(size.h, size.w)
Greg Clayton1827fc22015-09-19 00:39:09 +0000259
Greg Clayton87349242015-09-22 00:35:20 +0000260 def timeout(self, timeout_msec):
261 return self.window.timeout(timeout_msec)
262
263 def handle_key(self, key, check_parent=True):
264 '''Handle a key press in this window.'''
265
266 # First try the first responder if this window has one, but don't allow
267 # it to check with its parent (False second parameter) so we don't recurse
268 # and get a stack overflow
Greg Clayton37191a22015-10-07 20:00:28 +0000269 for first_responder in reversed(self.first_responders):
270 if first_responder.handle_key(key, False):
Greg Clayton87349242015-09-22 00:35:20 +0000271 return True
Greg Clayton414dba52015-09-24 00:19:42 +0000272
273 # Check our key map to see if we have any actions. Actions don't take
274 # any arguments, they must be callable
275 if key in self.key_actions:
276 key_action = self.key_actions[key]
277 key_action['callback']()
278 return True
279 # Check if there is a wildcard key for any key
280 if -1 in self.key_actions:
281 key_action = self.key_actions[-1]
282 key_action['callback']()
283 return True
Greg Clayton87349242015-09-22 00:35:20 +0000284 # Check if the window delegate wants to handle this key press
285 if self.delegate:
286 if callable(getattr(self.delegate, "handle_key", None)):
287 if self.delegate.handle_key(self, key):
288 return True
289 if self.delegate(self, key):
290 return True
291 # Check if we have a parent window and if so, let the parent
292 # window handle the key press
293 if check_parent and self.parent:
294 return self.parent.handle_key(key, True)
295 else:
296 return False # Key not handled
297
298 def update(self):
299 for child in self.children:
300 child.update()
301
302 def key_event_loop(self, timeout_msec=-1, n=sys.maxint):
303 '''Run an event loop to receive key presses and pass them along to the
304 responder chain.
305
306 timeout_msec is the timeout it milliseconds. If the value is -1, an
307 infinite wait will be used. It the value is zero, a non-blocking mode
308 will be used, and if greater than zero it will wait for a key press
309 for timeout_msec milliseconds.
310
311 n is the number of times to go through the event loop before exiting'''
312 self.timeout(timeout_msec)
313 while n > 0:
314 c = self.window.getch()
315 if c != -1:
Greg Clayton37191a22015-10-07 20:00:28 +0000316 try:
317 self.handle_key(c)
318 except:
319 break
Greg Clayton87349242015-09-22 00:35:20 +0000320 n -= 1
321
Greg Clayton1827fc22015-09-19 00:39:09 +0000322class Panel(Window):
Greg Clayton87349242015-09-22 00:35:20 +0000323 def __init__(self, frame, delegate = None, can_become_first_responder = True):
Greg Clayton1827fc22015-09-19 00:39:09 +0000324 window = curses.newwin(frame.size.h,frame.size.w, frame.origin.y, frame.origin.x)
Greg Clayton87349242015-09-22 00:35:20 +0000325 super(Panel, self).__init__(window, delegate, can_become_first_responder)
Greg Clayton1827fc22015-09-19 00:39:09 +0000326 self.panel = curses.panel.new_panel(window)
327
Greg Clayton87349242015-09-22 00:35:20 +0000328 def hide(self):
329 return self.panel.hide()
330
331 def hidden(self):
332 return self.panel.hidden()
333
334 def show(self):
335 return self.panel.show()
336
Greg Clayton1827fc22015-09-19 00:39:09 +0000337 def top(self):
Greg Clayton87349242015-09-22 00:35:20 +0000338 return self.panel.top()
Greg Clayton1827fc22015-09-19 00:39:09 +0000339
340 def set_position(self, pt):
341 self.panel.move(pt.y, pt.x)
342
343 def slide_position(self, pt):
344 new_position = self.get_position()
345 new_position.x = new_position.x + pt.x
346 new_position.y = new_position.y + pt.y
347 self.set_position(new_position)
348
349class BoxedPanel(Panel):
Greg Clayton87349242015-09-22 00:35:20 +0000350 def __init__(self, frame, title, delegate = None, can_become_first_responder = True):
351 super(BoxedPanel, self).__init__(frame, delegate, can_become_first_responder)
Greg Clayton1827fc22015-09-19 00:39:09 +0000352 self.title = title
353 self.lines = list()
354 self.first_visible_idx = 0
Greg Clayton87349242015-09-22 00:35:20 +0000355 self.selected_idx = -1
Greg Clayton37191a22015-10-07 20:00:28 +0000356 self.add_key_action(curses.KEY_UP, self.select_prev, "Select the previous item")
357 self.add_key_action(curses.KEY_DOWN, self.select_next, "Select the next item")
358 self.add_key_action(curses.KEY_HOME, self.scroll_begin, "Go to the beginning of the list")
359 self.add_key_action(curses.KEY_END, self.scroll_end, "Go to the end of the list")
360 self.add_key_action(curses.KEY_PPAGE, self.scroll_page_backward, "Scroll to previous page")
361 self.add_key_action(curses.KEY_NPAGE, self.scroll_page_forward, "Scroll to next forward")
Greg Clayton1827fc22015-09-19 00:39:09 +0000362 self.update()
363
Greg Claytond13c4fb2015-09-22 17:18:15 +0000364 def clear(self, update=True):
365 self.lines = list()
366 self.first_visible_idx = 0
367 self.selected_idx = -1
368 if update:
369 self.update()
370
Greg Clayton1827fc22015-09-19 00:39:09 +0000371 def get_usable_width(self):
372 '''Valid usable width is 0 to (width - 3) since the left and right lines display the box around
373 this frame and we skip a leading space'''
374 w = self.get_size().w
375 if w > 3:
376 return w-3
377 else:
378 return 0
379
380 def get_usable_height(self):
381 '''Valid line indexes are 0 to (height - 2) since the top and bottom lines display the box around this frame.'''
382 h = self.get_size().h
383 if h > 2:
384 return h-2
385 else:
386 return 0
387
388 def get_point_for_line(self, global_line_idx):
389 '''Returns the point to use when displaying a line whose index is "line_idx"'''
390 line_idx = global_line_idx - self.first_visible_idx
391 num_lines = self.get_usable_height()
392 if line_idx < num_lines:
393 return Point(x=2, y=1+line_idx)
394 else:
395 return Point(x=-1, y=-1) # return an invalid coordinate if the line index isn't valid
396
397 def set_title (self, title, update=True):
398 self.title = title
399 if update:
400 self.update()
401
Greg Clayton87349242015-09-22 00:35:20 +0000402 def scroll_begin (self):
403 self.first_visible_idx = 0
404 if len(self.lines) > 0:
405 self.selected_idx = 0
406 else:
407 self.selected_idx = -1
408 self.update()
409
410 def scroll_end (self):
411 max_visible_lines = self.get_usable_height()
412 num_lines = len(self.lines)
Greg Clayton414dba52015-09-24 00:19:42 +0000413 if num_lines > max_visible_lines:
Greg Clayton87349242015-09-22 00:35:20 +0000414 self.first_visible_idx = num_lines - max_visible_lines
415 else:
416 self.first_visible_idx = 0
417 self.selected_idx = num_lines-1
418 self.update()
Greg Clayton37191a22015-10-07 20:00:28 +0000419
420 def scroll_page_backward(self):
421 num_lines = len(self.lines)
422 max_visible_lines = self.get_usable_height()
423 new_index = self.first_visible_idx - max_visible_lines
424 if new_index < 0:
425 self.first_visible_idx = 0
426 else:
427 self.first_visible_idx = new_index
428 self.refresh()
429
430 def scroll_page_forward(self):
431 max_visible_lines = self.get_usable_height()
432 self.first_visible_idx += max_visible_lines
433 self._adjust_first_visible_line()
434 self.refresh()
435
Greg Clayton87349242015-09-22 00:35:20 +0000436 def select_next (self):
437 self.selected_idx += 1
438 if self.selected_idx >= len(self.lines):
439 self.selected_idx = len(self.lines) - 1
Greg Clayton37191a22015-10-07 20:00:28 +0000440 self.refresh()
Greg Clayton87349242015-09-22 00:35:20 +0000441
442 def select_prev (self):
443 self.selected_idx -= 1
444 if self.selected_idx < 0:
445 if len(self.lines) > 0:
446 self.selected_idx = 0
447 else:
448 self.selected_idx = -1
Greg Clayton37191a22015-10-07 20:00:28 +0000449 self.refresh()
Greg Clayton87349242015-09-22 00:35:20 +0000450
451 def get_selected_idx(self):
452 return self.selected_idx
453
Greg Clayton1827fc22015-09-19 00:39:09 +0000454 def _adjust_first_visible_line(self):
455 num_lines = len(self.lines)
456 max_visible_lines = self.get_usable_height()
Greg Clayton37191a22015-10-07 20:00:28 +0000457 if (self.first_visible_idx >= num_lines) or (num_lines - self.first_visible_idx) > max_visible_lines:
Greg Clayton1827fc22015-09-19 00:39:09 +0000458 self.first_visible_idx = num_lines - max_visible_lines
459
460 def append_line(self, s, update=True):
461 self.lines.append(s)
462 self._adjust_first_visible_line()
463 if update:
464 self.update()
465
466 def set_line(self, line_idx, s, update=True):
467 '''Sets a line "line_idx" within the boxed panel to be "s"'''
468 if line_idx < 0:
469 return
470 while line_idx >= len(self.lines):
471 self.lines.append('')
472 self.lines[line_idx] = s
473 self._adjust_first_visible_line()
474 if update:
475 self.update()
476
477 def update(self):
478 self.erase()
Greg Clayton37191a22015-10-07 20:00:28 +0000479 is_in_first_responder_chain = self.is_in_first_responder_chain()
480 if is_in_first_responder_chain:
Greg Clayton87349242015-09-22 00:35:20 +0000481 self.attron (curses.A_REVERSE)
Greg Clayton1827fc22015-09-19 00:39:09 +0000482 self.box()
Greg Clayton37191a22015-10-07 20:00:28 +0000483 if is_in_first_responder_chain:
Greg Clayton87349242015-09-22 00:35:20 +0000484 self.attroff (curses.A_REVERSE)
Greg Clayton1827fc22015-09-19 00:39:09 +0000485 if self.title:
486 self.addstr(Point(x=2, y=0), ' ' + self.title + ' ')
487 max_width = self.get_usable_width()
488 for line_idx in range(self.first_visible_idx, len(self.lines)):
489 pt = self.get_point_for_line(line_idx)
490 if pt.is_valid_coordinate():
Greg Clayton87349242015-09-22 00:35:20 +0000491 is_selected = line_idx == self.selected_idx
492 if is_selected:
493 self.attron (curses.A_REVERSE)
Greg Clayton1827fc22015-09-19 00:39:09 +0000494 self.addnstr(pt, self.lines[line_idx], max_width)
Greg Clayton87349242015-09-22 00:35:20 +0000495 if is_selected:
496 self.attroff (curses.A_REVERSE)
Greg Clayton1827fc22015-09-19 00:39:09 +0000497 else:
498 return
499
Greg Clayton37191a22015-10-07 20:00:28 +0000500class Item(object):
501 def __init__(self, title, action):
502 self.title = title
503 self.action = action
504
505class Menu(BoxedPanel):
506 def __init__(self, title, items):
507 max_title_width = 0
508 for item in items:
509 if max_title_width < len(item.title):
510 max_title_width = len(item.title)
511 frame = Rect(x=0, y=0, w=max_title_width+4, h=len(items)+2)
512 super(Menu, self).__init__(frame, title=None, delegate=None, can_become_first_responder=True)
513 self.selected_idx = 0
514 self.title = title
515 self.items = items
516 for (item_idx, item) in enumerate(items):
517 self.set_line(item_idx, item.title)
518 self.hide()
519
520 def update(self):
521 super(Menu, self).update()
522
523 def relinquish_first_responder(self):
524 if not self.hidden():
525 self.hide()
526
527 def perform_action(self):
528 selected_idx = self.get_selected_idx()
529 if selected_idx < len(self.items):
530 action = self.items[selected_idx].action
531 if action:
532 action()
533
534class MenuBar(Panel):
535 def __init__(self, frame):
536 super(MenuBar, self).__init__(frame, can_become_first_responder=True)
537 self.menus = list()
538 self.selected_menu_idx = -1
539 self.add_key_action(curses.KEY_LEFT, self.select_prev, "Select the previous menu")
540 self.add_key_action(curses.KEY_RIGHT, self.select_next, "Select the next menu")
541 self.add_key_action(curses.KEY_DOWN, lambda: self.select(0), "Select the first menu")
542 self.add_key_action(27, self.relinquish_first_responder, "Hide current menu")
543 self.add_key_action(curses.KEY_ENTER, self.perform_action, "Select the next menu item")
544 self.add_key_action(10, self.perform_action, "Select the next menu item")
545
546 def insert_menu(self, menu, index=sys.maxint):
547 if index >= len(self.menus):
548 self.menus.append(menu)
549 else:
550 self.menus.insert(index, menu)
551 pt = self.get_position()
552 for menu in self.menus:
553 menu.set_position(pt)
554 pt.x += len(menu.title) + 5
555
556 def perform_action(self):
557 '''If no menu is visible, show the first menu. If a menu is visible, perform the action
558 associated with the selected menu item in the menu'''
559 menu_visible = False
560 for menu in self.menus:
561 if not menu.hidden():
562 menu_visible = True
563 break
564 if menu_visible:
565 menu.perform_action()
566 self.selected_menu_idx = -1
567 self._selected_menu_changed()
568 else:
569 self.select(0)
570
571 def relinquish_first_responder(self):
572 if self.selected_menu_idx >= 0:
573 self.selected_menu_idx = -1
574 self._selected_menu_changed()
575
576 def _selected_menu_changed(self):
577 for (menu_idx, menu) in enumerate(self.menus):
578 is_hidden = menu.hidden()
579 if menu_idx != self.selected_menu_idx:
580 if not is_hidden:
581 if self.parent.pop_first_responder(menu) == False:
582 menu.hide()
583 for (menu_idx, menu) in enumerate(self.menus):
584 is_hidden = menu.hidden()
585 if menu_idx == self.selected_menu_idx:
586 if is_hidden:
587 menu.show()
588 self.parent.push_first_responder(menu)
589 menu.top()
590 self.parent.refresh()
591
592 def select(self, index):
593 if index < len(self.menus):
594 self.selected_menu_idx = index
595 self._selected_menu_changed()
596
597 def select_next (self):
598 num_menus = len(self.menus)
599 if self.selected_menu_idx == -1:
600 if num_menus > 0:
601 self.selected_menu_idx = 0
602 self._selected_menu_changed()
603 else:
604 if self.selected_menu_idx + 1 < num_menus:
605 self.selected_menu_idx += 1
606 else:
607 self.selected_menu_idx = -1
608 self._selected_menu_changed()
609
610 def select_prev (self):
611 num_menus = len(self.menus)
612 if self.selected_menu_idx == -1:
613 if num_menus > 0:
614 self.selected_menu_idx = num_menus - 1
615 self._selected_menu_changed()
616 else:
617 if self.selected_menu_idx - 1 >= 0:
618 self.selected_menu_idx -= 1
619 else:
620 self.selected_menu_idx = -1
621 self._selected_menu_changed()
622
623 def update(self):
624 self.erase()
625 is_in_first_responder_chain = self.is_in_first_responder_chain()
626 if is_in_first_responder_chain:
627 self.attron (curses.A_REVERSE)
628 pt = Point(x=0, y=0)
629 for menu in self.menus:
630 self.addstr(pt, '| ' + menu.title + ' ')
631 pt.x += len(menu.title) + 5
632 self.addstr(pt, '|')
633 width = self.get_size().w
634 while pt.x < width:
635 self.addch(pt, ' ')
636 pt.x += 1
637 if is_in_first_responder_chain:
638 self.attroff (curses.A_REVERSE)
639
640 for menu in self.menus:
641 menu.update()
642
643
Greg Clayton1827fc22015-09-19 00:39:09 +0000644class StatusPanel(Panel):
645 def __init__(self, frame):
Greg Clayton87349242015-09-22 00:35:20 +0000646 super(StatusPanel, self).__init__(frame, delegate=None, can_become_first_responder=False)
Greg Clayton1827fc22015-09-19 00:39:09 +0000647 self.status_items = list()
648 self.status_dicts = dict()
649 self.next_status_x = 1
650
651 def add_status_item(self, name, title, format, width, value, update=True):
652 status_item_dict = { 'name': name,
653 'title' : title,
654 'width' : width,
655 'format' : format,
656 'value' : value,
657 'x' : self.next_status_x }
658 index = len(self.status_items)
659 self.status_items.append(status_item_dict)
660 self.status_dicts[name] = index
661 self.next_status_x += width + 2;
662 if update:
663 self.update()
664
665 def increment_status(self, name, update=True):
666 if name in self.status_dicts:
667 status_item_idx = self.status_dicts[name]
668 status_item_dict = self.status_items[status_item_idx]
669 status_item_dict['value'] = status_item_dict['value'] + 1
670 if update:
671 self.update()
672
673 def update_status(self, name, value, update=True):
674 if name in self.status_dicts:
675 status_item_idx = self.status_dicts[name]
676 status_item_dict = self.status_items[status_item_idx]
677 status_item_dict['value'] = status_item_dict['format'] % (value)
678 if update:
679 self.update()
680 def update(self):
681 self.erase();
682 for status_item_dict in self.status_items:
683 self.addnstr(Point(x=status_item_dict['x'], y=0), '%s: %s' % (status_item_dict['title'], status_item_dict['value']), status_item_dict['width'])
684
685stdscr = None
686
687def intialize_curses():
688 global stdscr
689 stdscr = curses.initscr()
690 curses.noecho()
691 curses.cbreak()
692 stdscr.keypad(1)
693 try:
694 curses.start_color()
695 except:
696 pass
697 return Window(stdscr)
698
699def terminate_curses():
700 global stdscr
701 if stdscr:
702 stdscr.keypad(0)
703 curses.echo()
704 curses.nocbreak()
705 curses.endwin()
706