summaryrefslogtreecommitdiffstats
path: root/lldb/packages/Python/lldbsuite/test/lldbcurses.py
diff options
context:
space:
mode:
authorKate Stone <katherine.stone@apple.com>2016-09-06 20:57:50 +0000
committerKate Stone <katherine.stone@apple.com>2016-09-06 20:57:50 +0000
commitb9c1b51e45b845debb76d8658edabca70ca56079 (patch)
treedfcb5a13ef2b014202340f47036da383eaee74aa /lldb/packages/Python/lldbsuite/test/lldbcurses.py
parentd5aa73376966339caad04013510626ec2e42c760 (diff)
downloadbcm5719-llvm-b9c1b51e45b845debb76d8658edabca70ca56079.tar.gz
bcm5719-llvm-b9c1b51e45b845debb76d8658edabca70ca56079.zip
*** This commit represents a complete reformatting of the LLDB source code
*** to conform to clang-format’s LLVM style. This kind of mass change has *** two obvious implications: Firstly, merging this particular commit into a downstream fork may be a huge effort. Alternatively, it may be worth merging all changes up to this commit, performing the same reformatting operation locally, and then discarding the merge for this particular commit. The commands used to accomplish this reformatting were as follows (with current working directory as the root of the repository): find . \( -iname "*.c" -or -iname "*.cpp" -or -iname "*.h" -or -iname "*.mm" \) -exec clang-format -i {} + find . -iname "*.py" -exec autopep8 --in-place --aggressive --aggressive {} + ; The version of clang-format used was 3.9.0, and autopep8 was 1.2.4. Secondly, “blame” style tools will generally point to this commit instead of a meaningful prior commit. There are alternatives available that will attempt to look through this change and find the appropriate prior commit. YMMV. llvm-svn: 280751
Diffstat (limited to 'lldb/packages/Python/lldbsuite/test/lldbcurses.py')
-rw-r--r--lldb/packages/Python/lldbsuite/test/lldbcurses.py609
1 files changed, 387 insertions, 222 deletions
diff --git a/lldb/packages/Python/lldbsuite/test/lldbcurses.py b/lldb/packages/Python/lldbsuite/test/lldbcurses.py
index bc1fbf00d37..ae0082ce9b7 100644
--- a/lldb/packages/Python/lldbsuite/test/lldbcurses.py
+++ b/lldb/packages/Python/lldbsuite/test/lldbcurses.py
@@ -4,14 +4,16 @@ from __future__ import absolute_import
import curses
import curses.panel
import sys
-import time
+import time
# Third-party modules
import six
# LLDB modules
+
class Point(object):
+
def __init__(self, x, y):
self.x = x
self.y = y
@@ -27,11 +29,13 @@ class Point(object):
def __ne__(self, rhs):
return self.x != rhs.x or self.y != rhs.y
-
+
def is_valid_coordinate(self):
return self.x >= 0 and self.y >= 0
-
+
+
class Size(object):
+
def __init__(self, w, h):
self.w = w
self.h = h
@@ -48,11 +52,13 @@ class Size(object):
def __ne__(self, rhs):
return self.w != rhs.w or self.h != rhs.h
+
class Rect(object):
+
def __init__(self, x=0, y=0, w=0, h=0):
self.origin = Point(x, y)
self.size = Size(w, h)
-
+
def __repr__(self):
return str(self)
@@ -70,7 +76,7 @@ class Rect(object):
def get_max_y(self):
return self.origin.y + self.size.h
-
+
def contains_point(self, pt):
if pt.x < self.get_max_x():
if pt.y < self.get_max_y():
@@ -84,12 +90,16 @@ class Rect(object):
def __ne__(self, rhs):
return self.origin != rhs.origin or self.size != rhs.size
+
class QuitException(Exception):
+
def __init__(self):
super(QuitException, self).__init__('QuitException')
+
class Window(object):
- def __init__(self, window, delegate = None, can_become_first_responder = True):
+
+ def __init__(self, window, delegate=None, can_become_first_responder=True):
self.window = window
self.parent = None
self.delegate = delegate
@@ -97,11 +107,11 @@ class Window(object):
self.first_responders = list()
self.can_become_first_responder = can_become_first_responder
self.key_actions = dict()
-
+
def add_child(self, window):
self.children.append(window)
window.parent = self
-
+
def resize(self, size):
self.window.resize(size.h, size.w)
@@ -109,7 +119,13 @@ class Window(object):
if child in self.children:
frame = self.get_frame()
orig_frame = child.get_frame()
- 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)
+ 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)
old_child_max_x = orig_frame.get_max_x()
new_child_max_x = new_frame.get_max_x()
window_max_x = frame.get_max_x()
@@ -122,7 +138,7 @@ class Window(object):
new_frame.origin.x -= new_child_max_x - window_max_x
child.set_position(new_frame.origin)
child.resize(new_frame.size)
-
+
if adjust_neighbors:
#print('orig_frame = %s\r\n' % (str(orig_frame)), end='')
for curr_child in self.children:
@@ -131,32 +147,33 @@ class Window(object):
curr_child_frame = curr_child.get_frame()
if delta_size.w != 0:
#print('curr_child_frame = %s\r\n' % (str(curr_child_frame)), end='')
- if curr_child_frame.get_min_x() == orig_frame.get_max_x():
+ if curr_child_frame.get_min_x() == orig_frame.get_max_x():
curr_child_frame.origin.x += delta_size.w
curr_child_frame.size.w -= delta_size.w
#print('adjusted curr_child_frame = %s\r\n' % (str(curr_child_frame)), end='')
- curr_child.resize (curr_child_frame.size)
- curr_child.slide_position (Size(w=delta_size.w, h=0))
+ curr_child.resize(curr_child_frame.size)
+ curr_child.slide_position(
+ Size(w=delta_size.w, h=0))
elif curr_child_frame.get_max_x() == orig_frame.get_min_x():
curr_child_frame.size.w -= delta_size.w
#print('adjusted curr_child_frame = %s\r\n' % (str(curr_child_frame)), end='')
- curr_child.resize (curr_child_frame.size)
-
+ curr_child.resize(curr_child_frame.size)
+
def add_key_action(self, arg, callback, decription):
if isinstance(arg, list):
for key in arg:
self.add_key_action(key, callback, description)
else:
if isinstance(arg, six.integer_types):
- key_action_dict = { 'key' : arg,
- 'callback' : callback,
- 'description' : decription }
+ key_action_dict = {'key': arg,
+ 'callback': callback,
+ 'description': decription}
self.key_actions[arg] = key_action_dict
- elif isinstance(arg, basestring):
+ elif isinstance(arg, basestring):
key_integer = ord(arg)
- key_action_dict = { 'key' : key_integer,
- 'callback' : callback,
- 'description' : decription }
+ key_action_dict = {'key': key_integer,
+ 'callback': callback,
+ 'description': decription}
self.key_actions[key_integer] = key_action_dict
else:
raise ValueError
@@ -164,16 +181,16 @@ class Window(object):
def draw_title_box(self, title):
is_in_first_responder_chain = self.is_in_first_responder_chain()
if is_in_first_responder_chain:
- self.attron (curses.A_REVERSE)
+ self.attron(curses.A_REVERSE)
self.box()
if is_in_first_responder_chain:
- self.attroff (curses.A_REVERSE)
+ self.attroff(curses.A_REVERSE)
if title:
self.addstr(Point(x=2, y=0), ' ' + title + ' ')
-
+
def remove_child(self, window):
self.children.remove(window)
-
+
def get_first_responder(self):
if len(self.first_responders):
return self.first_responders[-1]
@@ -182,12 +199,16 @@ class Window(object):
def set_first_responder(self, window):
if window.can_become_first_responder:
- if six.callable(getattr(window, "hidden", None)) and window.hidden():
+ if six.callable(
+ getattr(
+ window,
+ "hidden",
+ None)) and window.hidden():
return False
- if not window in self.children:
+ if window not in self.children:
self.add_child(window)
# See if we have a current first responder, and if we do, let it know that
- # it will be resigning as first responder
+ # it will be resigning as first responder
first_responder = self.get_first_responder()
if first_responder:
first_responder.relinquish_first_responder()
@@ -199,29 +220,31 @@ class Window(object):
return True
else:
return False
-
+
def push_first_responder(self, window):
- # Only push the window as the new first responder if the window isn't already the first responder
+ # Only push the window as the new first responder if the window isn't
+ # already the first responder
if window != self.get_first_responder():
self.first_responders.append(window)
-
- def pop_first_responder(self, window):
- # Only pop the window from the first responder list if it is the first responder
+
+ def pop_first_responder(self, window):
+ # Only pop the window from the first responder list if it is the first
+ # responder
if window == self.get_first_responder():
old_first_responder = self.first_responders.pop()
old_first_responder.relinquish_first_responder()
return True
else:
return False
-
+
def relinquish_first_responder(self):
'''Override if there is something that you need to do when you lose first responder status.'''
- pass
-
- # def resign_first_responder(self, remove_from_parent, new_first_responder):
+ pass
+
+ # def resign_first_responder(self, remove_from_parent, new_first_responder):
# success = False
# if self.parent:
- # if self.is_first_responder():
+ # if self.is_first_responder():
# self.relinquish_first_responder()
# if len(self.parent.first_responder):
# self.parent.first_responder = None
@@ -253,21 +276,21 @@ class Window(object):
num_children = len(self.children)
if num_children == 1:
return self.set_first_responder(self.children[0])
- for (i,window) in enumerate(self.children):
+ for (i, window) in enumerate(self.children):
if window.is_first_responder():
break
if i < num_children:
- for i in range(i+1,num_children):
+ for i in range(i + 1, num_children):
if self.set_first_responder(self.children[i]):
return True
for i in range(0, i):
if self.set_first_responder(self.children[i]):
return True
-
+
def point_in_window(self, pt):
size = self.get_size()
return pt.x >= 0 and pt.x < size.w and pt.y >= 0 and pt.y < size.h
-
+
def addch(self, c):
try:
self.window.addch(c)
@@ -291,6 +314,7 @@ class Window(object):
self.window.addnstr(pt.y, pt.x, str, n)
except:
pass
+
def addnstr(self, str, n):
try:
self.window.addnstr(str, n)
@@ -298,19 +322,26 @@ class Window(object):
pass
def attron(self, attr):
- return self.window.attron (attr)
+ return self.window.attron(attr)
def attroff(self, attr):
- return self.window.attroff (attr)
+ return self.window.attroff(attr)
def box(self, vertch=0, horch=0):
if vertch == 0:
vertch = curses.ACS_VLINE
- if horch == 0:
+ if horch == 0:
horch = curses.ACS_HLINE
self.window.box(vertch, horch)
- def get_contained_rect(self, top_inset=0, bottom_inset=0, left_inset=0, right_inset=0, height=-1, width=-1):
+ def get_contained_rect(
+ self,
+ top_inset=0,
+ bottom_inset=0,
+ left_inset=0,
+ right_inset=0,
+ height=-1,
+ width=-1):
'''Get a rectangle based on the top "height" lines of this window'''
rect = self.get_frame()
x = rect.origin.x + left_inset
@@ -323,11 +354,11 @@ class Window(object):
w = rect.size.w - (left_inset + right_inset)
else:
w = width
- return Rect (x = x, y = y, w = w, h = h)
+ return Rect(x=x, y=y, w=w, h=h)
def erase(self):
self.window.erase()
-
+
def get_cursor(self):
(y, x) = self.window.getyx()
return Point(x=x, y=y)
@@ -341,11 +372,11 @@ class Window(object):
position = self.get_position_in_parent()
size = self.get_size()
return Rect(x=position.x, y=position.y, w=size.w, h=size.h)
-
+
def get_position_in_parent(self):
(y, x) = self.window.getparyx()
return Point(x, y)
-
+
def get_position(self):
(y, x) = self.window.getbegyx()
return Point(x, y)
@@ -362,22 +393,22 @@ class Window(object):
curses.panel.update_panels()
self.move(Point(x=0, y=0))
return self.window.refresh()
-
+
def resize(self, size):
return self.window.resize(size.h, size.w)
-
+
def timeout(self, timeout_msec):
return self.window.timeout(timeout_msec)
def handle_key(self, key, check_parent=True):
'''Handle a key press in this window.'''
-
+
# First try the first responder if this window has one, but don't allow
# it to check with its parent (False second parameter) so we don't recurse
# and get a stack overflow
for first_responder in reversed(self.first_responders):
if first_responder.handle_key(key, False):
- return True
+ return True
# Check our key map to see if we have any actions. Actions don't take
# any arguments, they must be callable
@@ -391,23 +422,23 @@ class Window(object):
key_action['callback']()
return True
# Check if the window delegate wants to handle this key press
- if self.delegate:
+ if self.delegate:
if six.callable(getattr(self.delegate, "handle_key", None)):
if self.delegate.handle_key(self, key):
return True
if self.delegate(self, key):
return True
- # Check if we have a parent window and if so, let the parent
+ # Check if we have a parent window and if so, let the parent
# window handle the key press
if check_parent and self.parent:
return self.parent.handle_key(key, True)
else:
- return False # Key not handled
+ return False # Key not handled
def update(self):
for child in self.children:
child.update()
-
+
def quit_action(self):
raise QuitException
@@ -425,17 +456,17 @@ class Window(object):
else:
c = c << 8 | escape_key
self.timeout(timeout_msec)
- return c
-
+ return c
+
def key_event_loop(self, timeout_msec=-1, n=sys.maxsize):
'''Run an event loop to receive key presses and pass them along to the
responder chain.
-
+
timeout_msec is the timeout it milliseconds. If the value is -1, an
infinite wait will be used. It the value is zero, a non-blocking mode
will be used, and if greater than zero it will wait for a key press
for timeout_msec milliseconds.
-
+
n is the number of times to go through the event loop before exiting'''
done = False
while not done and n > 0:
@@ -447,10 +478,21 @@ class Window(object):
done = True
n -= 1
+
class Panel(Window):
- def __init__(self, frame, delegate = None, can_become_first_responder = True):
- window = curses.newwin(frame.size.h,frame.size.w, frame.origin.y, frame.origin.x)
- super(Panel, self).__init__(window, delegate, can_become_first_responder)
+
+ def __init__(self, frame, delegate=None, can_become_first_responder=True):
+ window = curses.newwin(
+ frame.size.h,
+ frame.size.w,
+ frame.origin.y,
+ frame.origin.x)
+ super(
+ Panel,
+ self).__init__(
+ window,
+ delegate,
+ can_become_first_responder)
self.panel = curses.panel.new_panel(window)
def hide(self):
@@ -464,31 +506,63 @@ class Panel(Window):
def top(self):
return self.panel.top()
-
+
def set_position(self, pt):
self.panel.move(pt.y, pt.x)
-
+
def slide_position(self, size):
new_position = self.get_position()
new_position.x = new_position.x + size.w
new_position.y = new_position.y + size.h
self.set_position(new_position)
+
class BoxedPanel(Panel):
- def __init__(self, frame, title, delegate = None, can_become_first_responder = True):
- super(BoxedPanel, self).__init__(frame, delegate, can_become_first_responder)
+
+ def __init__(self, frame, title, delegate=None,
+ can_become_first_responder=True):
+ super(
+ BoxedPanel,
+ self).__init__(
+ frame,
+ delegate,
+ can_become_first_responder)
self.title = title
self.lines = list()
self.first_visible_idx = 0
self.selected_idx = -1
- self.add_key_action(curses.KEY_UP, self.select_prev, "Select the previous item")
- self.add_key_action(curses.KEY_DOWN, self.select_next, "Select the next item")
- self.add_key_action(curses.KEY_HOME, self.scroll_begin, "Go to the beginning of the list")
- self.add_key_action(curses.KEY_END, self.scroll_end, "Go to the end of the list")
- self.add_key_action(0x1b4f48, self.scroll_begin, "Go to the beginning of the list")
- self.add_key_action(0x1b4f46, self.scroll_end, "Go to the end of the list")
- self.add_key_action(curses.KEY_PPAGE, self.scroll_page_backward, "Scroll to previous page")
- self.add_key_action(curses.KEY_NPAGE, self.scroll_page_forward, "Scroll to next forward")
+ self.add_key_action(
+ curses.KEY_UP,
+ self.select_prev,
+ "Select the previous item")
+ self.add_key_action(
+ curses.KEY_DOWN,
+ self.select_next,
+ "Select the next item")
+ self.add_key_action(
+ curses.KEY_HOME,
+ self.scroll_begin,
+ "Go to the beginning of the list")
+ self.add_key_action(
+ curses.KEY_END,
+ self.scroll_end,
+ "Go to the end of the list")
+ self.add_key_action(
+ 0x1b4f48,
+ self.scroll_begin,
+ "Go to the beginning of the list")
+ self.add_key_action(
+ 0x1b4f46,
+ self.scroll_end,
+ "Go to the end of the list")
+ self.add_key_action(
+ curses.KEY_PPAGE,
+ self.scroll_page_backward,
+ "Scroll to previous page")
+ self.add_key_action(
+ curses.KEY_NPAGE,
+ self.scroll_page_forward,
+ "Scroll to next forward")
self.update()
def clear(self, update=True):
@@ -497,21 +571,21 @@ class BoxedPanel(Panel):
self.selected_idx = -1
if update:
self.update()
-
+
def get_usable_width(self):
- '''Valid usable width is 0 to (width - 3) since the left and right lines display the box around
+ '''Valid usable width is 0 to (width - 3) since the left and right lines display the box around
this frame and we skip a leading space'''
w = self.get_size().w
if w > 3:
- return w-3
+ return w - 3
else:
return 0
-
+
def get_usable_height(self):
'''Valid line indexes are 0 to (height - 2) since the top and bottom lines display the box around this frame.'''
h = self.get_size().h
if h > 2:
- return h-2
+ return h - 2
else:
return 0
@@ -520,24 +594,25 @@ class BoxedPanel(Panel):
line_idx = global_line_idx - self.first_visible_idx
num_lines = self.get_usable_height()
if line_idx < num_lines:
- return Point(x=2, y=1+line_idx)
+ return Point(x=2, y=1 + line_idx)
else:
- return Point(x=-1, y=-1) # return an invalid coordinate if the line index isn't valid
-
- def set_title (self, title, update=True):
+ # return an invalid coordinate if the line index isn't valid
+ return Point(x=-1, y=-1)
+
+ def set_title(self, title, update=True):
self.title = title
if update:
self.update()
- def scroll_to_line (self, idx):
+ def scroll_to_line(self, idx):
if idx < len(self.lines):
self.selected_idx = idx
- max_visible_lines = self.get_usable_height()
+ max_visible_lines = self.get_usable_height()
if idx < self.first_visible_idx or idx >= self.first_visible_idx + max_visible_lines:
self.first_visible_idx = idx
self.refresh()
- def scroll_begin (self):
+ def scroll_begin(self):
self.first_visible_idx = 0
if len(self.lines) > 0:
self.selected_idx = 0
@@ -545,39 +620,39 @@ class BoxedPanel(Panel):
self.selected_idx = -1
self.update()
- def scroll_end (self):
+ def scroll_end(self):
max_visible_lines = self.get_usable_height()
num_lines = len(self.lines)
if num_lines > max_visible_lines:
self.first_visible_idx = num_lines - max_visible_lines
else:
self.first_visible_idx = 0
- self.selected_idx = num_lines-1
+ self.selected_idx = num_lines - 1
self.update()
-
+
def scroll_page_backward(self):
- num_lines = len(self.lines)
- max_visible_lines = self.get_usable_height()
+ num_lines = len(self.lines)
+ max_visible_lines = self.get_usable_height()
new_index = self.first_visible_idx - max_visible_lines
if new_index < 0:
self.first_visible_idx = 0
else:
self.first_visible_idx = new_index
self.refresh()
-
+
def scroll_page_forward(self):
- max_visible_lines = self.get_usable_height()
+ max_visible_lines = self.get_usable_height()
self.first_visible_idx += max_visible_lines
self._adjust_first_visible_line()
self.refresh()
- def select_next (self):
+ def select_next(self):
self.selected_idx += 1
if self.selected_idx >= len(self.lines):
self.selected_idx = len(self.lines) - 1
self.refresh()
-
- def select_prev (self):
+
+ def select_prev(self):
self.selected_idx -= 1
if self.selected_idx < 0:
if len(self.lines) > 0:
@@ -588,13 +663,14 @@ class BoxedPanel(Panel):
def get_selected_idx(self):
return self.selected_idx
-
+
def _adjust_first_visible_line(self):
num_lines = len(self.lines)
max_visible_lines = self.get_usable_height()
- if (self.first_visible_idx >= num_lines) or (num_lines - self.first_visible_idx) > max_visible_lines:
+ if (self.first_visible_idx >= num_lines) or (
+ num_lines - self.first_visible_idx) > max_visible_lines:
self.first_visible_idx = num_lines - max_visible_lines
-
+
def append_line(self, s, update=True):
self.lines.append(s)
self._adjust_first_visible_line()
@@ -611,9 +687,9 @@ class BoxedPanel(Panel):
self._adjust_first_visible_line()
if update:
self.update()
-
+
def update(self):
- self.erase()
+ self.erase()
self.draw_title_box(self.title)
max_width = self.get_usable_width()
for line_idx in range(self.first_visible_idx, len(self.lines)):
@@ -621,11 +697,11 @@ class BoxedPanel(Panel):
if pt.is_valid_coordinate():
is_selected = line_idx == self.selected_idx
if is_selected:
- self.attron (curses.A_REVERSE)
+ self.attron(curses.A_REVERSE)
self.move(pt)
self.addnstr(self.lines[line_idx], max_width)
if is_selected:
- self.attroff (curses.A_REVERSE)
+ self.attroff(curses.A_REVERSE)
else:
return
@@ -636,16 +712,19 @@ class BoxedPanel(Panel):
for (idx, line) in enumerate(self.lines):
# Remove any tabs from lines since they hose up the display
if "\t" in line:
- self.lines[idx] = (8*' ').join(line.split('\t'))
+ self.lines[idx] = (8 * ' ').join(line.split('\t'))
self.selected_idx = 0
self.first_visible_idx = 0
self.refresh()
+
class Item(object):
+
def __init__(self, title, action):
self.title = title
self.action = action
+
class TreeItemDelegate(object):
def might_have_children(self):
@@ -663,21 +742,29 @@ class TreeItemDelegate(object):
def draw_item(self, tree_window, item):
self.draw_item_string(tree_window, item, item.title)
-
+
def do_action(self):
pass
+
class TreeItem(object):
- def __init__(self, delegate, parent = None, title = None, action = None, is_expanded = False):
+
+ def __init__(
+ self,
+ delegate,
+ parent=None,
+ title=None,
+ action=None,
+ is_expanded=False):
self.parent = parent
self.title = title
- self.action = action
+ self.action = action
self.delegate = delegate
- self.is_expanded = not parent or is_expanded == True
+ self.is_expanded = not parent or is_expanded
self._might_have_children = None
self.children = None
self._children_might_have_children = False
-
+
def get_children(self):
if self.is_expanded and self.might_have_children():
if self.children is None:
@@ -691,7 +778,7 @@ class TreeItem(object):
self._children_might_have_children = False
self.children = None
return self.children
-
+
def append_visible_items(self, items):
items.append(self)
children = self.get_children()
@@ -705,7 +792,8 @@ class TreeItem(object):
# Root item always might have children
self._might_have_children = True
else:
- # Check with the delegate to see if the item might have children
+ # Check with the delegate to see if the item might have
+ # children
self._might_have_children = self.delegate.might_have_children()
return self._might_have_children
@@ -717,10 +805,10 @@ class TreeItem(object):
self.children = self.delegate.update_children(self)
for child in self.children:
child.update_children()
- else:
+ else:
self.children = None
return self.children
-
+
def get_num_visible_rows(self):
rows = 1
if self.is_expanded:
@@ -729,6 +817,7 @@ class TreeItem(object):
for child in children:
rows += child.get_num_visible_rows()
return rows
+
def draw(self, tree_window, row):
display_row = tree_window.get_display_row(row)
if display_row >= 0:
@@ -736,79 +825,112 @@ class TreeItem(object):
if self.parent:
self.parent.draw_tree_for_child(tree_window, self, 0)
if self.might_have_children():
- tree_window.addch (curses.ACS_DIAMOND)
- tree_window.addch (curses.ACS_HLINE)
+ tree_window.addch(curses.ACS_DIAMOND)
+ tree_window.addch(curses.ACS_HLINE)
elif self.parent and self.parent.children_might_have_children():
if self.parent.parent:
- tree_window.addch (curses.ACS_HLINE)
- tree_window.addch (curses.ACS_HLINE)
+ tree_window.addch(curses.ACS_HLINE)
+ tree_window.addch(curses.ACS_HLINE)
else:
- tree_window.addch (' ')
- tree_window.addch (' ')
+ tree_window.addch(' ')
+ tree_window.addch(' ')
is_selected = tree_window.is_selected(row)
if is_selected:
- tree_window.attron (curses.A_REVERSE)
+ tree_window.attron(curses.A_REVERSE)
self.delegate.draw_item(tree_window, self)
if is_selected:
- tree_window.attroff (curses.A_REVERSE)
-
- def draw_tree_for_child (self, tree_window, child, reverse_depth):
+ tree_window.attroff(curses.A_REVERSE)
+
+ def draw_tree_for_child(self, tree_window, child, reverse_depth):
if self.parent:
- self.parent.draw_tree_for_child (tree_window, self, reverse_depth + 1)
+ self.parent.draw_tree_for_child(
+ tree_window, self, reverse_depth + 1)
if self.children[-1] == child:
# Last child
if reverse_depth == 0:
- tree_window.addch (curses.ACS_LLCORNER)
- tree_window.addch (curses.ACS_HLINE)
+ tree_window.addch(curses.ACS_LLCORNER)
+ tree_window.addch(curses.ACS_HLINE)
else:
- tree_window.addch (' ')
- tree_window.addch (' ')
+ tree_window.addch(' ')
+ tree_window.addch(' ')
else:
# Middle child
if reverse_depth == 0:
- tree_window.addch (curses.ACS_LTEE)
- tree_window.addch (curses.ACS_HLINE)
+ tree_window.addch(curses.ACS_LTEE)
+ tree_window.addch(curses.ACS_HLINE)
else:
- tree_window.addch (curses.ACS_VLINE)
- tree_window.addch (' ')
-
- def was_selected(self):
+ tree_window.addch(curses.ACS_VLINE)
+ tree_window.addch(' ')
+
+ def was_selected(self):
self.delegate.do_action()
-
+
+
class TreePanel(Panel):
+
def __init__(self, frame, title, root_item):
self.root_item = root_item
self.title = title
self.first_visible_idx = 0
self.selected_idx = 0
self.items = None
- super(TreePanel, self).__init__(frame)
- self.add_key_action(curses.KEY_UP, self.select_prev, "Select the previous item")
- self.add_key_action(curses.KEY_DOWN, self.select_next, "Select the next item")
- self.add_key_action(curses.KEY_RIGHT,self.right_arrow, "Expand an item")
- self.add_key_action(curses.KEY_LEFT, self.left_arrow, "Unexpand an item or navigate to parent")
- self.add_key_action(curses.KEY_HOME, self.scroll_begin, "Go to the beginning of the tree")
- self.add_key_action(curses.KEY_END, self.scroll_end, "Go to the end of the tree")
- self.add_key_action(0x1b4f48, self.scroll_begin, "Go to the beginning of the tree")
- self.add_key_action(0x1b4f46, self.scroll_end, "Go to the end of the tree")
- self.add_key_action(curses.KEY_PPAGE, self.scroll_page_backward, "Scroll to previous page")
- self.add_key_action(curses.KEY_NPAGE, self.scroll_page_forward, "Scroll to next forward")
+ super(TreePanel, self).__init__(frame)
+ self.add_key_action(
+ curses.KEY_UP,
+ self.select_prev,
+ "Select the previous item")
+ self.add_key_action(
+ curses.KEY_DOWN,
+ self.select_next,
+ "Select the next item")
+ self.add_key_action(
+ curses.KEY_RIGHT,
+ self.right_arrow,
+ "Expand an item")
+ self.add_key_action(
+ curses.KEY_LEFT,
+ self.left_arrow,
+ "Unexpand an item or navigate to parent")
+ self.add_key_action(
+ curses.KEY_HOME,
+ self.scroll_begin,
+ "Go to the beginning of the tree")
+ self.add_key_action(
+ curses.KEY_END,
+ self.scroll_end,
+ "Go to the end of the tree")
+ self.add_key_action(
+ 0x1b4f48,
+ self.scroll_begin,
+ "Go to the beginning of the tree")
+ self.add_key_action(
+ 0x1b4f46,
+ self.scroll_end,
+ "Go to the end of the tree")
+ self.add_key_action(
+ curses.KEY_PPAGE,
+ self.scroll_page_backward,
+ "Scroll to previous page")
+ self.add_key_action(
+ curses.KEY_NPAGE,
+ self.scroll_page_forward,
+ "Scroll to next forward")
def get_selected_item(self):
if self.selected_idx < len(self.items):
return self.items[self.selected_idx]
else:
return None
-
+
def select_item(self, item):
if self.items and item in self.items:
self.selected_idx = self.items.index(item)
return True
else:
return False
-
+
def get_visible_items(self):
- # Clear self.items when you want to update all chidren
+ # Clear self.items when you want to update all chidren
if self.items is None:
self.items = list()
children = self.root_item.get_children()
@@ -816,15 +938,15 @@ class TreePanel(Panel):
for child in children:
child.append_visible_items(self.items)
return self.items
-
+
def update(self):
- self.erase()
- self.draw_title_box(self.title)
+ self.erase()
+ self.draw_title_box(self.title)
visible_items = self.get_visible_items()
for (row, child) in enumerate(visible_items):
- child.draw(self, row)
+ child.draw(self, row)
- def get_item_draw_point(self, row):
+ def get_item_draw_point(self, row):
display_row = self.get_display_row(row)
if display_row >= 0:
return Point(2, display_row + 1)
@@ -834,28 +956,29 @@ class TreePanel(Panel):
def get_display_row(self, row):
if row >= self.first_visible_idx:
display_row = row - self.first_visible_idx
- if display_row < self.get_size().h-2:
- return display_row
+ if display_row < self.get_size().h - 2:
+ return display_row
return -1
def is_selected(self, row):
return row == self.selected_idx
- def get_num_lines(self):
+ def get_num_lines(self):
self.get_visible_items()
return len(self.items)
-
+
def get_num_visible_lines(self):
- return self.get_size().h-2
- def select_next (self):
- self.selected_idx += 1
+ return self.get_size().h - 2
+
+ def select_next(self):
+ self.selected_idx += 1
num_lines = self.get_num_lines()
if self.selected_idx >= num_lines:
- self.selected_idx = num_lines - 1
+ self.selected_idx = num_lines - 1
self._selection_changed()
self.refresh()
- def select_prev (self):
+ def select_prev(self):
self.selected_idx -= 1
if self.selected_idx < 0:
num_lines = self.get_num_lines()
@@ -866,7 +989,7 @@ class TreePanel(Panel):
self._selection_changed()
self.refresh()
- def scroll_begin (self):
+ def scroll_begin(self):
self.first_visible_idx = 0
num_lines = self.get_num_lines()
if num_lines > 0:
@@ -874,36 +997,35 @@ class TreePanel(Panel):
else:
self.selected_idx = -1
self.refresh()
-
+
def redisplay_tree(self):
self.items = None
self.refresh()
- def right_arrow(self):
+ def right_arrow(self):
selected_item = self.get_selected_item()
if selected_item and selected_item.is_expanded == False:
selected_item.is_expanded = True
self.redisplay_tree()
-
+
def left_arrow(self):
selected_item = self.get_selected_item()
if selected_item:
- if selected_item.is_expanded == True:
+ if selected_item.is_expanded:
selected_item.is_expanded = False
self.redisplay_tree()
elif selected_item.parent:
if self.select_item(selected_item.parent):
self.refresh()
-
- def scroll_end (self):
+ def scroll_end(self):
num_visible_lines = self.get_num_visible_lines()
num_lines = self.get_num_lines()
if num_lines > num_visible_lines:
self.first_visible_idx = num_lines - num_visible_lines
else:
self.first_visible_idx = 0
- self.selected_idx = num_lines-1
+ self.selected_idx = num_lines - 1
self.refresh()
def scroll_page_backward(self):
@@ -912,7 +1034,7 @@ class TreePanel(Panel):
if new_index < 0:
self.selected_idx = 0
else:
- self.selected_idx = new_index
+ self.selected_idx = new_index
self._selection_changed()
self.refresh()
@@ -926,59 +1048,87 @@ class TreePanel(Panel):
self._selection_changed()
self.refresh()
- def _selection_changed(self):
- num_lines = self.get_num_lines()
+ def _selection_changed(self):
+ num_lines = self.get_num_lines()
num_visible_lines = self.get_num_visible_lines()
last_visible_index = self.first_visible_idx + num_visible_lines
if self.selected_idx >= last_visible_index:
- self.first_visible_idx += (self.selected_idx - last_visible_index + 1)
+ self.first_visible_idx += (self.selected_idx -
+ last_visible_index + 1)
if self.selected_idx < self.first_visible_idx:
- self.first_visible_idx = self.selected_idx
+ self.first_visible_idx = self.selected_idx
if self.selected_idx >= 0 and self.selected_idx < len(self.items):
item = self.items[self.selected_idx]
item.was_selected()
class Menu(BoxedPanel):
+
def __init__(self, title, items):
max_title_width = 0
for item in items:
if max_title_width < len(item.title):
max_title_width = len(item.title)
- frame = Rect(x=0, y=0, w=max_title_width+4, h=len(items)+2)
- super(Menu, self).__init__(frame, title=None, delegate=None, can_become_first_responder=True)
+ frame = Rect(x=0, y=0, w=max_title_width + 4, h=len(items) + 2)
+ super(
+ Menu,
+ self).__init__(
+ frame,
+ title=None,
+ delegate=None,
+ can_become_first_responder=True)
self.selected_idx = 0
self.title = title
self.items = items
for (item_idx, item) in enumerate(items):
self.set_line(item_idx, item.title)
self.hide()
-
+
def update(self):
super(Menu, self).update()
def relinquish_first_responder(self):
if not self.hidden():
- self.hide()
-
- def perform_action(self):
+ self.hide()
+
+ def perform_action(self):
selected_idx = self.get_selected_idx()
if selected_idx < len(self.items):
action = self.items[selected_idx].action
if action:
action()
-
+
+
class MenuBar(Panel):
+
def __init__(self, frame):
super(MenuBar, self).__init__(frame, can_become_first_responder=True)
self.menus = list()
self.selected_menu_idx = -1
- self.add_key_action(curses.KEY_LEFT, self.select_prev, "Select the previous menu")
- self.add_key_action(curses.KEY_RIGHT, self.select_next, "Select the next menu")
- self.add_key_action(curses.KEY_DOWN, lambda: self.select(0), "Select the first menu")
- self.add_key_action(27, self.relinquish_first_responder, "Hide current menu")
- self.add_key_action(curses.KEY_ENTER, self.perform_action, "Select the next menu item")
- self.add_key_action(10, self.perform_action, "Select the next menu item")
+ self.add_key_action(
+ curses.KEY_LEFT,
+ self.select_prev,
+ "Select the previous menu")
+ self.add_key_action(
+ curses.KEY_RIGHT,
+ self.select_next,
+ "Select the next menu")
+ self.add_key_action(
+ curses.KEY_DOWN,
+ lambda: self.select(0),
+ "Select the first menu")
+ self.add_key_action(
+ 27,
+ self.relinquish_first_responder,
+ "Hide current menu")
+ self.add_key_action(
+ curses.KEY_ENTER,
+ self.perform_action,
+ "Select the next menu item")
+ self.add_key_action(
+ 10,
+ self.perform_action,
+ "Select the next menu item")
def insert_menu(self, menu, index=sys.maxsize):
if index >= len(self.menus):
@@ -989,8 +1139,8 @@ class MenuBar(Panel):
for menu in self.menus:
menu.set_position(pt)
pt.x += len(menu.title) + 5
-
- def perform_action(self):
+
+ def perform_action(self):
'''If no menu is visible, show the first menu. If a menu is visible, perform the action
associated with the selected menu item in the menu'''
menu_visible = False
@@ -1004,22 +1154,22 @@ class MenuBar(Panel):
self._selected_menu_changed()
else:
self.select(0)
-
+
def relinquish_first_responder(self):
if self.selected_menu_idx >= 0:
self.selected_menu_idx = -1
self._selected_menu_changed()
-
+
def _selected_menu_changed(self):
for (menu_idx, menu) in enumerate(self.menus):
is_hidden = menu.hidden()
- if menu_idx != self.selected_menu_idx:
+ if menu_idx != self.selected_menu_idx:
if not is_hidden:
if self.parent.pop_first_responder(menu) == False:
menu.hide()
for (menu_idx, menu) in enumerate(self.menus):
is_hidden = menu.hidden()
- if menu_idx == self.selected_menu_idx:
+ if menu_idx == self.selected_menu_idx:
if is_hidden:
menu.show()
self.parent.push_first_responder(menu)
@@ -1030,8 +1180,8 @@ class MenuBar(Panel):
if index < len(self.menus):
self.selected_menu_idx = index
self._selected_menu_changed()
-
- def select_next (self):
+
+ def select_next(self):
num_menus = len(self.menus)
if self.selected_menu_idx == -1:
if num_menus > 0:
@@ -1044,7 +1194,7 @@ class MenuBar(Panel):
self.selected_menu_idx = -1
self._selected_menu_changed()
- def select_prev (self):
+ def select_prev(self):
num_menus = len(self.menus)
if self.selected_menu_idx == -1:
if num_menus > 0:
@@ -1061,41 +1211,47 @@ class MenuBar(Panel):
self.erase()
is_in_first_responder_chain = self.is_in_first_responder_chain()
if is_in_first_responder_chain:
- self.attron (curses.A_REVERSE)
+ self.attron(curses.A_REVERSE)
pt = Point(x=0, y=0)
- for menu in self.menus:
+ for menu in self.menus:
self.addstr(pt, '| ' + menu.title + ' ')
pt.x += len(menu.title) + 5
- self.addstr(pt, '|')
+ self.addstr(pt, '|')
width = self.get_size().w
while pt.x < width:
self.addch_at_point(pt, ' ')
- pt.x += 1
+ pt.x += 1
if is_in_first_responder_chain:
- self.attroff (curses.A_REVERSE)
+ self.attroff(curses.A_REVERSE)
for menu in self.menus:
menu.update()
-
-
+
+
class StatusPanel(Panel):
+
def __init__(self, frame):
- super(StatusPanel, self).__init__(frame, delegate=None, can_become_first_responder=False)
+ super(
+ StatusPanel,
+ self).__init__(
+ frame,
+ delegate=None,
+ can_become_first_responder=False)
self.status_items = list()
self.status_dicts = dict()
self.next_status_x = 1
-
+
def add_status_item(self, name, title, format, width, value, update=True):
- status_item_dict = { 'name': name,
- 'title' : title,
- 'width' : width,
- 'format' : format,
- 'value' : value,
- 'x' : self.next_status_x }
+ status_item_dict = {'name': name,
+ 'title': title,
+ 'width': width,
+ 'format': format,
+ 'value': value,
+ 'x': self.next_status_x}
index = len(self.status_items)
self.status_items.append(status_item_dict)
self.status_dicts[name] = index
- self.next_status_x += width + 2;
+ self.next_status_x += width + 2
if update:
self.update()
@@ -1106,7 +1262,7 @@ class StatusPanel(Panel):
status_item_dict['value'] = status_item_dict['value'] + 1
if update:
self.update()
-
+
def update_status(self, name, value, update=True):
if name in self.status_dicts:
status_item_idx = self.status_dicts[name]
@@ -1114,13 +1270,22 @@ class StatusPanel(Panel):
status_item_dict['value'] = status_item_dict['format'] % (value)
if update:
self.update()
+
def update(self):
- self.erase();
+ self.erase()
for status_item_dict in self.status_items:
- 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'])
+ 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'])
stdscr = None
+
def intialize_curses():
global stdscr
stdscr = curses.initscr()
@@ -1133,6 +1298,7 @@ def intialize_curses():
pass
return Window(stdscr)
+
def terminate_curses():
global stdscr
if stdscr:
@@ -1140,4 +1306,3 @@ def terminate_curses():
curses.echo()
curses.nocbreak()
curses.endwin()
-
OpenPOWER on IntegriCloud