Python 3 - Turn on absolute imports, and fix existing imports.
Absolute imports were introduced in Python 2.5 as a feature
(e.g. from __future__ import absolute_import), and made default
in Python 3.
When absolute imports are enabled, the import system changes in
a couple of ways:
1) The `import foo` syntax will *only* search sys.path. If `foo`
isn't in sys.path, it won't be found. Period. Without absolute
imports, the import system will also search the same directory
that the importing file resides in, so that you can easily
import from the same folder.
2) From inside a package, you can use a dot syntax to refer to higher
levels of the current package. For example, if you are in the
package lldbsuite.test.utility, then ..foo refers to
lldbsuite.test.foo. You can use this notation with the
`from X import Y` syntax to write intra-package references. For
example, using the previous locationa s a starting point, writing
`from ..support import seven` would import lldbsuite.support.seven
Since this is now the default behavior in Python 3, this means that
importing from the same directory with `import foo` *no longer works*.
As a result, the only way to have portable code is to force absolute
imports for all versions of Python.
See PEP 0328 [https://www.python.org/dev/peps/pep-0328/] for more
information about absolute and relative imports.
Differential Revision: http://reviews.llvm.org/D14342
Reviewed By: Todd Fiala
llvm-svn: 252191
2015-11-06 03:22:28 +08:00
|
|
|
from __future__ import absolute_import
|
2015-11-04 03:20:39 +08:00
|
|
|
|
Python 3 - Turn on absolute imports, and fix existing imports.
Absolute imports were introduced in Python 2.5 as a feature
(e.g. from __future__ import absolute_import), and made default
in Python 3.
When absolute imports are enabled, the import system changes in
a couple of ways:
1) The `import foo` syntax will *only* search sys.path. If `foo`
isn't in sys.path, it won't be found. Period. Without absolute
imports, the import system will also search the same directory
that the importing file resides in, so that you can easily
import from the same folder.
2) From inside a package, you can use a dot syntax to refer to higher
levels of the current package. For example, if you are in the
package lldbsuite.test.utility, then ..foo refers to
lldbsuite.test.foo. You can use this notation with the
`from X import Y` syntax to write intra-package references. For
example, using the previous locationa s a starting point, writing
`from ..support import seven` would import lldbsuite.support.seven
Since this is now the default behavior in Python 3, this means that
importing from the same directory with `import foo` *no longer works*.
As a result, the only way to have portable code is to force absolute
imports for all versions of Python.
See PEP 0328 [https://www.python.org/dev/peps/pep-0328/] for more
information about absolute and relative imports.
Differential Revision: http://reviews.llvm.org/D14342
Reviewed By: Todd Fiala
llvm-svn: 252191
2015-11-06 03:22:28 +08:00
|
|
|
# System modules
|
|
|
|
import curses
|
|
|
|
import curses.panel
|
2015-09-22 08:35:20 +08:00
|
|
|
import sys
|
2016-09-07 04:57:50 +08:00
|
|
|
import time
|
2015-09-19 08:39:09 +08:00
|
|
|
|
Python 3 - Turn on absolute imports, and fix existing imports.
Absolute imports were introduced in Python 2.5 as a feature
(e.g. from __future__ import absolute_import), and made default
in Python 3.
When absolute imports are enabled, the import system changes in
a couple of ways:
1) The `import foo` syntax will *only* search sys.path. If `foo`
isn't in sys.path, it won't be found. Period. Without absolute
imports, the import system will also search the same directory
that the importing file resides in, so that you can easily
import from the same folder.
2) From inside a package, you can use a dot syntax to refer to higher
levels of the current package. For example, if you are in the
package lldbsuite.test.utility, then ..foo refers to
lldbsuite.test.foo. You can use this notation with the
`from X import Y` syntax to write intra-package references. For
example, using the previous locationa s a starting point, writing
`from ..support import seven` would import lldbsuite.support.seven
Since this is now the default behavior in Python 3, this means that
importing from the same directory with `import foo` *no longer works*.
As a result, the only way to have portable code is to force absolute
imports for all versions of Python.
See PEP 0328 [https://www.python.org/dev/peps/pep-0328/] for more
information about absolute and relative imports.
Differential Revision: http://reviews.llvm.org/D14342
Reviewed By: Todd Fiala
llvm-svn: 252191
2015-11-06 03:22:28 +08:00
|
|
|
# Third-party modules
|
|
|
|
import six
|
|
|
|
|
|
|
|
# LLDB modules
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-09-19 08:39:09 +08:00
|
|
|
class Point(object):
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-09-19 08:39:09 +08:00
|
|
|
def __init__(self, x, y):
|
|
|
|
self.x = x
|
|
|
|
self.y = y
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return str(self)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "(x=%u, y=%u)" % (self.x, self.y)
|
2015-10-16 08:34:18 +08:00
|
|
|
|
|
|
|
def __eq__(self, rhs):
|
|
|
|
return self.x == rhs.x and self.y == rhs.y
|
|
|
|
|
|
|
|
def __ne__(self, rhs):
|
|
|
|
return self.x != rhs.x or self.y != rhs.y
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-09-19 08:39:09 +08:00
|
|
|
def is_valid_coordinate(self):
|
|
|
|
return self.x >= 0 and self.y >= 0
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
|
2015-09-19 08:39:09 +08:00
|
|
|
class Size(object):
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-09-19 08:39:09 +08:00
|
|
|
def __init__(self, w, h):
|
|
|
|
self.w = w
|
|
|
|
self.h = h
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return str(self)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "(w=%u, h=%u)" % (self.w, self.h)
|
|
|
|
|
2015-10-16 08:34:18 +08:00
|
|
|
def __eq__(self, rhs):
|
|
|
|
return self.w == rhs.w and self.h == rhs.h
|
|
|
|
|
|
|
|
def __ne__(self, rhs):
|
|
|
|
return self.w != rhs.w or self.h != rhs.h
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-09-19 08:39:09 +08:00
|
|
|
class Rect(object):
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-09-19 08:39:09 +08:00
|
|
|
def __init__(self, x=0, y=0, w=0, h=0):
|
|
|
|
self.origin = Point(x, y)
|
|
|
|
self.size = Size(w, h)
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-09-19 08:39:09 +08:00
|
|
|
def __repr__(self):
|
|
|
|
return str(self)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "{ %s, %s }" % (str(self.origin), str(self.size))
|
|
|
|
|
|
|
|
def get_min_x(self):
|
|
|
|
return self.origin.x
|
|
|
|
|
|
|
|
def get_max_x(self):
|
|
|
|
return self.origin.x + self.size.w
|
|
|
|
|
|
|
|
def get_min_y(self):
|
|
|
|
return self.origin.y
|
|
|
|
|
|
|
|
def get_max_y(self):
|
|
|
|
return self.origin.y + self.size.h
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-09-19 08:39:09 +08:00
|
|
|
def contains_point(self, pt):
|
|
|
|
if pt.x < self.get_max_x():
|
|
|
|
if pt.y < self.get_max_y():
|
|
|
|
if pt.x >= self.get_min_y():
|
|
|
|
return pt.y >= self.get_min_y()
|
|
|
|
return False
|
|
|
|
|
2015-10-16 08:34:18 +08:00
|
|
|
def __eq__(self, rhs):
|
|
|
|
return self.origin == rhs.origin and self.size == rhs.size
|
|
|
|
|
|
|
|
def __ne__(self, rhs):
|
|
|
|
return self.origin != rhs.origin or self.size != rhs.size
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-15 08:49:36 +08:00
|
|
|
class QuitException(Exception):
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-15 08:49:36 +08:00
|
|
|
def __init__(self):
|
|
|
|
super(QuitException, self).__init__('QuitException')
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-09-19 08:39:09 +08:00
|
|
|
class Window(object):
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
def __init__(self, window, delegate=None, can_become_first_responder=True):
|
2015-09-19 08:39:09 +08:00
|
|
|
self.window = window
|
2015-09-22 08:35:20 +08:00
|
|
|
self.parent = None
|
|
|
|
self.delegate = delegate
|
|
|
|
self.children = list()
|
2015-10-08 04:00:28 +08:00
|
|
|
self.first_responders = list()
|
2015-09-22 08:35:20 +08:00
|
|
|
self.can_become_first_responder = can_become_first_responder
|
2015-09-24 08:19:42 +08:00
|
|
|
self.key_actions = dict()
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-09-22 08:35:20 +08:00
|
|
|
def add_child(self, window):
|
|
|
|
self.children.append(window)
|
|
|
|
window.parent = self
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-16 08:34:18 +08:00
|
|
|
def resize(self, size):
|
|
|
|
self.window.resize(size.h, size.w)
|
|
|
|
|
|
|
|
def resize_child(self, child, delta_size, adjust_neighbors):
|
|
|
|
if child in self.children:
|
|
|
|
frame = self.get_frame()
|
|
|
|
orig_frame = child.get_frame()
|
2016-09-07 04:57:50 +08:00
|
|
|
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)
|
2015-10-16 08:34:18 +08:00
|
|
|
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()
|
|
|
|
if new_child_max_x < window_max_x:
|
|
|
|
child.resize(new_frame.size)
|
|
|
|
if old_child_max_x == window_max_x:
|
|
|
|
new_frame.origin.x += window_max_x - new_child_max_x
|
|
|
|
child.set_position(new_frame.origin)
|
|
|
|
elif new_child_max_x > window_max_x:
|
|
|
|
new_frame.origin.x -= new_child_max_x - window_max_x
|
|
|
|
child.set_position(new_frame.origin)
|
|
|
|
child.resize(new_frame.size)
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-16 08:34:18 +08:00
|
|
|
if adjust_neighbors:
|
2015-10-24 01:04:29 +08:00
|
|
|
#print('orig_frame = %s\r\n' % (str(orig_frame)), end='')
|
2015-10-16 08:34:18 +08:00
|
|
|
for curr_child in self.children:
|
|
|
|
if curr_child is child:
|
|
|
|
continue
|
|
|
|
curr_child_frame = curr_child.get_frame()
|
|
|
|
if delta_size.w != 0:
|
2015-10-24 01:04:29 +08:00
|
|
|
#print('curr_child_frame = %s\r\n' % (str(curr_child_frame)), end='')
|
2016-09-07 04:57:50 +08:00
|
|
|
if curr_child_frame.get_min_x() == orig_frame.get_max_x():
|
2015-10-16 08:34:18 +08:00
|
|
|
curr_child_frame.origin.x += delta_size.w
|
|
|
|
curr_child_frame.size.w -= delta_size.w
|
2015-10-24 01:04:29 +08:00
|
|
|
#print('adjusted curr_child_frame = %s\r\n' % (str(curr_child_frame)), end='')
|
2016-09-07 04:57:50 +08:00
|
|
|
curr_child.resize(curr_child_frame.size)
|
|
|
|
curr_child.slide_position(
|
|
|
|
Size(w=delta_size.w, h=0))
|
2015-10-16 08:34:18 +08:00
|
|
|
elif curr_child_frame.get_max_x() == orig_frame.get_min_x():
|
|
|
|
curr_child_frame.size.w -= delta_size.w
|
2015-10-24 01:04:29 +08:00
|
|
|
#print('adjusted curr_child_frame = %s\r\n' % (str(curr_child_frame)), end='')
|
2016-09-07 04:57:50 +08:00
|
|
|
curr_child.resize(curr_child_frame.size)
|
|
|
|
|
2015-09-24 08:19:42 +08:00
|
|
|
def add_key_action(self, arg, callback, decription):
|
|
|
|
if isinstance(arg, list):
|
|
|
|
for key in arg:
|
|
|
|
self.add_key_action(key, callback, description)
|
|
|
|
else:
|
2015-10-27 00:51:09 +08:00
|
|
|
if isinstance(arg, six.integer_types):
|
2016-09-07 04:57:50 +08:00
|
|
|
key_action_dict = {'key': arg,
|
|
|
|
'callback': callback,
|
|
|
|
'description': decription}
|
2015-09-24 08:19:42 +08:00
|
|
|
self.key_actions[arg] = key_action_dict
|
2016-09-07 04:57:50 +08:00
|
|
|
elif isinstance(arg, basestring):
|
2015-09-24 08:19:42 +08:00
|
|
|
key_integer = ord(arg)
|
2016-09-07 04:57:50 +08:00
|
|
|
key_action_dict = {'key': key_integer,
|
|
|
|
'callback': callback,
|
|
|
|
'description': decription}
|
2015-09-24 08:19:42 +08:00
|
|
|
self.key_actions[key_integer] = key_action_dict
|
|
|
|
else:
|
|
|
|
raise ValueError
|
2015-10-14 07:16:29 +08:00
|
|
|
|
|
|
|
def draw_title_box(self, title):
|
|
|
|
is_in_first_responder_chain = self.is_in_first_responder_chain()
|
|
|
|
if is_in_first_responder_chain:
|
2016-09-07 04:57:50 +08:00
|
|
|
self.attron(curses.A_REVERSE)
|
2015-10-14 07:16:29 +08:00
|
|
|
self.box()
|
|
|
|
if is_in_first_responder_chain:
|
2016-09-07 04:57:50 +08:00
|
|
|
self.attroff(curses.A_REVERSE)
|
2015-10-14 07:16:29 +08:00
|
|
|
if title:
|
|
|
|
self.addstr(Point(x=2, y=0), ' ' + title + ' ')
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-09-22 08:35:20 +08:00
|
|
|
def remove_child(self, window):
|
|
|
|
self.children.remove(window)
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-08 04:00:28 +08:00
|
|
|
def get_first_responder(self):
|
|
|
|
if len(self.first_responders):
|
|
|
|
return self.first_responders[-1]
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2015-09-22 08:35:20 +08:00
|
|
|
def set_first_responder(self, window):
|
|
|
|
if window.can_become_first_responder:
|
2016-09-07 04:57:50 +08:00
|
|
|
if six.callable(
|
|
|
|
getattr(
|
|
|
|
window,
|
|
|
|
"hidden",
|
|
|
|
None)) and window.hidden():
|
2015-09-22 08:35:20 +08:00
|
|
|
return False
|
2016-09-07 04:57:50 +08:00
|
|
|
if window not in self.children:
|
2015-09-22 08:35:20 +08:00
|
|
|
self.add_child(window)
|
2015-10-08 04:00:28 +08:00
|
|
|
# See if we have a current first responder, and if we do, let it know that
|
2016-09-07 04:57:50 +08:00
|
|
|
# it will be resigning as first responder
|
2015-10-08 04:00:28 +08:00
|
|
|
first_responder = self.get_first_responder()
|
|
|
|
if first_responder:
|
|
|
|
first_responder.relinquish_first_responder()
|
|
|
|
# Now set the first responder to "window"
|
|
|
|
if len(self.first_responders) == 0:
|
|
|
|
self.first_responders.append(window)
|
|
|
|
else:
|
|
|
|
self.first_responders[-1] = window
|
2015-09-22 08:35:20 +08:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-08 04:00:28 +08:00
|
|
|
def push_first_responder(self, window):
|
2016-09-07 04:57:50 +08:00
|
|
|
# Only push the window as the new first responder if the window isn't
|
|
|
|
# already the first responder
|
2015-10-08 04:00:28 +08:00
|
|
|
if window != self.get_first_responder():
|
|
|
|
self.first_responders.append(window)
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
def pop_first_responder(self, window):
|
|
|
|
# Only pop the window from the first responder list if it is the first
|
|
|
|
# responder
|
2015-10-08 04:00:28 +08:00
|
|
|
if window == self.get_first_responder():
|
|
|
|
old_first_responder = self.first_responders.pop()
|
|
|
|
old_first_responder.relinquish_first_responder()
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-08 04:00:28 +08:00
|
|
|
def relinquish_first_responder(self):
|
|
|
|
'''Override if there is something that you need to do when you lose first responder status.'''
|
2016-09-07 04:57:50 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
# def resign_first_responder(self, remove_from_parent, new_first_responder):
|
2015-10-08 04:00:28 +08:00
|
|
|
# success = False
|
|
|
|
# if self.parent:
|
2016-09-07 04:57:50 +08:00
|
|
|
# if self.is_first_responder():
|
2015-10-08 04:00:28 +08:00
|
|
|
# self.relinquish_first_responder()
|
|
|
|
# if len(self.parent.first_responder):
|
|
|
|
# self.parent.first_responder = None
|
|
|
|
# success = True
|
|
|
|
# if remove_from_parent:
|
|
|
|
# self.parent.remove_child(self)
|
|
|
|
# if new_first_responder:
|
|
|
|
# self.parent.set_first_responder(new_first_responder)
|
|
|
|
# else:
|
|
|
|
# self.parent.select_next_first_responder()
|
|
|
|
# return success
|
2015-09-22 08:35:20 +08:00
|
|
|
|
|
|
|
def is_first_responder(self):
|
|
|
|
if self.parent:
|
2015-10-08 04:00:28 +08:00
|
|
|
return self.parent.get_first_responder() == self
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def is_in_first_responder_chain(self):
|
|
|
|
if self.parent:
|
|
|
|
return self in self.parent.first_responders
|
2015-09-22 08:35:20 +08:00
|
|
|
else:
|
|
|
|
return False
|
2015-09-19 08:39:09 +08:00
|
|
|
|
2015-09-22 08:35:20 +08:00
|
|
|
def select_next_first_responder(self):
|
2015-10-08 04:00:28 +08:00
|
|
|
if len(self.first_responders) > 1:
|
|
|
|
self.pop_first_responder(self.first_responders[-1])
|
|
|
|
else:
|
|
|
|
num_children = len(self.children)
|
|
|
|
if num_children == 1:
|
|
|
|
return self.set_first_responder(self.children[0])
|
2016-09-07 04:57:50 +08:00
|
|
|
for (i, window) in enumerate(self.children):
|
2015-10-08 04:00:28 +08:00
|
|
|
if window.is_first_responder():
|
|
|
|
break
|
|
|
|
if i < num_children:
|
2016-09-07 04:57:50 +08:00
|
|
|
for i in range(i + 1, num_children):
|
2015-10-08 04:00:28 +08:00
|
|
|
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
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-09-19 08:39:09 +08:00
|
|
|
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
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-14 07:16:29 +08:00
|
|
|
def addch(self, c):
|
|
|
|
try:
|
|
|
|
self.window.addch(c)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def addch_at_point(self, pt, c):
|
2015-10-08 04:00:28 +08:00
|
|
|
try:
|
|
|
|
self.window.addch(pt.y, pt.x, c)
|
|
|
|
except:
|
|
|
|
pass
|
2015-09-19 08:39:09 +08:00
|
|
|
|
|
|
|
def addstr(self, pt, str):
|
|
|
|
try:
|
|
|
|
self.window.addstr(pt.y, pt.x, str)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2015-10-14 07:16:29 +08:00
|
|
|
def addnstr_at_point(self, pt, str, n):
|
2015-09-19 08:39:09 +08:00
|
|
|
try:
|
|
|
|
self.window.addnstr(pt.y, pt.x, str, n)
|
|
|
|
except:
|
|
|
|
pass
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-14 07:16:29 +08:00
|
|
|
def addnstr(self, str, n):
|
|
|
|
try:
|
|
|
|
self.window.addnstr(str, n)
|
|
|
|
except:
|
|
|
|
pass
|
2015-09-19 08:39:09 +08:00
|
|
|
|
2015-09-22 08:35:20 +08:00
|
|
|
def attron(self, attr):
|
2016-09-07 04:57:50 +08:00
|
|
|
return self.window.attron(attr)
|
2015-09-22 08:35:20 +08:00
|
|
|
|
|
|
|
def attroff(self, attr):
|
2016-09-07 04:57:50 +08:00
|
|
|
return self.window.attroff(attr)
|
2015-09-22 08:35:20 +08:00
|
|
|
|
|
|
|
def box(self, vertch=0, horch=0):
|
|
|
|
if vertch == 0:
|
|
|
|
vertch = curses.ACS_VLINE
|
2016-09-07 04:57:50 +08:00
|
|
|
if horch == 0:
|
2015-09-22 08:35:20 +08:00
|
|
|
horch = curses.ACS_HLINE
|
|
|
|
self.window.box(vertch, horch)
|
2015-09-19 08:39:09 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def get_contained_rect(
|
|
|
|
self,
|
|
|
|
top_inset=0,
|
|
|
|
bottom_inset=0,
|
|
|
|
left_inset=0,
|
|
|
|
right_inset=0,
|
|
|
|
height=-1,
|
|
|
|
width=-1):
|
2015-09-19 08:39:09 +08:00
|
|
|
'''Get a rectangle based on the top "height" lines of this window'''
|
|
|
|
rect = self.get_frame()
|
|
|
|
x = rect.origin.x + left_inset
|
|
|
|
y = rect.origin.y + top_inset
|
|
|
|
if height == -1:
|
|
|
|
h = rect.size.h - (top_inset + bottom_inset)
|
|
|
|
else:
|
|
|
|
h = height
|
|
|
|
if width == -1:
|
|
|
|
w = rect.size.w - (left_inset + right_inset)
|
|
|
|
else:
|
|
|
|
w = width
|
2016-09-07 04:57:50 +08:00
|
|
|
return Rect(x=x, y=y, w=w, h=h)
|
2015-09-19 08:39:09 +08:00
|
|
|
|
|
|
|
def erase(self):
|
|
|
|
self.window.erase()
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-14 07:16:29 +08:00
|
|
|
def get_cursor(self):
|
|
|
|
(y, x) = self.window.getyx()
|
|
|
|
return Point(x=x, y=y)
|
2015-09-19 08:39:09 +08:00
|
|
|
|
|
|
|
def get_frame(self):
|
|
|
|
position = self.get_position()
|
|
|
|
size = self.get_size()
|
|
|
|
return Rect(x=position.x, y=position.y, w=size.w, h=size.h)
|
|
|
|
|
2015-10-16 08:34:18 +08:00
|
|
|
def get_frame_in_parent(self):
|
|
|
|
position = self.get_position_in_parent()
|
|
|
|
size = self.get_size()
|
|
|
|
return Rect(x=position.x, y=position.y, w=size.w, h=size.h)
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-16 08:34:18 +08:00
|
|
|
def get_position_in_parent(self):
|
|
|
|
(y, x) = self.window.getparyx()
|
|
|
|
return Point(x, y)
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-09-19 08:39:09 +08:00
|
|
|
def get_position(self):
|
|
|
|
(y, x) = self.window.getbegyx()
|
|
|
|
return Point(x, y)
|
|
|
|
|
|
|
|
def get_size(self):
|
|
|
|
(y, x) = self.window.getmaxyx()
|
|
|
|
return Size(w=x, h=y)
|
2015-10-08 04:00:28 +08:00
|
|
|
|
2015-10-14 07:16:29 +08:00
|
|
|
def move(self, pt):
|
|
|
|
self.window.move(pt.y, pt.x)
|
|
|
|
|
2015-09-19 08:39:09 +08:00
|
|
|
def refresh(self):
|
2015-09-22 08:35:20 +08:00
|
|
|
self.update()
|
2015-09-19 08:39:09 +08:00
|
|
|
curses.panel.update_panels()
|
2015-10-14 07:16:29 +08:00
|
|
|
self.move(Point(x=0, y=0))
|
2015-09-19 08:39:09 +08:00
|
|
|
return self.window.refresh()
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-09-19 08:39:09 +08:00
|
|
|
def resize(self, size):
|
2015-09-22 08:35:20 +08:00
|
|
|
return self.window.resize(size.h, size.w)
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-09-22 08:35:20 +08:00
|
|
|
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.'''
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-09-22 08:35:20 +08:00
|
|
|
# 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
|
2015-10-08 04:00:28 +08:00
|
|
|
for first_responder in reversed(self.first_responders):
|
|
|
|
if first_responder.handle_key(key, False):
|
2016-09-07 04:57:50 +08:00
|
|
|
return True
|
2015-09-24 08:19:42 +08:00
|
|
|
|
|
|
|
# Check our key map to see if we have any actions. Actions don't take
|
|
|
|
# any arguments, they must be callable
|
|
|
|
if key in self.key_actions:
|
|
|
|
key_action = self.key_actions[key]
|
|
|
|
key_action['callback']()
|
|
|
|
return True
|
|
|
|
# Check if there is a wildcard key for any key
|
|
|
|
if -1 in self.key_actions:
|
|
|
|
key_action = self.key_actions[-1]
|
|
|
|
key_action['callback']()
|
|
|
|
return True
|
2015-09-22 08:35:20 +08:00
|
|
|
# Check if the window delegate wants to handle this key press
|
2016-09-07 04:57:50 +08:00
|
|
|
if self.delegate:
|
2015-10-27 02:48:24 +08:00
|
|
|
if six.callable(getattr(self.delegate, "handle_key", None)):
|
2015-09-22 08:35:20 +08:00
|
|
|
if self.delegate.handle_key(self, key):
|
|
|
|
return True
|
|
|
|
if self.delegate(self, key):
|
|
|
|
return True
|
2016-09-07 04:57:50 +08:00
|
|
|
# Check if we have a parent window and if so, let the parent
|
2015-09-22 08:35:20 +08:00
|
|
|
# window handle the key press
|
|
|
|
if check_parent and self.parent:
|
|
|
|
return self.parent.handle_key(key, True)
|
|
|
|
else:
|
2016-09-07 04:57:50 +08:00
|
|
|
return False # Key not handled
|
2015-09-22 08:35:20 +08:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
for child in self.children:
|
|
|
|
child.update()
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-15 08:49:36 +08:00
|
|
|
def quit_action(self):
|
|
|
|
raise QuitException
|
2015-09-22 08:35:20 +08:00
|
|
|
|
2015-10-17 07:34:40 +08:00
|
|
|
def get_key(self, timeout_msec=-1):
|
|
|
|
self.timeout(timeout_msec)
|
|
|
|
done = False
|
|
|
|
c = self.window.getch()
|
|
|
|
if c == 27:
|
|
|
|
self.timeout(0)
|
|
|
|
escape_key = 0
|
|
|
|
while True:
|
|
|
|
escape_key = self.window.getch()
|
|
|
|
if escape_key == -1:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
c = c << 8 | escape_key
|
|
|
|
self.timeout(timeout_msec)
|
2016-09-07 04:57:50 +08:00
|
|
|
return c
|
|
|
|
|
2015-10-27 00:51:20 +08:00
|
|
|
def key_event_loop(self, timeout_msec=-1, n=sys.maxsize):
|
2015-09-22 08:35:20 +08:00
|
|
|
'''Run an event loop to receive key presses and pass them along to the
|
|
|
|
responder chain.
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-09-22 08:35:20 +08:00
|
|
|
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.
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-09-22 08:35:20 +08:00
|
|
|
n is the number of times to go through the event loop before exiting'''
|
2015-10-15 08:49:36 +08:00
|
|
|
done = False
|
|
|
|
while not done and n > 0:
|
2015-10-22 05:55:16 +08:00
|
|
|
c = self.get_key(timeout_msec)
|
2015-09-22 08:35:20 +08:00
|
|
|
if c != -1:
|
2015-10-08 04:00:28 +08:00
|
|
|
try:
|
|
|
|
self.handle_key(c)
|
2015-10-15 08:49:36 +08:00
|
|
|
except QuitException:
|
|
|
|
done = True
|
2015-09-22 08:35:20 +08:00
|
|
|
n -= 1
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-09-19 08:39:09 +08:00
|
|
|
class Panel(Window):
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
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)
|
2015-09-19 08:39:09 +08:00
|
|
|
self.panel = curses.panel.new_panel(window)
|
|
|
|
|
2015-09-22 08:35:20 +08:00
|
|
|
def hide(self):
|
|
|
|
return self.panel.hide()
|
|
|
|
|
|
|
|
def hidden(self):
|
|
|
|
return self.panel.hidden()
|
|
|
|
|
|
|
|
def show(self):
|
|
|
|
return self.panel.show()
|
|
|
|
|
2015-09-19 08:39:09 +08:00
|
|
|
def top(self):
|
2015-09-22 08:35:20 +08:00
|
|
|
return self.panel.top()
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-09-19 08:39:09 +08:00
|
|
|
def set_position(self, pt):
|
|
|
|
self.panel.move(pt.y, pt.x)
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-16 08:34:18 +08:00
|
|
|
def slide_position(self, size):
|
2015-09-19 08:39:09 +08:00
|
|
|
new_position = self.get_position()
|
2015-10-16 08:34:18 +08:00
|
|
|
new_position.x = new_position.x + size.w
|
|
|
|
new_position.y = new_position.y + size.h
|
2015-09-19 08:39:09 +08:00
|
|
|
self.set_position(new_position)
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-09-19 08:39:09 +08:00
|
|
|
class BoxedPanel(Panel):
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
def __init__(self, frame, title, delegate=None,
|
|
|
|
can_become_first_responder=True):
|
|
|
|
super(
|
|
|
|
BoxedPanel,
|
|
|
|
self).__init__(
|
|
|
|
frame,
|
|
|
|
delegate,
|
|
|
|
can_become_first_responder)
|
2015-09-19 08:39:09 +08:00
|
|
|
self.title = title
|
|
|
|
self.lines = list()
|
|
|
|
self.first_visible_idx = 0
|
2015-09-22 08:35:20 +08:00
|
|
|
self.selected_idx = -1
|
2016-09-07 04:57:50 +08:00
|
|
|
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")
|
2015-09-19 08:39:09 +08:00
|
|
|
self.update()
|
|
|
|
|
2015-09-23 01:18:15 +08:00
|
|
|
def clear(self, update=True):
|
|
|
|
self.lines = list()
|
|
|
|
self.first_visible_idx = 0
|
|
|
|
self.selected_idx = -1
|
|
|
|
if update:
|
|
|
|
self.update()
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-09-19 08:39:09 +08:00
|
|
|
def get_usable_width(self):
|
2016-09-07 04:57:50 +08:00
|
|
|
'''Valid usable width is 0 to (width - 3) since the left and right lines display the box around
|
2015-09-19 08:39:09 +08:00
|
|
|
this frame and we skip a leading space'''
|
|
|
|
w = self.get_size().w
|
|
|
|
if w > 3:
|
2016-09-07 04:57:50 +08:00
|
|
|
return w - 3
|
2015-09-19 08:39:09 +08:00
|
|
|
else:
|
|
|
|
return 0
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-09-19 08:39:09 +08:00
|
|
|
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:
|
2016-09-07 04:57:50 +08:00
|
|
|
return h - 2
|
2015-09-19 08:39:09 +08:00
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def get_point_for_line(self, global_line_idx):
|
|
|
|
'''Returns the point to use when displaying a line whose index is "line_idx"'''
|
|
|
|
line_idx = global_line_idx - self.first_visible_idx
|
|
|
|
num_lines = self.get_usable_height()
|
|
|
|
if line_idx < num_lines:
|
2016-09-07 04:57:50 +08:00
|
|
|
return Point(x=2, y=1 + line_idx)
|
2015-09-19 08:39:09 +08:00
|
|
|
else:
|
2016-09-07 04:57:50 +08:00
|
|
|
# return an invalid coordinate if the line index isn't valid
|
|
|
|
return Point(x=-1, y=-1)
|
|
|
|
|
|
|
|
def set_title(self, title, update=True):
|
2015-09-19 08:39:09 +08:00
|
|
|
self.title = title
|
|
|
|
if update:
|
|
|
|
self.update()
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def scroll_to_line(self, idx):
|
2015-10-16 08:34:18 +08:00
|
|
|
if idx < len(self.lines):
|
|
|
|
self.selected_idx = idx
|
2016-09-07 04:57:50 +08:00
|
|
|
max_visible_lines = self.get_usable_height()
|
2015-10-16 08:34:18 +08:00
|
|
|
if idx < self.first_visible_idx or idx >= self.first_visible_idx + max_visible_lines:
|
|
|
|
self.first_visible_idx = idx
|
|
|
|
self.refresh()
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def scroll_begin(self):
|
2015-09-22 08:35:20 +08:00
|
|
|
self.first_visible_idx = 0
|
|
|
|
if len(self.lines) > 0:
|
|
|
|
self.selected_idx = 0
|
|
|
|
else:
|
|
|
|
self.selected_idx = -1
|
|
|
|
self.update()
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def scroll_end(self):
|
2015-09-22 08:35:20 +08:00
|
|
|
max_visible_lines = self.get_usable_height()
|
|
|
|
num_lines = len(self.lines)
|
2015-09-24 08:19:42 +08:00
|
|
|
if num_lines > max_visible_lines:
|
2015-09-22 08:35:20 +08:00
|
|
|
self.first_visible_idx = num_lines - max_visible_lines
|
|
|
|
else:
|
|
|
|
self.first_visible_idx = 0
|
2016-09-07 04:57:50 +08:00
|
|
|
self.selected_idx = num_lines - 1
|
2015-09-22 08:35:20 +08:00
|
|
|
self.update()
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-08 04:00:28 +08:00
|
|
|
def scroll_page_backward(self):
|
2016-09-07 04:57:50 +08:00
|
|
|
num_lines = len(self.lines)
|
|
|
|
max_visible_lines = self.get_usable_height()
|
2015-10-08 04:00:28 +08:00
|
|
|
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()
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-08 04:00:28 +08:00
|
|
|
def scroll_page_forward(self):
|
2016-09-07 04:57:50 +08:00
|
|
|
max_visible_lines = self.get_usable_height()
|
2015-10-08 04:00:28 +08:00
|
|
|
self.first_visible_idx += max_visible_lines
|
|
|
|
self._adjust_first_visible_line()
|
|
|
|
self.refresh()
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def select_next(self):
|
2015-09-22 08:35:20 +08:00
|
|
|
self.selected_idx += 1
|
|
|
|
if self.selected_idx >= len(self.lines):
|
|
|
|
self.selected_idx = len(self.lines) - 1
|
2015-10-08 04:00:28 +08:00
|
|
|
self.refresh()
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
def select_prev(self):
|
2015-09-22 08:35:20 +08:00
|
|
|
self.selected_idx -= 1
|
|
|
|
if self.selected_idx < 0:
|
|
|
|
if len(self.lines) > 0:
|
|
|
|
self.selected_idx = 0
|
|
|
|
else:
|
|
|
|
self.selected_idx = -1
|
2015-10-08 04:00:28 +08:00
|
|
|
self.refresh()
|
2015-09-22 08:35:20 +08:00
|
|
|
|
|
|
|
def get_selected_idx(self):
|
|
|
|
return self.selected_idx
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-09-19 08:39:09 +08:00
|
|
|
def _adjust_first_visible_line(self):
|
|
|
|
num_lines = len(self.lines)
|
|
|
|
max_visible_lines = self.get_usable_height()
|
2016-09-07 04:57:50 +08:00
|
|
|
if (self.first_visible_idx >= num_lines) or (
|
|
|
|
num_lines - self.first_visible_idx) > max_visible_lines:
|
2015-09-19 08:39:09 +08:00
|
|
|
self.first_visible_idx = num_lines - max_visible_lines
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-09-19 08:39:09 +08:00
|
|
|
def append_line(self, s, update=True):
|
|
|
|
self.lines.append(s)
|
|
|
|
self._adjust_first_visible_line()
|
|
|
|
if update:
|
|
|
|
self.update()
|
|
|
|
|
|
|
|
def set_line(self, line_idx, s, update=True):
|
|
|
|
'''Sets a line "line_idx" within the boxed panel to be "s"'''
|
|
|
|
if line_idx < 0:
|
|
|
|
return
|
|
|
|
while line_idx >= len(self.lines):
|
|
|
|
self.lines.append('')
|
|
|
|
self.lines[line_idx] = s
|
|
|
|
self._adjust_first_visible_line()
|
|
|
|
if update:
|
|
|
|
self.update()
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-09-19 08:39:09 +08:00
|
|
|
def update(self):
|
2016-09-07 04:57:50 +08:00
|
|
|
self.erase()
|
2015-10-14 07:16:29 +08:00
|
|
|
self.draw_title_box(self.title)
|
2015-09-19 08:39:09 +08:00
|
|
|
max_width = self.get_usable_width()
|
|
|
|
for line_idx in range(self.first_visible_idx, len(self.lines)):
|
|
|
|
pt = self.get_point_for_line(line_idx)
|
|
|
|
if pt.is_valid_coordinate():
|
2015-09-22 08:35:20 +08:00
|
|
|
is_selected = line_idx == self.selected_idx
|
|
|
|
if is_selected:
|
2016-09-07 04:57:50 +08:00
|
|
|
self.attron(curses.A_REVERSE)
|
2015-10-16 08:34:18 +08:00
|
|
|
self.move(pt)
|
|
|
|
self.addnstr(self.lines[line_idx], max_width)
|
2015-09-22 08:35:20 +08:00
|
|
|
if is_selected:
|
2016-09-07 04:57:50 +08:00
|
|
|
self.attroff(curses.A_REVERSE)
|
2015-09-19 08:39:09 +08:00
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
2015-10-16 08:34:18 +08:00
|
|
|
def load_file(self, path):
|
|
|
|
f = open(path)
|
|
|
|
if f:
|
|
|
|
self.lines = f.read().splitlines()
|
|
|
|
for (idx, line) in enumerate(self.lines):
|
|
|
|
# Remove any tabs from lines since they hose up the display
|
|
|
|
if "\t" in line:
|
2016-09-07 04:57:50 +08:00
|
|
|
self.lines[idx] = (8 * ' ').join(line.split('\t'))
|
2015-10-16 08:34:18 +08:00
|
|
|
self.selected_idx = 0
|
|
|
|
self.first_visible_idx = 0
|
|
|
|
self.refresh()
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-08 04:00:28 +08:00
|
|
|
class Item(object):
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-08 04:00:28 +08:00
|
|
|
def __init__(self, title, action):
|
|
|
|
self.title = title
|
|
|
|
self.action = action
|
2015-10-14 07:16:29 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-15 08:49:36 +08:00
|
|
|
class TreeItemDelegate(object):
|
|
|
|
|
|
|
|
def might_have_children(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def update_children(self, item):
|
|
|
|
'''Return a list of child Item objects'''
|
|
|
|
return None
|
|
|
|
|
|
|
|
def draw_item_string(self, tree_window, item, s):
|
|
|
|
pt = tree_window.get_cursor()
|
|
|
|
width = tree_window.get_size().w - 1
|
|
|
|
if width > pt.x:
|
|
|
|
tree_window.addnstr(s, width - pt.x)
|
|
|
|
|
|
|
|
def draw_item(self, tree_window, item):
|
|
|
|
self.draw_item_string(tree_window, item, item.title)
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-16 08:34:18 +08:00
|
|
|
def do_action(self):
|
|
|
|
pass
|
2015-10-15 08:49:36 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-14 07:16:29 +08:00
|
|
|
class TreeItem(object):
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
delegate,
|
|
|
|
parent=None,
|
|
|
|
title=None,
|
|
|
|
action=None,
|
|
|
|
is_expanded=False):
|
2015-10-14 07:16:29 +08:00
|
|
|
self.parent = parent
|
|
|
|
self.title = title
|
2016-09-07 04:57:50 +08:00
|
|
|
self.action = action
|
2015-10-14 07:16:29 +08:00
|
|
|
self.delegate = delegate
|
2016-09-07 04:57:50 +08:00
|
|
|
self.is_expanded = not parent or is_expanded
|
2015-10-15 08:49:36 +08:00
|
|
|
self._might_have_children = None
|
2015-10-14 07:16:29 +08:00
|
|
|
self.children = None
|
2015-10-15 08:49:36 +08:00
|
|
|
self._children_might_have_children = False
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-14 07:16:29 +08:00
|
|
|
def get_children(self):
|
|
|
|
if self.is_expanded and self.might_have_children():
|
|
|
|
if self.children is None:
|
2015-10-15 08:49:36 +08:00
|
|
|
self._children_might_have_children = False
|
2015-10-14 07:16:29 +08:00
|
|
|
self.children = self.update_children()
|
2015-10-15 08:49:36 +08:00
|
|
|
for child in self.children:
|
|
|
|
if child.might_have_children():
|
|
|
|
self._children_might_have_children = True
|
|
|
|
break
|
2015-10-14 07:16:29 +08:00
|
|
|
else:
|
2015-10-15 08:49:36 +08:00
|
|
|
self._children_might_have_children = False
|
2015-10-14 07:16:29 +08:00
|
|
|
self.children = None
|
|
|
|
return self.children
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-14 07:16:29 +08:00
|
|
|
def append_visible_items(self, items):
|
|
|
|
items.append(self)
|
|
|
|
children = self.get_children()
|
|
|
|
if children:
|
|
|
|
for child in children:
|
|
|
|
child.append_visible_items(items)
|
|
|
|
|
|
|
|
def might_have_children(self):
|
2015-10-15 08:49:36 +08:00
|
|
|
if self._might_have_children is None:
|
2015-10-14 07:16:29 +08:00
|
|
|
if not self.parent:
|
|
|
|
# Root item always might have children
|
2015-10-15 08:49:36 +08:00
|
|
|
self._might_have_children = True
|
2015-10-14 07:16:29 +08:00
|
|
|
else:
|
2016-09-07 04:57:50 +08:00
|
|
|
# Check with the delegate to see if the item might have
|
|
|
|
# children
|
2015-10-15 08:49:36 +08:00
|
|
|
self._might_have_children = self.delegate.might_have_children()
|
|
|
|
return self._might_have_children
|
|
|
|
|
|
|
|
def children_might_have_children(self):
|
|
|
|
return self._children_might_have_children
|
|
|
|
|
2015-10-14 07:16:29 +08:00
|
|
|
def update_children(self):
|
|
|
|
if self.is_expanded and self.might_have_children():
|
|
|
|
self.children = self.delegate.update_children(self)
|
|
|
|
for child in self.children:
|
|
|
|
child.update_children()
|
2016-09-07 04:57:50 +08:00
|
|
|
else:
|
2015-10-14 07:16:29 +08:00
|
|
|
self.children = None
|
|
|
|
return self.children
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-14 07:16:29 +08:00
|
|
|
def get_num_visible_rows(self):
|
|
|
|
rows = 1
|
|
|
|
if self.is_expanded:
|
|
|
|
children = self.get_children()
|
2015-10-16 08:34:18 +08:00
|
|
|
if children:
|
|
|
|
for child in children:
|
|
|
|
rows += child.get_num_visible_rows()
|
2015-10-14 07:16:29 +08:00
|
|
|
return rows
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-14 07:16:29 +08:00
|
|
|
def draw(self, tree_window, row):
|
|
|
|
display_row = tree_window.get_display_row(row)
|
|
|
|
if display_row >= 0:
|
|
|
|
tree_window.move(tree_window.get_item_draw_point(row))
|
|
|
|
if self.parent:
|
|
|
|
self.parent.draw_tree_for_child(tree_window, self, 0)
|
|
|
|
if self.might_have_children():
|
2016-09-07 04:57:50 +08:00
|
|
|
tree_window.addch(curses.ACS_DIAMOND)
|
|
|
|
tree_window.addch(curses.ACS_HLINE)
|
2015-10-15 08:49:36 +08:00
|
|
|
elif self.parent and self.parent.children_might_have_children():
|
2015-10-16 08:34:18 +08:00
|
|
|
if self.parent.parent:
|
2016-09-07 04:57:50 +08:00
|
|
|
tree_window.addch(curses.ACS_HLINE)
|
|
|
|
tree_window.addch(curses.ACS_HLINE)
|
2015-10-16 08:34:18 +08:00
|
|
|
else:
|
2016-09-07 04:57:50 +08:00
|
|
|
tree_window.addch(' ')
|
|
|
|
tree_window.addch(' ')
|
2015-10-14 07:16:29 +08:00
|
|
|
is_selected = tree_window.is_selected(row)
|
|
|
|
if is_selected:
|
2016-09-07 04:57:50 +08:00
|
|
|
tree_window.attron(curses.A_REVERSE)
|
2015-10-14 07:16:29 +08:00
|
|
|
self.delegate.draw_item(tree_window, self)
|
|
|
|
if is_selected:
|
2016-09-07 04:57:50 +08:00
|
|
|
tree_window.attroff(curses.A_REVERSE)
|
|
|
|
|
|
|
|
def draw_tree_for_child(self, tree_window, child, reverse_depth):
|
2015-10-14 07:16:29 +08:00
|
|
|
if self.parent:
|
2016-09-07 04:57:50 +08:00
|
|
|
self.parent.draw_tree_for_child(
|
|
|
|
tree_window, self, reverse_depth + 1)
|
2015-10-14 07:16:29 +08:00
|
|
|
if self.children[-1] == child:
|
|
|
|
# Last child
|
|
|
|
if reverse_depth == 0:
|
2016-09-07 04:57:50 +08:00
|
|
|
tree_window.addch(curses.ACS_LLCORNER)
|
|
|
|
tree_window.addch(curses.ACS_HLINE)
|
2015-10-14 07:16:29 +08:00
|
|
|
else:
|
2016-09-07 04:57:50 +08:00
|
|
|
tree_window.addch(' ')
|
|
|
|
tree_window.addch(' ')
|
2015-10-14 07:16:29 +08:00
|
|
|
else:
|
|
|
|
# Middle child
|
|
|
|
if reverse_depth == 0:
|
2016-09-07 04:57:50 +08:00
|
|
|
tree_window.addch(curses.ACS_LTEE)
|
|
|
|
tree_window.addch(curses.ACS_HLINE)
|
2015-10-14 07:16:29 +08:00
|
|
|
else:
|
2016-09-07 04:57:50 +08:00
|
|
|
tree_window.addch(curses.ACS_VLINE)
|
|
|
|
tree_window.addch(' ')
|
|
|
|
|
|
|
|
def was_selected(self):
|
2015-10-16 08:34:18 +08:00
|
|
|
self.delegate.do_action()
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
|
2015-10-14 07:16:29 +08:00
|
|
|
class TreePanel(Panel):
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-14 07:16:29 +08:00
|
|
|
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
|
2016-09-07 04:57:50 +08:00
|
|
|
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")
|
2015-10-14 07:16:29 +08:00
|
|
|
|
|
|
|
def get_selected_item(self):
|
|
|
|
if self.selected_idx < len(self.items):
|
|
|
|
return self.items[self.selected_idx]
|
|
|
|
else:
|
|
|
|
return None
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-14 07:16:29 +08:00
|
|
|
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
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-14 07:16:29 +08:00
|
|
|
def get_visible_items(self):
|
2016-09-07 04:57:50 +08:00
|
|
|
# Clear self.items when you want to update all chidren
|
2015-10-14 07:16:29 +08:00
|
|
|
if self.items is None:
|
|
|
|
self.items = list()
|
|
|
|
children = self.root_item.get_children()
|
|
|
|
if children:
|
|
|
|
for child in children:
|
|
|
|
child.append_visible_items(self.items)
|
|
|
|
return self.items
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-14 07:16:29 +08:00
|
|
|
def update(self):
|
2016-09-07 04:57:50 +08:00
|
|
|
self.erase()
|
|
|
|
self.draw_title_box(self.title)
|
2015-10-14 07:16:29 +08:00
|
|
|
visible_items = self.get_visible_items()
|
|
|
|
for (row, child) in enumerate(visible_items):
|
2016-09-07 04:57:50 +08:00
|
|
|
child.draw(self, row)
|
2015-10-14 07:16:29 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def get_item_draw_point(self, row):
|
2015-10-14 07:16:29 +08:00
|
|
|
display_row = self.get_display_row(row)
|
|
|
|
if display_row >= 0:
|
|
|
|
return Point(2, display_row + 1)
|
|
|
|
else:
|
|
|
|
return Point(-1, -1)
|
|
|
|
|
|
|
|
def get_display_row(self, row):
|
|
|
|
if row >= self.first_visible_idx:
|
|
|
|
display_row = row - self.first_visible_idx
|
2016-09-07 04:57:50 +08:00
|
|
|
if display_row < self.get_size().h - 2:
|
|
|
|
return display_row
|
2015-10-14 07:16:29 +08:00
|
|
|
return -1
|
|
|
|
|
|
|
|
def is_selected(self, row):
|
|
|
|
return row == self.selected_idx
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def get_num_lines(self):
|
2015-10-16 08:34:18 +08:00
|
|
|
self.get_visible_items()
|
|
|
|
return len(self.items)
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-14 07:16:29 +08:00
|
|
|
def get_num_visible_lines(self):
|
2016-09-07 04:57:50 +08:00
|
|
|
return self.get_size().h - 2
|
|
|
|
|
|
|
|
def select_next(self):
|
|
|
|
self.selected_idx += 1
|
2015-10-14 07:16:29 +08:00
|
|
|
num_lines = self.get_num_lines()
|
|
|
|
if self.selected_idx >= num_lines:
|
2016-09-07 04:57:50 +08:00
|
|
|
self.selected_idx = num_lines - 1
|
2015-10-16 08:34:18 +08:00
|
|
|
self._selection_changed()
|
2015-10-14 07:16:29 +08:00
|
|
|
self.refresh()
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def select_prev(self):
|
2015-10-14 07:16:29 +08:00
|
|
|
self.selected_idx -= 1
|
|
|
|
if self.selected_idx < 0:
|
|
|
|
num_lines = self.get_num_lines()
|
|
|
|
if num_lines > 0:
|
|
|
|
self.selected_idx = 0
|
|
|
|
else:
|
|
|
|
self.selected_idx = -1
|
2015-10-16 08:34:18 +08:00
|
|
|
self._selection_changed()
|
2015-10-14 07:16:29 +08:00
|
|
|
self.refresh()
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def scroll_begin(self):
|
2015-10-14 07:16:29 +08:00
|
|
|
self.first_visible_idx = 0
|
|
|
|
num_lines = self.get_num_lines()
|
|
|
|
if num_lines > 0:
|
|
|
|
self.selected_idx = 0
|
|
|
|
else:
|
|
|
|
self.selected_idx = -1
|
2015-10-16 08:34:18 +08:00
|
|
|
self.refresh()
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-14 07:16:29 +08:00
|
|
|
def redisplay_tree(self):
|
|
|
|
self.items = None
|
|
|
|
self.refresh()
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def right_arrow(self):
|
2015-10-14 07:16:29 +08:00
|
|
|
selected_item = self.get_selected_item()
|
|
|
|
if selected_item and selected_item.is_expanded == False:
|
|
|
|
selected_item.is_expanded = True
|
|
|
|
self.redisplay_tree()
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-14 07:16:29 +08:00
|
|
|
def left_arrow(self):
|
|
|
|
selected_item = self.get_selected_item()
|
|
|
|
if selected_item:
|
2016-09-07 04:57:50 +08:00
|
|
|
if selected_item.is_expanded:
|
2015-10-14 07:16:29 +08:00
|
|
|
selected_item.is_expanded = False
|
|
|
|
self.redisplay_tree()
|
|
|
|
elif selected_item.parent:
|
|
|
|
if self.select_item(selected_item.parent):
|
|
|
|
self.refresh()
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def scroll_end(self):
|
2015-10-14 07:16:29 +08:00
|
|
|
num_visible_lines = self.get_num_visible_lines()
|
2015-10-16 08:34:18 +08:00
|
|
|
num_lines = self.get_num_lines()
|
2015-10-14 07:16:29 +08:00
|
|
|
if num_lines > num_visible_lines:
|
|
|
|
self.first_visible_idx = num_lines - num_visible_lines
|
|
|
|
else:
|
|
|
|
self.first_visible_idx = 0
|
2016-09-07 04:57:50 +08:00
|
|
|
self.selected_idx = num_lines - 1
|
2015-10-16 08:34:18 +08:00
|
|
|
self.refresh()
|
2015-10-14 07:16:29 +08:00
|
|
|
|
|
|
|
def scroll_page_backward(self):
|
|
|
|
num_visible_lines = self.get_num_visible_lines()
|
2015-10-16 08:34:18 +08:00
|
|
|
new_index = self.selected_idx - num_visible_lines
|
2015-10-14 07:16:29 +08:00
|
|
|
if new_index < 0:
|
2015-10-16 08:34:18 +08:00
|
|
|
self.selected_idx = 0
|
2015-10-14 07:16:29 +08:00
|
|
|
else:
|
2016-09-07 04:57:50 +08:00
|
|
|
self.selected_idx = new_index
|
2015-10-16 08:34:18 +08:00
|
|
|
self._selection_changed()
|
2015-10-14 07:16:29 +08:00
|
|
|
self.refresh()
|
|
|
|
|
|
|
|
def scroll_page_forward(self):
|
2015-10-16 08:34:18 +08:00
|
|
|
num_lines = self.get_num_lines()
|
2015-10-14 07:16:29 +08:00
|
|
|
num_visible_lines = self.get_num_visible_lines()
|
2015-10-16 08:34:18 +08:00
|
|
|
new_index = self.selected_idx + num_visible_lines
|
|
|
|
if new_index >= num_lines:
|
|
|
|
new_index = num_lines - 1
|
|
|
|
self.selected_idx = new_index
|
|
|
|
self._selection_changed()
|
2015-10-14 07:16:29 +08:00
|
|
|
self.refresh()
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def _selection_changed(self):
|
|
|
|
num_lines = self.get_num_lines()
|
2015-10-14 07:16:29 +08:00
|
|
|
num_visible_lines = self.get_num_visible_lines()
|
2015-10-16 08:34:18 +08:00
|
|
|
last_visible_index = self.first_visible_idx + num_visible_lines
|
|
|
|
if self.selected_idx >= last_visible_index:
|
2016-09-07 04:57:50 +08:00
|
|
|
self.first_visible_idx += (self.selected_idx -
|
|
|
|
last_visible_index + 1)
|
2015-10-16 08:34:18 +08:00
|
|
|
if self.selected_idx < self.first_visible_idx:
|
2016-09-07 04:57:50 +08:00
|
|
|
self.first_visible_idx = self.selected_idx
|
2015-10-16 08:34:18 +08:00
|
|
|
if self.selected_idx >= 0 and self.selected_idx < len(self.items):
|
|
|
|
item = self.items[self.selected_idx]
|
|
|
|
item.was_selected()
|
2015-10-14 07:16:29 +08:00
|
|
|
|
|
|
|
|
2015-10-08 04:00:28 +08:00
|
|
|
class Menu(BoxedPanel):
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-08 04:00:28 +08:00
|
|
|
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)
|
2016-09-07 04:57:50 +08:00
|
|
|
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)
|
2015-10-08 04:00:28 +08:00
|
|
|
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()
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-08 04:00:28 +08:00
|
|
|
def update(self):
|
|
|
|
super(Menu, self).update()
|
|
|
|
|
|
|
|
def relinquish_first_responder(self):
|
|
|
|
if not self.hidden():
|
2016-09-07 04:57:50 +08:00
|
|
|
self.hide()
|
|
|
|
|
|
|
|
def perform_action(self):
|
2015-10-08 04:00:28 +08:00
|
|
|
selected_idx = self.get_selected_idx()
|
|
|
|
if selected_idx < len(self.items):
|
|
|
|
action = self.items[selected_idx].action
|
|
|
|
if action:
|
|
|
|
action()
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
|
2015-10-08 04:00:28 +08:00
|
|
|
class MenuBar(Panel):
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-08 04:00:28 +08:00
|
|
|
def __init__(self, frame):
|
|
|
|
super(MenuBar, self).__init__(frame, can_become_first_responder=True)
|
|
|
|
self.menus = list()
|
|
|
|
self.selected_menu_idx = -1
|
2016-09-07 04:57:50 +08:00
|
|
|
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")
|
2015-10-08 04:00:28 +08:00
|
|
|
|
2015-10-27 00:51:20 +08:00
|
|
|
def insert_menu(self, menu, index=sys.maxsize):
|
2015-10-08 04:00:28 +08:00
|
|
|
if index >= len(self.menus):
|
|
|
|
self.menus.append(menu)
|
|
|
|
else:
|
|
|
|
self.menus.insert(index, menu)
|
|
|
|
pt = self.get_position()
|
|
|
|
for menu in self.menus:
|
|
|
|
menu.set_position(pt)
|
|
|
|
pt.x += len(menu.title) + 5
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
def perform_action(self):
|
2015-10-08 04:00:28 +08:00
|
|
|
'''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
|
|
|
|
for menu in self.menus:
|
|
|
|
if not menu.hidden():
|
|
|
|
menu_visible = True
|
|
|
|
break
|
|
|
|
if menu_visible:
|
|
|
|
menu.perform_action()
|
|
|
|
self.selected_menu_idx = -1
|
|
|
|
self._selected_menu_changed()
|
|
|
|
else:
|
|
|
|
self.select(0)
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-08 04:00:28 +08:00
|
|
|
def relinquish_first_responder(self):
|
|
|
|
if self.selected_menu_idx >= 0:
|
|
|
|
self.selected_menu_idx = -1
|
|
|
|
self._selected_menu_changed()
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-08 04:00:28 +08:00
|
|
|
def _selected_menu_changed(self):
|
|
|
|
for (menu_idx, menu) in enumerate(self.menus):
|
|
|
|
is_hidden = menu.hidden()
|
2016-09-07 04:57:50 +08:00
|
|
|
if menu_idx != self.selected_menu_idx:
|
2015-10-08 04:00:28 +08:00
|
|
|
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()
|
2016-09-07 04:57:50 +08:00
|
|
|
if menu_idx == self.selected_menu_idx:
|
2015-10-08 04:00:28 +08:00
|
|
|
if is_hidden:
|
|
|
|
menu.show()
|
|
|
|
self.parent.push_first_responder(menu)
|
|
|
|
menu.top()
|
|
|
|
self.parent.refresh()
|
|
|
|
|
|
|
|
def select(self, index):
|
|
|
|
if index < len(self.menus):
|
|
|
|
self.selected_menu_idx = index
|
|
|
|
self._selected_menu_changed()
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
def select_next(self):
|
2015-10-08 04:00:28 +08:00
|
|
|
num_menus = len(self.menus)
|
|
|
|
if self.selected_menu_idx == -1:
|
|
|
|
if num_menus > 0:
|
|
|
|
self.selected_menu_idx = 0
|
|
|
|
self._selected_menu_changed()
|
|
|
|
else:
|
|
|
|
if self.selected_menu_idx + 1 < num_menus:
|
|
|
|
self.selected_menu_idx += 1
|
|
|
|
else:
|
|
|
|
self.selected_menu_idx = -1
|
|
|
|
self._selected_menu_changed()
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def select_prev(self):
|
2015-10-08 04:00:28 +08:00
|
|
|
num_menus = len(self.menus)
|
|
|
|
if self.selected_menu_idx == -1:
|
|
|
|
if num_menus > 0:
|
|
|
|
self.selected_menu_idx = num_menus - 1
|
|
|
|
self._selected_menu_changed()
|
|
|
|
else:
|
|
|
|
if self.selected_menu_idx - 1 >= 0:
|
|
|
|
self.selected_menu_idx -= 1
|
|
|
|
else:
|
|
|
|
self.selected_menu_idx = -1
|
|
|
|
self._selected_menu_changed()
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
self.erase()
|
|
|
|
is_in_first_responder_chain = self.is_in_first_responder_chain()
|
|
|
|
if is_in_first_responder_chain:
|
2016-09-07 04:57:50 +08:00
|
|
|
self.attron(curses.A_REVERSE)
|
2015-10-08 04:00:28 +08:00
|
|
|
pt = Point(x=0, y=0)
|
2016-09-07 04:57:50 +08:00
|
|
|
for menu in self.menus:
|
2015-10-08 04:00:28 +08:00
|
|
|
self.addstr(pt, '| ' + menu.title + ' ')
|
|
|
|
pt.x += len(menu.title) + 5
|
2016-09-07 04:57:50 +08:00
|
|
|
self.addstr(pt, '|')
|
2015-10-08 04:00:28 +08:00
|
|
|
width = self.get_size().w
|
|
|
|
while pt.x < width:
|
2015-10-14 07:16:29 +08:00
|
|
|
self.addch_at_point(pt, ' ')
|
2016-09-07 04:57:50 +08:00
|
|
|
pt.x += 1
|
2015-10-08 04:00:28 +08:00
|
|
|
if is_in_first_responder_chain:
|
2016-09-07 04:57:50 +08:00
|
|
|
self.attroff(curses.A_REVERSE)
|
2015-10-08 04:00:28 +08:00
|
|
|
|
|
|
|
for menu in self.menus:
|
|
|
|
menu.update()
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
|
2015-09-19 08:39:09 +08:00
|
|
|
class StatusPanel(Panel):
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-09-19 08:39:09 +08:00
|
|
|
def __init__(self, frame):
|
2016-09-07 04:57:50 +08:00
|
|
|
super(
|
|
|
|
StatusPanel,
|
|
|
|
self).__init__(
|
|
|
|
frame,
|
|
|
|
delegate=None,
|
|
|
|
can_become_first_responder=False)
|
2015-09-19 08:39:09 +08:00
|
|
|
self.status_items = list()
|
|
|
|
self.status_dicts = dict()
|
|
|
|
self.next_status_x = 1
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-09-19 08:39:09 +08:00
|
|
|
def add_status_item(self, name, title, format, width, value, update=True):
|
2016-09-07 04:57:50 +08:00
|
|
|
status_item_dict = {'name': name,
|
|
|
|
'title': title,
|
|
|
|
'width': width,
|
|
|
|
'format': format,
|
|
|
|
'value': value,
|
|
|
|
'x': self.next_status_x}
|
2015-09-19 08:39:09 +08:00
|
|
|
index = len(self.status_items)
|
|
|
|
self.status_items.append(status_item_dict)
|
|
|
|
self.status_dicts[name] = index
|
2016-09-07 04:57:50 +08:00
|
|
|
self.next_status_x += width + 2
|
2015-09-19 08:39:09 +08:00
|
|
|
if update:
|
|
|
|
self.update()
|
|
|
|
|
|
|
|
def increment_status(self, name, update=True):
|
|
|
|
if name in self.status_dicts:
|
|
|
|
status_item_idx = self.status_dicts[name]
|
|
|
|
status_item_dict = self.status_items[status_item_idx]
|
|
|
|
status_item_dict['value'] = status_item_dict['value'] + 1
|
|
|
|
if update:
|
|
|
|
self.update()
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-09-19 08:39:09 +08:00
|
|
|
def update_status(self, name, value, update=True):
|
|
|
|
if name in self.status_dicts:
|
|
|
|
status_item_idx = self.status_dicts[name]
|
|
|
|
status_item_dict = self.status_items[status_item_idx]
|
|
|
|
status_item_dict['value'] = status_item_dict['format'] % (value)
|
|
|
|
if update:
|
|
|
|
self.update()
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-09-19 08:39:09 +08:00
|
|
|
def update(self):
|
2016-09-07 04:57:50 +08:00
|
|
|
self.erase()
|
2015-09-19 08:39:09 +08:00
|
|
|
for status_item_dict in self.status_items:
|
2016-09-07 04:57:50 +08:00
|
|
|
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'])
|
2015-09-19 08:39:09 +08:00
|
|
|
|
|
|
|
stdscr = None
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-09-19 08:39:09 +08:00
|
|
|
def intialize_curses():
|
|
|
|
global stdscr
|
|
|
|
stdscr = curses.initscr()
|
|
|
|
curses.noecho()
|
|
|
|
curses.cbreak()
|
|
|
|
stdscr.keypad(1)
|
|
|
|
try:
|
|
|
|
curses.start_color()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
return Window(stdscr)
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-09-19 08:39:09 +08:00
|
|
|
def terminate_curses():
|
|
|
|
global stdscr
|
|
|
|
if stdscr:
|
|
|
|
stdscr.keypad(0)
|
|
|
|
curses.echo()
|
|
|
|
curses.nocbreak()
|
|
|
|
curses.endwin()
|